Select2 limit results

I use the excellent select2 jquery plugin to select tags.

I cannot find a way to limit the results (say, only for the first 5).

My code is:

var tags = []; // this array is filled with user tags
$("#tags").select2({
    minimumInputLength: 2,
    placeholder: 'tags',
    tags: tags,
    tokenSeparators: [",", " "],
    closeOnSelect: false
});

Any ideas?

+5
source share
3 answers

see option maximumSelectionSizeif you want to limit the number of results you can select

+5
source

What about

tags = tags.slice(0, 5);

where necessary in your context.

+1
source

If you get the results through a database call, you can edit the LIMIT statement in the SQL query. For example, this will return only the first 10 results:

SELECT id,categories FROM wikitags WHERE categories LIKE :term ORDER BY 
categories ASC LIMIT 0,10 

If a remote call evaluates the solution is much more complicated.

0
source

All Articles