Reputation: 2287
I'm want to add a text property in my select2 data items and be able to search it.
For example I have this data :
var data :
[
{ id : 1, text : 'Item 1', description : 'Long text description for item 1' },
{ id : 2, text : 'Item 2', description : 'Long text description for item 2' },
{ id : 3, text : 'Item 3', description : 'Long text description for item 3' },
]
I want select2 to be able to find result if I type text that matches the description property value in the input.
Is it possible to do it ?
Thank you in advance.
Upvotes: 1
Views: 1233
Reputation: 2287
The solution is to create a custom matcher and use it like this
$('.select2-typeahead').select2
({
data :
[
{ id : 1, text : 'Item 1', description : 'Long text description for item 1' },
{ id : 2, text : 'Item 2', description : 'Long text description for item 2' },
{ id : 3, text : 'Item 3', description : 'Long text description for item 3' },
],
matcher : function(params, data, item)
{
if ($.trim(params) === '')
{
return data;
}
if(data.toLowerCase().indexOf(params.toLowerCase()) > -1 || item.description.toLowerCase().indexOf(params.toLowerCase()) > -1)
{
return data;
}
return null;
}
Upvotes: 3