乏mily

导航

Select2

select2把select标签画成了别的的东西,常规的select对象被jquery藏了起来,所以修改值的时候使用dom对象的触发器才行。

初始化

显示5行selectDropDown,显示10行selectDropDownTen

 

$("#sectioncode").select2({

    dropdownCssClass: 'selectDropDownTen',//设置下拉框选项区域显示10行。  selectDropDown - 显示5行

    minimumResultsForSearch: '0' //带搜索下拉框(设置为7时,表示选项 >=7 时,显示搜索框;否则不显示搜索框)

});

 

取值、赋值

 
$("#id").select2("val", "2");//赋值
$("#id").val(); 或 $("#id").select2("val");//取值
$("#supplycode").select2("data").text;//取text

 

设置选中状态

1)       $("#id").val("XXXXX").select2()

2)       $(“#id”).val(data).trigger(‘change’); data是一个存放id值的数组,如 [1,2,3,4 ],或者[1,2,3,4,] 这样也可以。

 

第二种方法会出发change事件

 

 启用、禁用

                       //先启用 或 禁用 元素, 再实例化
                        this.initPatentHospital = function (isDisabled) {
                            if (isDisabled) {
                                $("#parenthospitalcode").prop("disabled", true);

                            } else {
                                $("#parenthospitalcode").prop("disabled", false);
                            }
                            w.find("#parenthospitalcode").select2({
                                dropdownCssClass: 'selectDropDownEight',
                                minimumResultsForSearch: '0'
                            });
                        }

 

ajax动态加载数据源

html元素:

<input type="text" id="firmcode" name="firmcode" style="width: 100%;"  data-bv-notempty="true" data-bv-notempty-message="@T("$basemodule_Can not be empty$")" />

JS代码:

 var firmUrl = '@HttpUtility.JavaScriptStringEncode(Url.Action("GetSupplyerList", "Item"))';

            $("#firmcode").select2({
                ajax: {
                    type: 'GET',
                    url: function (params) {
                        return firmUrl + '/' + params;
                    },
                    dataType: 'json',
                    results: function (data) {
                        return {
                            results: $.map(data.data, function (e) {
                                var id, text;
                                if (e[0].Key == "text") {
                                    text = e[0].Value;
                                    id = e[1].Value;
                                } else {
                                    text = e[1].Value;
                                    id = e[0].Value;
                                }
                                return { id: id, text: text };
                            })
                        };
                    },
                    //success: function () {
                    //    $("#firmcode").val(v_m.form.firmcode).trigger("change");
                    //}
                },
                placeholder: '请选择',
                dropdownCssClass: 'selectDropDownTen',
                minimumResultsForSearch: '10',
                formatSelection: function formatRepoSelection(repo) {
                    v_m.form.firm(repo.id);
                    return repo.text;
                } // 函数用于呈现当前的选择
            });
            //debugger; 下面的绑定 knockout v2.2.1 效果不好使,其他版本没试
            //$("#firmcode").val(v_m.form.firmcode()).trigger('change');
            //if ($('#firmcode').find("option[value='" + v_m.form.firmcode() + "']").length) {
            //    $('#firmcode').val(v_m.form.firmcode()).trigger('change');
            //} else {
            //    // Create a DOM Option and pre-select by default
            //    var newOption = new Option(v_m.form.firm(), v_m.form.firmcode(), true, true);
            //    // Append it to the select
            //    $('#firmcode').append(newOption).trigger('change');
            //} 

 

posted on 2019-06-19 17:25  乏mily  阅读(199)  评论(0编辑  收藏  举报