无法从select2搜索结果中选择结果

时间:2013-04-10 18:40:17

标签: php html json jquery-select2

我在我的搜索框上使用select2。我从我的网址获得结果,但我无法从中选择一个选项。我想使用'product.productName'作为选择后显示的文本。有什么我错过了或我做的任何错误。我已经包含了select2.css和select2.min.js,jquery.js

  function dataFormatResult(product) {
        var markup = "<table class='product-result'><tr>";

        markup += "<td class='product-info'><div class='product-title'>" +     product.productName + "</div>";
        if (product.manufacturer !== undefined) {
            markup += "<div class='product-synopsis'>" + product.manufacturer + "</div>";
        }
        else if (product.productOptions !== undefined) {
            markup += "<div class='product-synopsis'>" + product.productOptions + "</div>";
        }
        markup += "</td></tr></table>";
        return markup;
    }

    function dataFormatSelection(product) {
        return product.productName;
    }
    $(document).ready(function() {
        $("#e7").select2({
            placeholder: "Search for a product",
            minimumInputLength: 2,
            ajax: {
                url: myURL,
                dataType: 'json',
                data: function(term,page) {
                    return {
                        productname: term 
                    };
                },
                results: function(data,page) { 

                    return {results: data.result_object};
                }
            },
            formatResult: dataFormatResult, 
            formatSelection: dataFormatSelection, 
            dropdownCssClass: "bigdrop", 
            escapeMarkup: function(m) {
                return m;
            } 
        });
    });

这是我的resut_object

"result_object":[{"productName":"samsung galaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Silver;32GB"},{"productName":"samsung salaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Graphite;32GB"},{"productName":"samsung galaxy s3","manufacturer":"Samsung","productOptions":"Color;Memory","productOptiondesc":"Silver;16GB"}]

4 个答案:

答案 0 :(得分:35)

您缺少结果数据的ID属性。如果没有,它会使选项“无法选择”。

示例:

            $('#e7').select2({
                    id: function(e) { return e.productName; },
            });

答案 1 :(得分:0)

我遇到了同样的问题,这个问题的其他解决方案是: -

在您的响应对象中(在上面的响应中,产品详细信息对象)必须具有&#34; id&#34;作为关键和价值。

示例: - 您上面给出的响应对象必须像这样

{&#34; id&#34;:&#34; 1&#34;,&#34; productName&#34;:&#34; samsung galaxy s3&#34;,&#34;制造商&#34; :&#34;三星&#34;&#34; productOptions&#34;:&#34;颜色;存储&#34;&#34; productOptiondesc&#34;:&#34;银; 32GB&#34;}

所以你不需要这个 id:function(object){return object.key;}

答案 2 :(得分:0)

id param可以是与对象属性名称相关的字符串,并且必须位于对象的根目录中。数据对象内的文本。

var fruits = [{code: 222, fruit: 'grape', color:'purple', price: 2.2},
  {code: 234,fruit: 'banana', color:'yellow', price: 1.9} ];

$(yourfield).select2(
 {
   id: 'code',
   data: { results: fruits, text: 'fruit' }
 }
);

答案 3 :(得分:0)

由于我使用的是AJAX,对我有用的是在processResults上返回ID作为ID:

$(field).select2({
   ajax: {
        // [..] ajax params here
        processResults: function(data) {
            return {
                results: $.map(data, function(item) {
                    return {
                        // proccessResults NEEDS the attribute id here
                        id: item.code,
                        // [...] other attributes here
                        foo: item.bar,
                    }
                })
            }
        },
    },
});