如何在Dgrid选择mixin中获取所选行的列表

时间:2013-09-07 11:13:57

标签: dojo grid dgrid

我在Dgrid ondemandgrid中使用Selector和Selection Mixin。我使用复选框作为选择器。我的问题是。

  1. 如何在html按钮上点击javascript中的Checked行列表?我知道有一个dgrid-select和deselect事件,但我想要按钮点击事件中所有选定行的列表。
  2. 目前,如果我点击任意位置的一行,该复选框将被选中。但我只想在我点击复选框时选择该行。如何实现这一目标?
  3. 这是我的代码

                 require([
            "dgrid/OnDemandGrid",
            "dojo/store/JsonRest",
            "dojo/dom",
            "dojo/dom-style",
            "dojo/_base/declare",
            "dgrid/extensions/ColumnResizer",
            "dgrid/Selection", 
            "dgrid/selector"
        ], function (OnDemandGrid,JsonRest,dom,domStyle,declare,ColumnResizer,Selection, selector) {
                var Layout = [
                     selector({ label: selector({}), selectorType: "checkbox" }),
                     {field: 'srno',label: 'Sr No'},
                     {field: "Name",label: "name"}
                ];
                jsonstore = new JsonRest({target: url,idProperty: "srno"});
                grid = new(declare([OnDemandGrid,ColumnResizer,Selection]))({
                    store: jsonstore,
                    columns: Layout,
                    minRowsPerPage : 40,
                    maxRowsPerPage : 40,
                    keepScrollPosition : true,
                    allowSelectAll: true,
                    loadingMessage: "Loading data...",
                    noDataMessage: "No results found."
                }, "grid");
                domStyle.set(dom.byId("grid"),"height","210px");                
                grid.startup();
                grid.on("dgrid-select", function(event){
                        //
                });
                grid.on("dgrid-deselect", function(event){
                        //
                });
    
            });
    

4 个答案:

答案 0 :(得分:3)

以下是您的问题的解决方案:

    var Layout = [
             selector({ label: '', sortable: false}),
             {field: 'srno',label: 'Sr No'},
             {field: "Name",label: "name"}
        ];
        jsonstore = new JsonRest({target: url,idProperty: "srno"});
        grid = new(declare([OnDemandGrid,ColumnResizer,Selection]))({
            store: jsonstore,
            columns: Layout,
            minRowsPerPage : 40,
            maxRowsPerPage : 40,
            selectionMode: "none",
            deselectOnRefresh: false,
            keepScrollPosition : true,
            allowSelectAll: true,
            loadingMessage: "Loading data...",
            noDataMessage: "No results found."
        }, "grid");



    new Button({
        label: "Ok",
        onClick: function () {

             // here you can use grid.selection to get the list of selected rows.
             // it is an object with { 'rowid': true} format  for example, like below 
            array.forEach(grid.store.data, function (item) {
                if (grid.selection[item.id]) {
                    //your code to handle this selected item
                }
            });
        })
    }, 'button');

答案 1 :(得分:1)

For 1:声明var selection = []; 那么,

   grid.on("dgrid-select", function(event){
            var selected = grid.selection;
        if (selected) {
            for (row in selected) {
                selection.push(row);
            }
        }
    });

并将其拼接在dgrid-deselect上 然后点击按钮访问数组。

For 2:使用selectionMode: "none",定义网格 来自文档:https://github.com/SitePen/dgrid/wiki/Selection

答案 2 :(得分:1)

this.grid.on(".dgrid-row:dblclick", function(vet) {
    // your selected row
    var row = self.grid.row(vet);
});

答案 3 :(得分:1)

您可以在此处理程序中获取选定的行。 selectedRows 变量为您提供网格中的所有选定项目

   window.grid = new (declare([Grid, ColumnResizer, Selection]))({
                store: jsonstore,
                columns: Layout,
                minRowsPerPage: 40,
                maxRowsPerPage: 40,
                keepScrollPosition: true,
                allowSelectAll: true,
                loadingMessage: "Loading data...",
                noDataMessage: "No results found."
            }, "grid");

            window.grid.on("dgrid-select", function (event) {


                var selectedRows = event.rows;

            });