2016-08-04 28 views
2

Mit Select2 mit JSON/Ajax-Aufruf, Ich versuche Gruppe wie diese zu erstellen:Select2 optgroup Gruppe auf JSON/Ajax-Format

<optgroup label="Property Reference"> 
    <option value="1">5742</option> 
    <option value="2">5788</option> 
    <option value="3">5762</option> 
    <option value="4">5711</option> 
</optgroup> 

Die Dokumentation für mein Problem is not available.


Das Format, das ich zur Arbeit kommen verwaltet:

{ 
    results: [ 
     { 
      id: 'CA', 
      text: 'California' 
     }, 
     { 
      id: 'CO', 
      text: 'Colarado' 
     ] 
    } 
} 

Aber es gibt hier keine Gruppe.

this question Folgen, habe ich versucht, das folgende Format:

{ 
    "results": { 
     "text": "Mountain Time Zone", 
     "children": [ 
      { 
       "id": "CA", 
       "text": "California" 
      }, 
      { 
       "id": "CO", 
       "text": "Colorado" 
      } 
     ] 
    } 
} 

und

{ 
    "results": { 
     "Mountain Time Zone": [ 
      { 
       "id": "CA", 
       "text": "California" 
      }, 
      { 
       "id": "CO", 
       "text": "Colorado" 
      } 
     ] 
    } 
} 

und

{ 
    "Mountain Time Zone": [ 
     { 
      "id": "CA", 
      "text": "California" 
     }, 
     { 
      "id": "CO", 
      "text": "Colorado" 
     } 
    ] 
} 

Aber keiner arbeiten. Wer weiß, wie das richtige Format ist?

Sie können es hier testen: https://jsfiddle.net/5am4zda6/2/

EDIT Gelöst: Verdammt ... vergessen []. Richtiges Format ist:

{ 
    "results": [ 
     { 
      "text": "Mountain Time Zone", 
      "children": [ 
       { 
        "id": "CA", 
        "text": "California" 
       }, 
       { 
        "id": "CO", 
        "text": "Colarado" 
       } 
      ] 
     } 
    ] 
} 
+0

Arbeits Es ist nicht an der Geige vergessen haben. –

Antwort

2

Versuchen wie folgt aus:

{ 
    "results": [ 
     { 
      "text": "Groupe 1", 
      "children": [ 
       { 
        "id": "CA", 
        "text": "California" 
       }, 
       { 
        "id": "CO", 
        "text": "Colarado" 
       } 
      ] 
     }, 
     { 
      "text": "Groupe 2", 
      "children": [ 
       { 
        "id": "CA", 
        "text": "California" 
       }, 
       { 
        "id": "CO", 
        "text": "Colarado" 
       } 
      ] 
     } 
    ] 
} 

Ich glaube, Sie [ juste nach "Ergebnisse"

0

ich nicht dieses Plugin, bevor versucht haben, aber nach an der damit verbundenen Frage suchen Sie erwähnt haben, versuchen Sie das „Ergebnis“ durch Index wie „data“ etwas zu ändern dies:

{ 
    "data" : { 
     'text': 'Mountain Time Zone', 
     'children': [ 
      { 
      'id': 'CA', 
      'text': 'California' 
      }, 
      { 
      'id': 'CO', 
      'text': 'Colorado' 
      } 
     ] 
    } 
} 

Aber es wäre besser, wenn Sie uns eine funktionierende jsfiddle liefern könnten.

+0

Danke für Ihre Antwort. Leider funktioniert es auch nicht ... Ich versuche, ein Problem zu lösen. – hg8

+0

jsfiddle hinzugefügt. – hg8