Created
February 15, 2019 09:32
-
-
Save lucawen/095cdc9012eaa95a3f60c2554a79e508 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Select2Item = (function () { | |
function formatRepoSelection(item) { | |
return item.text; | |
} | |
function formatRepo(item) { | |
return item.text; | |
} | |
function processData(data) { | |
return { | |
results: data | |
}; | |
} | |
function select2item(classe, url, options) { | |
options = options || {}; | |
var length_search = options.length_search || 3; | |
var extra_data = options.extra_data || null; | |
$(classe).select2({ | |
language: 'pt-BR', | |
width: '100%', | |
ajax: { | |
url: url, | |
dataType: 'json', | |
data: function (params) { | |
var data = { search: params.term }; | |
if (extra_data) { | |
return merge_options(data, extra_data); | |
} else { | |
return data; | |
} | |
}, | |
processResults: processData | |
}, | |
minimumInputLength: length_search, | |
templateResult: formatRepo, | |
templateSelection: formatRepoSelection, | |
}); | |
} | |
function merge_options(obj1, obj2) { | |
var obj3 = {}; | |
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } | |
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } | |
return obj3; | |
} | |
return { | |
set: select2item | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment