Home > database >  How can I select children options from optgroup in Select2?
How can I select children options from optgroup in Select2?

Time:06-14

Question: I have successfully created a select2 template with a group but I can't select the items in it, is there anything missing from the code I made below? Any help from you means a lot to me, thank you


See this first, Preview Image

For the data below, i folow https://select2.org/data-sources/formats

Result from AJAX :

{
  "status": true,
  "message": null,
  "data": [
    {
      "text": "Bank",
      "children": [
        {
          "id": "002",
          "icon": ".../payment/bri_1.png",
          "text": "BANK BRI"
        },
        {
          "id": "008",
          "icon": ".../payment/mandiri_1.png",
          "text": "BANK MANDIRI"
        },
        {
          "id": "009",
          "icon": ".../payment/bni_1.png",
          "text": "BANK BNI / SYARIAH"
        },
        {
          "id": "014",
          "icon": ".../payment/bca_1.png",
          "text": "BANK BCA"
        },
        {
          "id": "022",
          "icon": ".../payment/1280px-CIMB_Niaga_logo.svg.png",
          "text": "CIMB NIAGA / SYARIAH"
        },
        {
          "id": "016",
          "icon": ".../payment/Maybank-Logo.png",
          "text": "Maybank"
        },
        {
          "id": "013",
          "icon": ".../payment/images.png",
          "text": "PERMATA BANK"
        },
        {
          "id": "213",
          "icon": ".../payment/Jenius-logo.png",
          "text": "Jenius/BTPN"
        },
        {
          "id": "011",
          "icon": ".../payment/kissclipart-danamon-logo-png-clipart-bank-danamon-logo-a9b2b21755c37a3a.png",
          "text": "Danamon"
        },
        {
          "id": "012",
          "icon": ".../payment/images_1.png",
          "text": "Bank Neo Commerce/Bank Yudha Bakti"
        },
        {
          "id": "017",
          "icon": ".../payment/images-removebg-preview.png",
          "text": "Bank Syariah Indonesia"
        }
      ]
    },
    {
      "text": "eMoney",
      "children": [
        {
          "id": "900",
          "icon": ".../payment/ovo.png",
          "text": "OVO"
        },
        {
          "id": "901",
          "icon": ".../payment/gopay.png",
          "text": "GOPAY"
        },
        {
          "id": "903",
          "icon": ".../payment/dana_1.png",
          "text": "DANA"
        },
        {
          "id": "904",
          "icon": ".../payment/linkaja.png",
          "text": "LINK AJA"
        },
        {
          "id": "906",
          "icon": ".../payment/shopeepay-shopee.co.id_ratio-16x9.jpg",
          "text": "SHOPEE PAY"
        }
      ]
    }
  ]
}

Code :

$("#payment").select2({
  placeholder: 'Payment',
  width: 'resolve',
  minimumResultsForSearch: Infinity,
  templateResult: function formatState(state) {
    if (!state.id) return state.text;
    var $state = $(
      '<span><img src="'   state.icon   '" style="width:30px;"/> '   state.text   '</span>'
    );
    return $state;
  },
  ajax: {
    url: base_url   'user/api/payment',
    dataType: 'json',
    processResults: function(data) {
      return {
        results: $.map(data.data, function(item, key) {
          var children = [];
          for (var k in item.children) {
            var childItem = [];
            childItem.id = item.children[k].id;
            childItem.icon = item.children[k].icon;
            childItem.text = item.children[k].text;
            children.push(childItem);
          }
          return {
            text: item.text,
            children: children,
          }
        })
      };
    },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<div >
  <label for="" >Payment</label>
  <div >
    <select name="payment" id="payment" >
    </select>
  </div>
</div>

CodePudding user response:

As far as I can see in your code, your API is returning results already formatted, so keep it simple. Please check this fiddle with a fully working example based on your code.

$("#payment").select2({
placeholder: 'Payment',
width: 'resolve',
ajax: {
    url: 'https://randomuser.me/api/', //a random URL to simulate a request response in jsfiddle
    dataType: 'json',
    processResults: (data) => ({ results: response.data })
}
});
  • Related