【问题标题】:Select2 load default Value in single select elements using remote dataSelect2 使用远程数据在单个选择元素中加载默认值
【发布时间】:2014-10-04 17:55:56
【问题描述】:

我使用jquery Select2,select2 工作得非常好。但我想要的是,当页面加载时,应该从控制器加载默认值。

是的,我已经在谷歌上搜索过,是的,这个问题已经发布了很多次,我无法完全理解它。所以这就是我需要帮助的原因。

这里是select2 js编码:

/*----------- Select2 DropDown Common Script  -------------------------*/
//For ServerSide Script
function commonSelect2(selector,url,id,text,minInputLength,placeholder){
    selector.select2({
        minimumInputLength:minInputLength,
        placeholder:placeholder,
        ajax: {
            type:"post",
            url: url,
            dataType: 'json',
            quietMillis: 100,
            data: function(term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            initSelection : function (element, callback) {
                var data = {id: element.val(), text: element.val()};
                callback(data);
            },
            results: function(data, page) {
                var newData = [];
                $.each(data, function (index,value) {
                    newData.push({
                        id: value[id],  //id part present in data
                        text: value[text]  //string to be displayed
                    });
                });
                return { results: newData };
            }
        }
    });
}

以及我如何从 php 文件中调用它

{{*The Selector for Selecting the User Group*}}
var selector = $('#selectGroup');
var url = "{{base_url()}}admin/usersManageUsers/loadAllUserGroups/";
var id = "GroupID";
var text = "GroupName";
var minInputLength = 0;
var placeholder = "Select User Group";
commonSelect2(selector,url,id,text,minInputLength,placeholder);
//End of the CommonSelect2 function

我添加了initselection()函数,但不知道我写的对不对?

最后我在 html select2 隐藏输入标签中添加了这个。

我尝试在网上搜索,但看起来我没有找到任何地方。

【问题讨论】:

    标签: javascript php jquery jquery-select2


    【解决方案1】:

    我的错误。 我在 ajax 中定义了 initSelection。应该在 ajax 之外定义它。

    现在在 ajax 之后定义 initSelection()

    ajax: {
        type:"post",
        url: url,
        dataType: 'json',
        quietMillis: 100,
        data: function(term, page) {
            return {
                term: term, //search term
                page_limit: 10 // page size
            };
        },
        results: function(data, page) {
            var newData = [];
            $.each(data, function (index,value) {
                newData.push({
                    id: value[id],  //id part present in data
                    text: value[text]  //string to be displayed
                });
            });
            return { results: newData };
        }
    },
    initSelection : function (element, callback) {
        var data = {id: element.val(), text: element.val()};
        callback(data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多