Duplicate Results Filter Issue Solved #4

chetanvarshney opened this Issue Mar 18, 2013 · 4 comments

3 participants


Hi Konstantin,

I have solved duplicate results issue.

Here is what I have changed. Please let me know if you like it

in chosen.ajaxaddition.jquery.js
added following lines on line no 196 (just before //show loading)

var selIds =$('option:selected', select).map(function(){ return this.value }).get().join(",");
if(selIds) {
ajaxOptions.data.data['selIds']= selIds;

in search.php
added following lines on line no 61 (just after $results=array())
if(isset($_POST['data']['selIds'])) {
$selIds = explode(',', $_POST['data']['selIds']);

and changed if condition to
if( stripos($state,$q) === 0 && !in_array($i, $selIds))


I think this an OK way of solving it - giving the user the option to solve it on server in their query.

A few quick thoughts about that approach.. the position of that calculation should really be moved down to prevent extra work from being done. Think about this scenario..
1. The user starts typing
2. keyup callback fires
3. It begins processing the map for selIds
4. The user types some more before the ajax call gets fired
5. keyup callback fires
6. It again begins processing the map for selIds
7. Then potentially the ajax call will fire

So I recommend moving that calculation down to within the timeout function. For instance on line 202. That way, only when we are sure we are ready to make the request to the server we perform that calculation. I would also only send up the options if the box they are typing in is a multi-select. We save some cpu cycles + network IO on single select options - where it's not needed.

Another small point about this approach in general. As the user selects more and more options the calls to the server will become larger and larger. Potentially not a huge issue, but something to think about as this may not scale well in extreme cases.

If you have time please make a pull request and we can work through this together.

PS here's a bit easier way of doing that map:


Hello ksykulev,
I am having problems with the special characters like "()" and "-" . When I hit the ajax call, I get all the records in the json array, but they didnt show in the search box options as my results contain special characters. Could you please help me solve my problem.

Thanks in advance..!!


@sidhidhall - Can you do me a favor and test to make sure this behaves properly on the original chosen without ajax?


Closing (e5ddd9f)

@ksykulev ksykulev closed this Mar 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment