简体   繁体   English

select2 为每个选项手动设置选项属性

[英]select2 set option attributes manually for each option

i have select2 dropdown like:我有select2下拉菜单,如:

 <select class="form-control validateblank txtSelectChallan" id="txtSelectChallan" />

and i am setting dropdown data by ajax call like:我正在通过ajax调用设置dropdown数据,例如:

   $.ajax({
    type: "POST",
    url: "/Account/MaterialSheet.aspx/GetMaterialSheetByLedgerId",
    data: '{LedgerId: "' + AccId + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        if (data.d.Result == "OK") {
            var challanresults = [];
            $.each(data.d.Records, function (index, challn) {
                challanresults.push({
                    id: challn.MaterialSheet_ID,
                    text: challn.Challan_No,
                    Amount: challn.Total_Amount
                });
            });

            eachtr.find('.txtSelectChallan').select2({
                placeholder: "Select Challan",
                data: challanresults,
                multiple: true
            });
            swal.close();
            challanresults = null;
        }
    },
    error: function (err) {
        swal(
           'Oops...',
           'Error occured while retrieving data',
           'error'
         );
    }
});

and i get dropdown like :我得到如下dropdown

<select class="form-control validateblank txtSelectChallan select2 hidden-accessible" id="txtSelectChallan" tabindex="-1" aria-hidden="true" multiple="">
  <option value="1006">123123</option>
  <option value="1007">32123</option>

i have tried to set option attribute using:我尝试使用以下方法设置option属性:

             challanresults.push({
                    id: challn.MaterialSheet_ID,
                    text: challn.Challan_No,
                    Amount: challn.Total_Amount
                });

but i cant get amout as option attribute any idea how to set custom attribute for all option in select2 ?但我无法将数量作为option属性知道如何为select2所有option设置自定义属性?

Try like this inside foreach loop, and set the trigger after that.在 foreach 循环中尝试这样,然后设置触发器。

var data = {
id: challn.MaterialSheet_ID,
text: challn.Challan_No
};

var newOption = new Option(data.text, data.id, false, false);
$('#txtSelectChallan').append(newOption).trigger('change');

Check this link for further solution on custom attributes 检查此链接以获取有关自定义属性的进一步解决方案

Or Simply you can do like this in a loop for the result set var option = "<option value="+challn.MaterialSheet_ID+" amount="+challn.Total_Amount+">"+challn.Challan_No+"</option>或者,您可以在结果集的循环中这样做var option = "<option value="+challn.MaterialSheet_ID+" amount="+challn.Total_Amount+">"+challn.Challan_No+"</option>

This is what Select2 Official Site has to say about custom-data-fields这是 Select2 官方网站对自定义数据字段的说明

$('#mySelect2').select2({
// ...
templateSelection: function (data, container) {
// Add custom attributes to the <option> tag for the selected option
$(data.element).attr('data-custom-attribute', data.customValue);
return data.text;
}
});

// Retrieve custom attribute value of the first selected element
$('#mySelect2').find(':selected').data('custom-attribute');

Click here for the above reference link 点击此处查看以上参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM