我正在创建一个允许用户添加多个条件的动态过滤器列表。该列表由一个选择元素和两个输入字段组成。最初应该只显示一个字段。The second field should only display when the value BETWEEN is selected. 我已经看到使用 on select 元素设置元素可见性的示例。这是我到目前为止所拥有的:
js
function FilterList(Operator, FilterCriteria, FilterCriteriaRange) {
var self = this;
self.operator = ko.observable(Operator);
self.criteria1 = ko.observable(FilterCriteria);
self.criteria2 = ko.observable(FilterCriteriaRange);
}
function AppViewModel() {
var self = this;
self.filterOperators = ko.observableArray([{
operatorName: "EQUALS", operatorValue: "=" }, {
operatorName: "GREATER THAN", operatorValue: ">"}, {
operatorName: "GREATER THAN OR EQUAL TO", operatorValue: ">="}, {
operatorName: "LESS THAN", operatorValue: "<" }, {
operatorName: "LESS THAN OR EQUAL TO", operatorValue: "<=" }, {
operatorName: "NOT EQUAL TO", operatorValue: "<>" }, {
operatorName: "NOT LESS THAN", operatorValue: "!>" }, {
operatorName: "NOT GREATER THAN", operatorValue: "!<" }, {
operatorName: "BETWEEN", operatorValue: "BETWEEN" }, {
operatorName: "LIKE", operatorValue: "LIKE"
}]);
//define filter collection
self.myfilters = ko.observableArray([]);
self.addFilter = function () {
self.myfilters.push(new FilterList(self.filterOperators[0]));
};
self.inputVisible = ko.computed(function(){
//retrieve the selected value of the current row and display the second criteria field if the selected value is BETWEEN
//return self.operator();
});
}
ko.applyBindings(new AppViewModel());
html
<input type="button" value="Add Filter" title="Add Group" data-bind="click: $root.addFilter" />
<table>
<tbody data-bind="foreach: myfilters">
<tr>
<td>
<select data-bind="options: $root.filterOperators, value:operator, optionsText:'operatorName'"></select>
</td>
<td>
<input data-bind="value: criteria1" />
</td>
<td>
<input data-bind="value: criteria2, visible: inputVisible() === 'BETWEEN'" />
</td>
</tr>
</tbody>
</table>
我被卡住的地方是为我正在与之交互的当前行获取正确的值。此链接http://jsfiddle.net/rlcrews/R6Kcu/提供了一个显示正在生成的行的工作小提琴我只是坚持如何从选择行中派生所选值。