Jquery ui自动填充搜索标签,值或其他字段?

时间:2016-02-24 07:03:23

标签: jquery jquery-ui autocomplete

<script>
    $(function() {
                    var products = [{"label":"Biscuit","value":532,"product_code":532,"product_name":"Biscuit","mrp":"25.00","selling_price":"22.00","measuring_type":"Qty","group_name":"TAX"},{"label":"Bread","value":533,"product_code":533,"product_name":"Bread","mrp":"23.00","selling_price":"21.00","measuring_type":"Qty","group_name":"NON TAXABLE"}];

        $( "#product_code" ).autocomplete({
            autoFocus: true,
            source: products,
            minLength: 0,
            focus: function(event, ui){
                $('#product_name').val(ui.item.product_name);
                $('#mrp').val(ui.item.mrp);
                $('#selling_price').val(ui.item.selling_price);
                $('#measuring_type').val(ui.item.measuring_type);
                $('.measuring_type').text(ui.item.measuring_type);
                $('#group_name').val(ui.item.group_name);
            },
            select: function(event, ui) {
                $('#product_code').val(ui.item.product_code);
                $('#product_name').val(ui.item.product_name);
                $('#mrp').val(ui.item.mrp);
                $('#selling_price').val(ui.item.selling_price);
                $('#measuring_type').val(ui.item.measuring_type);
                $('.measuring_type').text(ui.item.measuring_type);
                $('#group_name').val(ui.item.group_name);
                $('#quantity').focus();
            }
        }).focus(function(){
            $(this).autocomplete("search", "");
        });

        $("#product_code").autocomplete().data("uiAutocomplete")._renderItem =  function( ul, item )
        {
            return $( "<li>" )
                    .append( "<a>" + item.label + " - " + item.selling_price + "</a>" )
                    .appendTo( ul );
        };

    });
</script>

以上代码显示&#34;所有标签&#34;当我专注于自动完成输入时,我正在键入内容,以便过滤该标签。好到现在。

我希望输入的文字不仅可以单独搜索和返回标签,我希望它能够搜索匹配的文本值,或者是mrp或者卖价(&#34; mrp&#34;:&#34; 25.00&#34 ;,&#34; selling_price&#34;:&#34; 22.00&#34;),我在源头有这些东西。我怎么能实现它?

1 个答案:

答案 0 :(得分:1)

您可以像source一样从fucntion(request, response)返回function isInt(n) { return Number(n) === n && n % 1 === 0; } function isFloat(n) { return Number(n) === n && n % 1 !== 0; } $(function() { var products = [{ "label": "Cheese", "value": 531, "product_code": 531, "product_name": "Cheese", "mrp": "8.95", "selling_price": "6.95", "measuring_type": "Pnd", "group_name": "TAX" }, { "label": "Biscuit", "value": 532, "product_code": 532, "product_name": "Biscuit", "mrp": "25.00", "selling_price": "22.00", "measuring_type": "Qty", "group_name": "TAX" }, { "label": "Bread", "value": 533, "product_code": 533, "product_name": "Bread", "mrp": "23.00", "selling_price": "21.00", "measuring_type": "Qty", "group_name": "NON TAXABLE" }, { "label": "Cracker", "value": 534, "product_code": 534, "product_name": "Cracker", "mrp": "12.00", "selling_price": "7.00", "measuring_type": "Qty", "group_name": "TAX" }]; $("#product_code").autocomplete({ autoFocus: true, source: function(req, resp) { console.log(req); var results = []; if (isInt(parseInt(req.term)) || isFloat(parseFloat(req.term))) { // Number entered, check MRP or Selling Price console.log("Number found."); $.each(products, function(k, v) { var mrp = v.mrp.toString(); var sell = v.selling_price.toString(); if (mrp.startsWith(req.term) || sell.startsWith(req.term)) { results.push(products[k]); } }); } else { //Text entered, check labels console.log("Text found."); $.each(products, function(k, v) { var label = v.label.toLowerCase(); if (label.startsWith(req.term.toLowerCase())) { results.push(products[k]); } }); } resp(results); }, minLength: 0, focus: function(event, ui) { $('#product_name').val(ui.item.product_name); $('#mrp').val(ui.item.mrp); $('#selling_price').val(ui.item.selling_price); $('#measuring_type').val(ui.item.measuring_type); //$('.measuring_type').text(ui.item.measuring_type); $('#group_name').val(ui.item.group_name); }, select: function(event, ui) { $('#product_code').val(ui.item.product_code); $('#product_name').val(ui.item.product_name); $('#mrp').val(ui.item.mrp); $('#selling_price').val(ui.item.selling_price); $('#measuring_type').val(ui.item.measuring_type); //$('.measuring_type').text(ui.item.measuring_type); $('#group_name').val(ui.item.group_name); $('#quantity').focus(); } }).focus(function() { $(this).autocomplete("search", ""); }).autocomplete("instance")._renderItem = function(ul, item) { return $("<li>") .append("<a>" + item.label + " - $" + item.selling_price + "</a>") .appendTo(ul); } }); https://jsfiddle.net/Twisty/6xgg4074/5/

req.term

我添加了一些示例项来测试不同类型的条目。为了帮助识别号码或浮点数,例如21或21.00,我添加了一些小功能。为了创建搜索,我使用了一个函数,其中条目传递给resp(),回调函数是for idx,w in enumerate(df.groupby(pd.TimeGrouper("3w-SAT"))): # your first day is a saturday df.loc[w[0], "week"] = idx+1 # propagate the week number df["week"] = df.week.fillna(method="ffill") # remove added date by the Timegrouper as your number of date is not a multiple of 3 weeks. df.dropna(inplace=1) df.tail() x week 2013-07-16 15.717111 3 2013-07-17 9.815201 3 2013-07-18 9.426426 3 2013-07-19 12.725350 3 2013-07-20 16.100748 3 # just use seaborn as usual sns.boxplot(data=df, x="week", y="x") # plot it 。现在我们检查条目并确定用户是否输入了文本或某种类型的数字。然后,我们找到与此匹配的产品,并将它们作为一个数组返回。

这完全基于https://jqueryui.com/autocomplete/#remote-jsonp

中的示例