选择搜索和initSelection的2个问题

时间:2015-03-23 09:56:00

标签: jquery codeigniter jquery-select2

选择2代码:

$(".js-select").select2( {
    placeholder: "Select Something",

    ajax: {
        // The number of milliseconds to wait for the user to stop typing before issuing the ajax request
        delay: 400,
        url: "<?php echo site_url('main/get_items') ?>",
        dataType: "json",
        cache: "true",

        data: function (params) {
            return {
            q: params.term, // search term
            page: params.page,

            };
        },

        processResults: function (data) {
            return {
                results: $.map(data, function(obj) {
                    return { id: obj.id, text: obj.nume };
                })
            };
        },
    },

    initSelection : function (element, callback) {
         var data = {id: "1", text: "test relation"};
         callback(data);
    },
});

HTML:

<select class="js-select" name="judet" style="width: 100%" required> </select>
  1. 第一个问题: 使用initSelection不起作用:
  2. 代码:

    initSelection : function (element, callback) {
        var data = {id: "1", text: "test relation"};
        callback(data);
    },
    

    我看到“测试关系”文本,但是当我提交时,我没有得到id的值。

    1. Secound问题:
    2. 当我尝试从下拉列表中选择1项时... Select2初始化搜索...我需要select2才能在用户输入搜索内容时运行搜索...

      网页:http://www.my-web-projects.com/ajax/main/table - &gt;按钮ADD - &gt;下拉列表选择1并选择2

2 个答案:

答案 0 :(得分:1)

  

第一个问题:使用initSelection不起作用:

这听起来像是Select2中的一个错误,它应该尝试创建<option>元素,以便发送值。如果您能够完成有用的错误in a jsbinreport it on GitHub

  

当我尝试从下拉列表中选择1项时... Select2初始化搜索...我需要select2才能在用户输入搜索内容时运行搜索...

您应该可以使用minimumInputLength并将其设置为1来实现此目的。我们使用此in the AJAX example,因此请求仅在用户搜索时发送。

$element.select2({
  minimumInputLength: 1
});

答案 1 :(得分:0)

更改

var data = {id: "1", text: "test relation"};

var data = [{id: "1", text: "test relation"}];

EDITED

$(".js-select").select2({/* */}).select2("data", [{"id":"1","text":"test relation"}]);