淘汰验证 - 高级搜索用户界面

时间:2013-02-10 12:51:29

标签: asp.net-mvc knockout.js knockout-validation

我正在构建类似于TFS查询构建器Web界面的高级搜索UI。使用knockout进行客户端实现并使所有内容都或多或少地工作,除了最终验证以使某些必需项目基本上被选中。如果我选择一个项目然后取消选择该项目,它就会给我一个验证错误。哪个没问题,但我希望在点击搜索按钮时验证表格。

我很确定我需要使用ko.validatedobservable方法,我只是不确定如何。无论如何,我有一个小提琴:http://jsfiddle.net/sstolp/uXBSA/如果有人有时间或倾向帮助我。我会非常感激。

感谢您的时间。

scvm.SearchLine = function () {
var self = this;
self.selectedField = ko.observable().extend({ required: true });
self.selectedOperator = ko.observable().extend({ required: true });
self.firstdate = ko.observable(new Date());
self.lastdate = ko.observable(new Date());
self.thedate = ko.observable(new Date());

return self;};

scvm.Criteria = function () {
var self = this,
    lines = ko.observableArray([]),

    // Put one line in by default
    loadInitialData = function () {
        lines.push(new scvm.SearchLine());
    },

    rowcount = ko.computed(function () {
        return lines().length;
    }),

    // Operations
    addLine = function () {
        lines.push(new scvm.SearchLine());
    },

    removeLine = function (line) {
        lines.remove(line);
    },

    search = function () {
        var data = $.map(lines(), function (line) {
            return line.selectedField() ? {
                selectedField: line.selectedField().searchfield,
                selectedOperator: line.selectedOperator().name,
            } : undefined
        });
        alert("Send to server: " + JSON.stringify(data));            
    },

    clear = function () {
        lines.removeAll();
    };

return {
    lines: lines,
    loadInitialData: loadInitialData,
    rowcount: rowcount,
    addLine: addLine,
    removeLine: removeLine,
    search: search,
    clear: clear
};
}();

1 个答案:

答案 0 :(得分:0)

是的,您的所有SearchLine对象都必须包装到ko.validatedObservable中。您还应该实现计算属性,该属性将检查每个条件行的isValid()并返回全局有效性标记。

scvm.SearchLine = function () {
    var self = this;
    self.selectedField = ko.observable().extend({ required: true });
    self.selectedOperator = ko.observable().extend({ required: true });
    self.firstdate = ko.observable(new Date());
    self.lastdate = ko.observable(new Date());
    self.thedate = ko.observable(new Date());

    return ko.validatedObservable(self);
};

scvm.Criteria = function () {

    // ...

    return {
        lines: lines,
        loadInitialData: loadInitialData,
        rowcount: rowcount,
        addLine: addLine,
        removeLine: removeLine,
        search: search,
        clear: clear,
        // new property that indicates validity of all lines
        linesValid: ko.computed(function(){
            var items = lines();
            for (var i = 0, l = items.length; i < l; i++)
                if (!items[i].isValid()) return false;
            return true;
        })
    };
}();

此新属性可用于enable绑定“搜索”按钮:

<input type="button"
       data-bind="enable: linesValid, click: search"
       title="Clicking this button will run a search."
       value="Search" />

我修改了你的小提琴。看看:http://jsfiddle.net/ostgals/uXBSA/8/


更新

我们也应该稍微修改Criteria.search方法,因为我们的行数组包含observables而不是对象:

        //...

        search = function () {
            var data = $.map(lines(), function (line) {
                line = ko.utils.unwrapObservable(line);
                return line.selectedField() ? {
                    selectedField: line.selectedField().searchfield,
                    selectedOperator: line.selectedOperator().name,
                } : undefined
            });
            alert("Send to server: " + JSON.stringify(data));            
        },

        //...