Skip to content

Instantly share code, notes, and snippets.

@damianoporta
Created November 3, 2015 13:16
Show Gist options
  • Save damianoporta/053cd4358a6c3655e5cf to your computer and use it in GitHub Desktop.
Save damianoporta/053cd4358a6c3655e5cf to your computer and use it in GitHub Desktop.
$(document).ready(function () {
var selectizeOptions = {
maxItems: null,
delimiter: ',',
preload: true,
valueField: 'id',
labelField: 'tag_name',
searchField: 'tag_name',
options: [],
create: function (input) {
return {
id: '_NEW:' + input,
tag_name: input,
}
},
load: function (query, callback) {
var data = {};
data.query = query;
// Controllo se tra gli attributi c'è scope (o se non è valorizzato a 'all')
if (($(this)[0].$input.attr('scope') != 'all') && (typeof $(this)[0].$input.attr('scope') !== "undefined") && ($(this)[0].$input.attr('scope'))) {
data.scope = $(this)[0].$input.attr('scope');
}
$.ajax({
url: '/Tags/getDataAjax.json',
type: "GET",
dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
},
data: data,
error: function (e) {
callback();
},
success: function (data) {
callback(data.data);
}
});
},
render: {
option_create: function (data, escape) {
var addString = 'Aggiungi';
return '<div class="create">' + addString + ' <strong>' + escape(data.input) + '</strong>&hellip;</div>';
}
},
plugins: ['restore_on_backspace', 'remove_button']
};
var notCreate = {
render: {
option_create: function (data, escape) {
return '';
}
},
create: function (input) {
return false;
},
};
/**
* Aggiungo le opzioni specifiche al selectize
* elem: elemento a cui aggiungere il selectize
* extendedOptions: opzioni specifiche da estendere a quelle generiche
*/
function initSelectize(elem, extendedOptions) {
var specificOptions = $.extend(true, {}, selectizeOptions);
if ((typeof extendedOptions !== "undefined") && (extendedOptions)) {
specificOptions = $.extend({}, specificOptions, extendedOptions);
}
if ((typeof elem.attr('plugin') !== "undefined") && (elem.attr('plugin'))) {
specificOptions = $.extend({}, specificOptions, {
plugins: ['restore_on_backspace', 'remove_button', elem.attr('plugin')]
});
}
$(elem).selectize(specificOptions);
}
initSelectize($('.selectize[scope=1]'), notCreate);
initSelectize($('.selectize[scope=2]'), notCreate);
initSelectize($('.selectize[scope=3]'), notCreate);
initSelectize($('.selectize[scope=4]'), null);
initSelectize($('.selectize[scope=5]'), null);
initSelectize($('.selectize[scope=all]'), null);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment