【问题标题】:Using Dojo dgrid with selector column plugin without a store?在没有商店的情况下使用带有选择器列插件的 Dojo dgrid?
【发布时间】:2012-05-16 07:47:18
【问题描述】:

是否可以使用由数组(而不是数据存储区)支持的 Dojo dgrid 小部件和复选框来选择行?

我成功地从一个基本的数组支持的 dgrid 开始,然后添加了 Selection mixin 以启用行选择。所以现在我有一个由数组支持并允许行选择的 dgrid。但是,当我尝试通过 selector column plugin 添加复选框时,出现错误:this.store is undefined.

我确定 this.store 用于识别选择了哪些行:对 this.store.getIdentity(rowObject) 方法的多次调用与查询网格选择时返回的结果直接相关。

当使用对象数组而不是存储时,是否可以指定某个列字段来标识选定的行?我下面代码中的 WORKAROUND_STORE 有效地做到了这一点,但也许我错过了一个简单的解决方案,比如设置一些属性,例如:selector({idProperty: 'col1'}). 看起来应该更容易做到。

require(["dgrid/Grid",
         "dgrid/selector",
         "dgrid/Selection",
         "dojo/_base/declare"],
    function(Grid, selector, Selection, declare){

        var columns = {
            col1: selector({}),
            col2: {label: 'COL2'},
            col3: {label: 'COL3'},
        };

        var data = [
            { col1: '1', col2: 'A', col3: 'I'},
            { col1: '2', col2: 'B', col3: 'II'},
            { col1: '3', col2: 'C', col3: 'III'}
        ];

        WORKAROUND_STORE = {
            getIdentity: function(item) { return item.col1 }
        }

        var SelectionGrid = declare([Grid, Selection]);
        window.methodGrid = new SelectionGrid({
            store: WORKAROUND_STORE,
            columns: columns,
            selectionMode: "none",
            allowSelectAll: true
        }, "methodGridDiv");
        window.methodGrid.renderArray(data);
    }
);

【问题讨论】:

    标签: javascript dojo widget dgrid


    【解决方案1】:

    这就是我最终要做的事情:我将我的数据数组包装在一个 Dojo 内存存储中,该存储有一个 setData() 方法来接受一个新的数据数组。主要变化:

    • 使用 OnDemandGrid 代替 Grid
    • 将数据数组包装在 Dojo 内存存储对象中
    • 更新网格的存储以更新数据数组
    • 调用网格的刷新方法()

    完整代码:

    require(["dgrid/OnDemandGrid",
             "dgrid/selector",
             "dgrid/Selection",
             "dojo/_base/declare",
             "dojo/store/Memory"],
        function(OnDemandGrid, selector, Selection, declare, Memory){
    
            var columns = {
                col1: selector({}),
                col2: {label: 'COL2'},
                col3: {label: 'COL3'},
            };
    
            var data = [
                { col1: '1', col2: 'A', col3: 'I'},
                { col1: '2', col2: 'B', col3: 'II'},
                { col1: '3', col2: 'C', col3: 'III'}
            ];
    
            var SelectionGrid = declare([OnDemandGrid, Selection]);
            window.methodGrid = new SelectionGrid({
                store: Memory({idProperty: 'methodId'}),
                columns: columns,
                selectionMode: "none",
                allowSelectAll: true
            }, "methodGridDiv");
            window.methodGrid.store.setData(data);
            window.methodGrid.refresh();
        }
    );
    

    更新以回应:

    你真的在那里得到了复选框吗?我没有看到你的 代码 sn-ps。

    添加复选框的代码是:col1: selector({})。或者更明确地说:col1: selector({selectorType: 'checkbox'})

    请注意,选择器插件是另一个 column plugin,其工作方式与编辑器插件不同。

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多