jQuery UI自动完成:从对象数组加载:筛选已损坏

时间:2019-05-25 14:27:08

标签: jquery jquery-ui jquery-ui-autocomplete

我遵循JSFiddle给出的in this thread作为从简单对象数组加载jQuery UI自动完成的解决方案:

http://jsfiddle.net/khsbme4k/

此处过滤已中断。有2个带有First_Name字符串的数据行,“ Will”和“ Willem”,但是如果您键入其他任何内容,例如“ WA”您仍然可以选择2种商品,而应该没有。

  var data = [
        {
            "id": 1,
            "first_name": "Will",
            "last_name": "Smith",
            "created_at": "2015-01-27T13:09:20.243Z",
            "updated_at": "2015-01-27T13:09:20.243Z"
        },
        {
            "id": 2,
            "first_name": "Willem",
            "last_name": "Dafoe",
            "created_at": "2015-01-27T13:17:23.479Z",
            "updated_at": "2015-01-27T13:17:23.479Z"
        }
    ];

$('#search').autocomplete({
        // This shows the min length of charcters that must be typed before the autocomplete looks for a match.
        minLength: 2,
source: function (request, response) {
           response($.map(data, function (value, key) {
                return {
                    label: value.first_name,
                    value: value.id
                }
            }));

    },    
        focus: function(event, ui) {
            $('#search').val(ui.item.first_name);
            return false;
        },
        // Once a value in the drop down list is selected, do the following:
        select: function(event, ui) {
            // place the person.given_name value into the textfield called 'select_origin'...
            $('#search').val(ui.item.first_name);
            // and place the person.id into the hidden textfield called 'link_origin_id'. 
            $('#link_origin_id').val(ui.item.id);
                return false;
        }

2 个答案:

答案 0 :(得分:1)

使用indexOf()方法获取搜索项的自动完成列表。

     $(function() {
    var data = [
        {
            "id": 1,
            "first_name": "Will",
            "last_name": "Smith",
            "created_at": "2015-01-27T13:09:20.243Z",
            "updated_at": "2015-01-27T13:09:20.243Z"
        },
        {
            "id": 2,
            "first_name": "Willem",
            "last_name": "Dafoe",
            "created_at": "2015-01-27T13:17:23.479Z",
            "updated_at": "2015-01-27T13:17:23.479Z"
        }
    ];

var auto_array = {};
$('#search').autocomplete({
	// This shows the min length of charcters that must be typed before the autocomplete looks for a match.
	minLength: 2,
source: function (request, response) {
	   response($.map(data, function (value, key) {
	   
			//get the list of autocomplete start with search value. for case insensitive i used the toUpperCase() function.
			var first_name = value.first_name.toUpperCase()
			if(first_name.indexOf(request.term.toUpperCase()) != -1){
				label = value.first_name;
				auto_array[label] = value.id;
				return label;
			}else{
				return null;
			}
		}));

},    
	
	 select: function(event, ui) {
	 $('#link_origin_id').val(auto_array[ui.item.value]);
	  }
					
});
});
 
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

    <input type="text" id="search">
	<input type="text" id="link_origin_id">
	

答案 1 :(得分:1)

请考虑以下source的代码示例:

source: function(request, response) {
  var results;
  var aData = $.map(data, function(value, key) {
    return {
      label: value.first_name,
      value: value.id
    }
  });
  results = $.ui.autocomplete.filter(aData, request.term);
  response(results);
}

首先,我们将您的数据映射到自动完成期望的{ label, value }对。然后,我们像自动完成一样使用$.ui.autocomplete.filter()执行预期的过滤。这样便得到了结果数组,可以将其发送到response()进行显示。

工作示例:https://jsfiddle.net/Twisty/svnbw2uj/3/

希望有帮助。