Reputation: 3562
I am creating a dynamic filter list that allows a user to add multiple criteria. The list is made up of a select element and two input fields. Initially only one field should display. The second field should only display when the value BETWEEN is selected. I have seen examples of setting the visibility of an element using on select element. Here is what I have so far:
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>
Where I am stuck is getting the correct value for the current row that I am interacting with. This link http://jsfiddle.net/rlcrews/R6Kcu/ provides a working fiddle showing the rows being generated I am just stuck on how to derive the selected value from within a select row.
Upvotes: 1
Views: 612
Reputation: 1404
Is this what you're looking for? http://jsfiddle.net/pyvJW/1/.
Since you're looping through myfilters, you have access to the operator field. Since operator is observable, you just needed the parenthesis to access operatorName.
<input data-bind="value: criteria2, visible: operator().operatorName === 'BETWEEN'" />
Upvotes: 1