如何验证dojo OnDemandGrid

时间:2015-09-23 14:43:51

标签: javascript validation dojo dgrid

我已经构建了一个dojo OnDemandGrid,它有几个包含编辑器的列(NumberTextBox和FilteringSelect),因此我可以修改网格中的值。我还在这些列上添加了一些验证,以帮助确保输入的值是正确的。这一切似乎都工作正常,当我输入无效值时,我得到一个值,表明该值不正确。

我的问题是当我尝试将此数据发送回服务器时。当我使用ui.searchResultGrid.collection.data获取网格数据时,任何无效的字段都未在集合中更新,并且仍具有其原始值。我的猜测是,当单元格更新并进行保存时,它会进行验证,如果验证失败,则不会更新集合。如何判断某些单元格验证失败并出现问题?

以下是我如何帮助定义我的网格:

require(["dojo/_base/declare", "dgrid/OnDemandGrid", "dgrid/Keyboard", "dgrid/Selection", "dgrid/Editor", 
        "dgrid/extensions/ColumnResizer", "dgrid/extensions/DijitRegistry", "dijit/form/NumberTextBox", 
        "dijit/form/FilteringSelect", "dojo/_base/lang", "dojo/domReady!"],
    function (declare, OnDemandGrid, Keyboard, Selection, Editor, ColumnResizer, DijitRegistry, NumberTextBox, FilteringSelect, lang) {

        var CustomGrid = declare([OnDemandGrid, Keyboard, Selection, Editor, ColumnResizer, DijitRegistry]);
        ui.searchResultGrid = new CustomGrid({
            columns: {
                newItemAmount: {
                    label: "# of New Item",
                    editor: NumberTextBox,
                    editorArgs: {
                        constraints: {
                            min: 0.0001,
                            max: 999.9999,
                            allowNegative: false,
                            places: 4
                        },
                        style: "width:90px;",
                        required: true
                    },
                    autoSave: true,
                    resizable: false,
                    sortable: false,
                    width: 100
                },
                newItemUnitId: {
                    label: "Unit of New Item",
                    editor: FilteringSelect,
                    editorArgs: {
                        style: "width:130px",
                        searchAttr: "name",
                        labelAttr: "name",
                        required: true
                    },
                    autoSave: true,
                    resizable: false,
                    sortable: false,
                    width: 140
                }
            },
            selectionMode: "none",
            cellNavigation: false,
        }, "searchResultGrid");
    });

1 个答案:

答案 0 :(得分:0)

从我能够找到的内容中,您必须查看要验证的每个单元格并检查.element.widget.isValid()以查看该窗口小部件是否通过验证。我写了一个函数来验证带参数的网格: grid - 要验证的网格 cols - 要验证的列名称字符串数组

function gridValid(grid, cols) {
    for (var i = 0; i < grid.collection.data.length; i++) {
    var rowId = grid.collection.getIdentity(grid.collection.data[i])
    for (var c = 0; c < cols.length; c++) {
        if (!grid.cell(rowId, cols[c]).element.widget.isValid()) {
        return false;
        }
    };
    }
    return true;
}

我打电话给这个函数:

if (gridValid(ui.searchResultGrid, ["newItemAmount", "newItemUnitId"])) {
    var toSave = ui.searchResultGrid.collection.data;
    //TODO: save the grid data
}

这可能不是验证网格的最佳方法,但似乎有效。