Skip to content

Instantly share code, notes, and snippets.

@dfch
Created September 13, 2014 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfch/f2d1df81136f1dff6b10 to your computer and use it in GitHub Desktop.
Save dfch/f2d1df81136f1dff6b10 to your computer and use it in GitHub Desktop.
[NoBrainer] Using JayData with jQuery select2 plugin in LightSwitch HTML
function searchData(svcUrl, data, searchTerm) {
var context = new LightSwitchApplication.ApplicationData({
name: 'oData',
oDataServiceHost: svcUrl
});
return context
.EntitySet
.filter(function (entity) {
return entity.Name.startsWith(this.q);
}, { q: searchTerm })
.forEach(function (entity) {
data.results.push({ id: entity.Id, text: entity.Name });
});
}
function _addSelect2Control(svcUrl, $parentElement, $element, contentItem, options) {
try {
var items = { results : [] };
$element
.select2({
placeholder: options["placeholder"],
quietMillis: options["quietMillis"],
minimumInputLength: options["minimumInputLength"],
page_limit: options["page_limit"],
width: '90%',
query: function (query) {
items.length = 0;
var promise = searchData(svcUrl, items, query.term)
.then(function () {
query.callback(items);
});
},
dropdownCssClass: options["dropdownCssClass"]
})
.on("change", function (e) {
contentItem.value = getSelectedValues($parentElement);
});
} catch (e) {
console.log(e.message + " description: " + e.description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment