[英]Applying jquery plugin (selectric) to ko-bound dropdown
I'm building a simple query interface for a table of data. 我正在为数据表构建一个简单的查询接口。
I have two dropdowns - one to select the field from the table, and one to select the query to perform on that field. 我有两个下拉菜单-一个用于从表中选择字段,另一个用于选择要对该字段执行的查询。 The query dropdown items depend on the data type, so I have to wait until the field is selected to populate it.
查询下拉菜单项取决于数据类型,因此我必须等待直到选择该字段来填充它。
All the bindings work if I use plain old select
elements. 如果使用普通的旧
select
元素,则所有绑定均有效。 But I want to apply the selectric plugin to them. 但是我想将selectric插件应用于它们。 Problem is, after calling $(element).selectric() on the elements, I don't know how to get it to "refresh" the items - so only the first dropdown contains the bound elements because it's initially populated.
问题是,在元素上调用$(element).selectric()之后,我不知道如何使它“刷新”项目-因此只有第一个下拉列表包含绑定的元素,因为它最初是填充的。 The second one never seems to get the updated 'query' elements.
第二个似乎从未获得更新的“查询”元素。
I've tried using a custom ko binding, and calling .selectric() on the update like so: 我试过使用自定义的ko绑定,并像这样在更新中调用.selectric():
ko.bindingHandlers.selectric = {
init: function(element, valueAccessor)
{
$(element).selectric();
},
update: function(element, valueAccessor)
{
$(element).selectric();
}
};
Here's my bindings for the two drop downs: 这是我对两个下拉菜单的绑定:
<select data-bind="options: $parent.fields,
optionsCaption: 'Select field...',
value: field_name,
event: { change: fieldSelected },
selectric: {}"></select>
<select data-bind="options: queries,
optionsCaption: 'Select query...',
selectric: queries"></select>
Here's the fiddle w/ viewmodel, etc. http://jsfiddle.net/rUNJY/12/ . 这是带有视图模型的小提琴,等等。http://jsfiddle.net/rUNJY/12/ 。 If you disable the selectric binding, it will work... how can I get the plugin to re-create the dropdown with updated items?
如果您禁用selectric绑定,它将起作用...我如何才能使插件重新创建包含更新项的下拉菜单?
There are many ways to do this. 有很多方法可以做到这一点。 Here are two:
这是两个:
1) Listen to the observable specified in your binding. 1)聆听您的绑定中指定的可观察对象。 This same as your way.
这和你的方法一样。
Usage: 用法:
<select data-bind="options: queries,
optionsCaption: 'Select query...',
selectric: queries"></select>
Code: 码:
ko.bindingHandlers.selectric = {
update: function(element, valueAccessor)
{
ko.unwrap(valueAccessor()); //must use value in order for update to be called
$(element).selectric('refresh'); //must specify that plugin should refresh. See selectric documentation
}
};
Sample: http://jsfiddle.net/p4X4j/ 范例: http : //jsfiddle.net/p4X4j/
2) Use the observable specified in the options binding. 2)使用选项绑定中指定的可观察值。 I prefer this solution since I don't need to specify the same observable in two bindings.
我更喜欢这种解决方案,因为我不需要在两个绑定中指定相同的可观察值。
Usage: 用法:
<select data-bind="options: queries,
optionsCaption: 'Select query...',
selectric: {}"></select>
Code: 码:
ko.bindingHandlers.selectric = {
init: function (element, valueAccessor, allBindingsAccessor) {
$(element).selectric('');
if(allBindingsAccessor().options.subscribe) {
var optionsSubscription = allBindingsAccessor().options.subscribe(function (newValue) {
$(element).selectric('refresh');
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
optionsSubscription.dispose();
});
}
}
};
Sample: http://jsfiddle.net/LbRGz/1/ 样本: http : //jsfiddle.net/LbRGz/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.