Ich habe gefunden, schön jQuery plugin und es verwendet kessel platte vorlage. Alles funktioniert gut, aber ich kann keine interne Funktion aufrufen, um ausgewählte Objekte zu erhalten. Die Konstruktion dieses Plugin ist unter:Funktion aufrufen von jquery plugin kessel platte code
(function ($, window, document) {
'use strict';
// constructor
var SearchableOptionList = function ($element, options) {
this.$originalElement = $element;
this.options = options;
this.metadata = this.$originalElement.data('sol-options');
};
// plugin prototype
SearchableOptionList.prototype = {
DATA_KEY: 'sol-element',
// default option values
defaults: {
...
},
// initialize the plugin
init: function() {
this.config = $.extend(true, {}, this.defaults, this.options, this.metadata);
...
return this;
},
//some functions
...
selectAll: function() {
...
},
deselectAll: function() {
...
},
getSelection: function() {
return this.$selection.find('input:checked');
}
};
// jquery plugin boiler plate code
SearchableOptionList.defaults = SearchableOptionList.prototype.defaults;
window.SearchableOptionList = SearchableOptionList;
$.fn.searchableOptionList = function (options) {
var result = [];
this.each(function() {
var $this = $(this),
$alreadyInitializedSol = $this.data(SearchableOptionList.prototype.DATA_KEY);
if ($alreadyInitializedSol) {
result.push($alreadyInitializedSol);
} else {
var newSol = new SearchableOptionList($this, options);
result.push(newSol);
setTimeout(function() {
newSol.init();
}, 0);
}
});
if (result.length === 1) {
return result[0];
}
return result;
};
}(jQuery, window, document));
Voll Code, den Sie auf GitHub finden.
Ich versuche getSelection
Funktion wie unten aufzurufen:
// initialize sol
var s = $('#my-select').searchableOptionList({
maxHeight: '150px',
showSelectAll: true
});
s.selectAll();
ich einen Fehler:
TypeError: this.config is undefined
Ist es möglich, Funktionen mit dieser Vorlage der Kesselplatte aufzurufen?
Sie auf jsfiddle
Dank! Können Sie erklären, warum der Autor 'setTimeout' verwendet? Ist es nötig? – Nolesh
Ich vermute es liegt daran, dass das Plugin Remote-Daten ermöglicht, so dass es alle Daten asynchron behandelt. –
Wenn ich keine asynchronen Daten verwenden möchte, kann ich das weglassen? – Nolesh