jQuery select2 query: preload_data variable array
I'd like to fill a select2 with values of an array with the 'query'
option. The thing is, the array can be one out of four arrays. The
declaration which array shall be loaded is depending on the class name of
the select2 div container's parent div.
var preload_data_editors = [
{ id: '1', text: '1'}
];
var preload_data_techniker = [
{ id: '2', text: '2'}
];
var preload_data_cvd = [
{ id: '3', text: '3'}
];
var preload_data_pgmrat = [
{ id: '4', text: '4'}
];
$('div.multisel div').select2({
multiple: true,
query: function (query) {
var data = {results: []};
switch ($(this).parent('div').attr('class')) {
case 'multisel editors':
preload = preload_data_editors;
break;
case 'multisel techniker':
preload = preload_data_techniker;
break;
case 'multisel cvd':
preload = preload_data_cvd;
break;
case 'multisel pgmrat':
preload = preload_data_pgmrat;
break;
}
$.each(preload, function() {
// if (query.term.length == 0 ||
this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0
){
data.results.push({id: this.id, text: this.text });
// }
});
query.callback(data);
}
})
My problem: $(this) in switch is not set. How can I get $(this) to be set
as it is intended? Maybe I need to say I'm not really good with jQuery at
this point.
Maybe you can help me fixing this issue.
Thanks in advance!
No comments:
Post a Comment