select2的使用

  • 简单的模糊查询方法如下
				<input name="product" type="text" id="product"/>
				
				if (data.productForms != null && data.productForms != '') {
                $.each(data.productForms, function (index, value) {
                    productArr.push(value.name);
                });
      
                $('#product').autocomplete({
                    source: productArr
                });
            }
  • input含有value的模糊查询方法如下:
    1. 方法1的方式如下,但是模糊查询的方式好像没有效果
$("#customerName").autocomplete({
                    source: function (request, response) {
                        response($.map(data.customerForms, function (item) {
                            return {
                                label: item.name,
                                value: item.customerId
                            }
                        }));
                    },
                    select: function (event, ui) {
                        $('#customerName').val(ui.item.label);
                        $('#customerId').val(ui.item.value);
                        return false;
                        // event.preventDefault();
                    }
                });
  1. 方式2的方式是有效果的
function AutoCompleteForOrg(data,ele_name,ele_id, attr_id) {
    $(ele_name).autocomplete({
        minLength: 0,
        source: function (req, res) {
            var term = req.term.trim();
            var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), "i");
            res($.map(data, function (el) {
                /!* Remove dashes from the source and compare with the term: *!/
                if (matcher.test(el.name || matcher.test(el[attr_id]))) {
                    $(ele_name).val(el.name);
                    $(ele_id).val(el[attr_id]);
                    return el;
                }
            }));
        },
        minChars: 0,
        max: 5,
        autoFill: true,
        mustMatch: true,
        matchContains: true,
        scrollHeight: 60,
        open: function (event, ui) {
            $('.ui-autocomplete').css('position', 'absolute');
        },
        focus: function (event, ui) {
            $(ele_name).attr('source', ui.item.source);
            return false;
        },
        select: function (event, ui) {
            $(ele_name).val(ui.item.name);
            $(ele_id).val(ui.item[attr_id]);
            console.log(ui.item[attr_id]);
            console.log(ele_id);
            return false;
        }
    }).data( "ui-autocomplete" )._renderItem = function(ul,item) {
        return $( "<li style='list-style:none;'>" )
            .append( "<a>" + item.name + "</a>" )
            .appendTo(ul);
    };

参考链接

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页