dourong8495 2013-04-10 18:40
浏览 144
已采纳

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

I am using the select2 for on of my search boxes. I'm getting the results from my URL but I'm not able to select an option from it. I want to use the 'product.productName' as the text to be shown after selection. Is there anything that I have missed out or any mistake that I have made. I have included select2.css and 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;
            } 
        });
    });

This is my 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条回答 默认 最新

  • duangang79177 2013-04-11 03:57
    关注

    You are missing id attribute for result data. if it has not, it makes option "unselectable".

    Example:

                $('#e7').select2({
                        id: function(e) { return e.productName; },
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度