TL獲得更多的數據; DRSelectize.js在「onItemAdd」回調
使其更清晰,我希望有機會在onItemAdd
函數內選擇JSON對象的所有數據。現在我只能通過配置變量訪問_id
和name
。
好吧,所以我有一個工作Selectize.js函數從我的服務器抓取JSON對象並創建選擇選項。
我想知道的是,我可以從「onItemSelect」回調中的現有JSON對象獲得更多數據嗎?
我可以直接得到的唯一數據是value
作爲在配置,在這種情況下是「_id」和我假設是形成在配置在labelField
,在這種情況下是name
形式$item
指定JSON數據。
如何獲得比選定項目更多的數據?您在呈現對象中看到我使用變量item.sku
,如何在「onItemAdd」回調中訪問sku
變量?
數據形成的服務器是一個JSON陣列:
[
{ _id: 'abcd1234', name: 'Sample', sku: '00123' },
{ _id: 'efgh5678', name: 'Sample2', sku: '00124' }
]
我的功能
// setup select box to add new products to list
$('#buyingGroupAddProducts').selectize({
valueField: '_id',
labelField: 'name',
searchField: 'name',
options: [],
create: false,
closeAfterSelect: true,
render: {
option: function(item, escape) {
return '<div>'+ escape(item.sku) + ': ' + escape(item.name) + '</div>';
}
},
load: function(query, callback) {
if (!query.length) return callback();
$.ajax({
url: '/buying/products-search',
type: 'GET',
dataType: 'json',
data: {
q: query
},
error: function() {
callback();
},
success: function(res) {
callback(res);
}
});
},
onItemAdd: function(value, $item) {
// DO the thing
console.log($item);
}
});
SelectizeJS網站:http://selectize.github.io/selectize.js/
你有沒有這樣做?我也在尋找答案。 – neobie
@neobie我做過了,但沒有選擇Selectize。我不記得我使用過這個項目,我從頭開始構建自己的插件,另一個我使用http://www.runningcoder.org/jquerytypeahead/ – Nicekiwi