如何验证dojo OnDemandGrid中的编辑器字段

vbopmzt1  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(150)

我已经构建了一个dojo OnDemandGrid,其中有几列包含编辑器(NumberTextBox和FilteringSelect),因此我可以修改网格中的值。我还在这些列上添加了一些验证,以帮助确保输入的值是正确的。这些都看起来运行良好,当我输入一个无效值时,我会得到一个值不正确的指示。
我的问题是当我尝试将此数据发送回服务器时。当我通过www.example.com获取网格数据时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");
    });
w8f9ii69

w8f9ii691#

根据我找到的结果,您必须查看每个要验证的单元格,并检查.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
}

这可能不是验证网格的最佳方法,但它似乎正在起作用。

相关问题