Click here to Skip to main content
15,914,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is given below


.autocomplete({
minLength: 0,
source: function (request, response) {
response($.ui.autocomplete.filter(
data, extractLast(request.term)));
},

In the above code,get all the autofill data 'data',and extract the item from the data.
How to hide the already selected item.

Please help me.

What I have tried:

Hide already selected items jQuery Autocomplete
Posted
Updated 7-Mar-17 5:00am

1 solution

Actually, you can make the code to filter the list on server side before sending it to the client

Here is an example:
On the View page, overwrite the source by passing in additional parameter
JavaScript
function split(val) {
                  return val.split(/,\s*/);
              }
 function extractLast(term) {
                return split(term).pop();
            }

.autocomplete({
...
   source: function (request, response) {
                    $.getJSON(
                        'YourController/Action',
                        {
                            term: extractLast(request.term),
                            sValue: $('#textBoxId').val()
                        },
                        response);
                },
...
});


On the server side, add additional logic to filter out the selected value from the source
C#
public JsonResult AutocompleteFilter(string term, string sValue)
{
 string[] selectedValue = 
		sValue.Split(',').Select(s => s.ToLower().Trim()).ToArray();
				
 var result = from o in yourSource
              where !selectedValue.Contains(o.WhatEverField.ToLower())
			&&  ...
              select o;
...
}


Resources:
jQuery UI autocomplete filter data - Stack Overflow[^]

Autocomplete: Refactored code for array filtering into $.ui.autocompl… · jquery/jquery-ui@dbc9add · GitHub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900