【问题标题】:What could be the reason that dgrid-select Selection is not working for me?dgrid-select Selection 对我不起作用的原因可能是什么?
【发布时间】:2017-02-21 15:31:41
【问题描述】:

我有这段代码:

define([
 ...
"MyGrid/lib/dgrid/Grid",
"MyGrid/lib/dgrid/Keyboard",
"MyGrid/lib/dgrid/Selection",
"MyGrid/lib/dgrid/extensions/Pagination",
"MyGrid/lib/dgrid/extensions/ColumnHider",
"MyGrid/lib/dgrid/extensions/ColumnResizer",
...
], function (declare, ..., dgrid, dgridKeyboard, dgridSelection,
             dgridPagination, dgridColumnHider, dgridColumnResizer, 
             Memory, widgetTemplate) {
        ...
        // creates the grid
        _createGrid: function (columns, collection) {
            this._grid = new CustomGrid({
                className: "dgrid-autoheight",
                columns: columns,
                collection: collection,
                firstLastArrows: true,
                loadingMessage: "Loading...",
                noDataMessage: "No results.",
                pagingLinks: 2,
                pageSizeOptions: [10, 15, 25],
                pagingTextBox: false,
                selectionMode: "toggle",
                allowSelectAll: true
            }, this.gridNode);
            this._grid.startup();
        },
        ...
         // Attach events to HTML dom elements
        _setupEvents: function () {
            logger.debug(this.id + "._setupEvents");
            ...

            this._selectEventListener = this._grid.on("dgrid-select", function (event) {
                var rows, id;

            console.log(event.grid.id + ": selected " + dojoArray.map(event.rows, function (row) {
                return row.id;
            }).join(", "));
            console.log("selection: " + JSON.stringify(event.grid.selection, null, "  "));

            // Get the rows that were just selected
            rows = event.rows;

            console.log("selectionMode: " + event.grid.selectionMode);

            console.log("Row entries: " + Object.entries(rows));
            console.log("Row[0] entries: " + Object.entries(rows[0].element));
            console.log("Selection: " +  Object.entries(event.grid.selection));

                // Iterate through all currently-selected items
                for (id in event.grid.selection) {
                    if (event.grid.selection[id]) {
                        console.log("keys: " + Object.entries(event.grid.selection[id]));
                    }
                }
            });
        },
        ...
        // rerender the grid. 
        _renderGrid: function (objs) {
            var objAttributes, gridColumns;

            if (objs.length) {
                objAttributes = objs[0].getAttributes();

                if (this._grid) {
                    this._selectEventListener.remove();
                    this._errorEventListener.remove();
                    this._grid.destroy();
                    dojoConstruct.place(this.gridNode, this.domNode);
                }

                // Create header and content data.
                gridColumns = this._createColumns(objAttributes);
                this._gridStore = new Memory({
                    data: this._createData(objs, objAttributes, gridColumns)
                });
                this._createGrid(gridColumns, this._gridStore);
                this._setupEvents();
            }
        },

        // create grid HeaderData from retrieved objects.
        _createColumns: function (objAttributes) {
            var fieldname, columns, i;
            columns = [
                {field: "id", label: "ID"}
            ];

            if (objAttributes) {
                for (i in objAttributes) {
                    fieldname = objAttributes[i].toLowerCase().replace(".", "-");
                    columns.push({field: fieldname, label: objAttributes[i]});
                }
            }

            return columns;
        },

然而,选择确实参差不齐。 它仅适用于双击。以下是点击选择的结果:

dgrid_0: selected  
selection: {
  "undefined": true
}  
selectionMode: toggle  
Row entries: 0,[object Object]  
Row[0] entries: rowIndex,0  
Selection: undefined,true  
keys: 

如您所见,它仅通过事件行获取选择,而不是通过询问 grid.selection。 多选也不会被选中。

为什么没有注册?

【问题讨论】:

  • 我怀疑这里的一个原因可能是集合中没有idProperty。如果要将集合对象的 id 映射到它,则应该有 id 属性。如果要将其映射到不同的属性,则在创建集合时定义 idProperty: otherProperty
  • 你是救生员。非常感谢你。把你的评论作为答案写下来,我会接受的。这就是答案:this._gridStore = new Memory({ data: this._createData(objs, objAttributes, gridColumns), idProperty: "id"});
  • 完成!很高兴我能帮上忙。 :)

标签: dojo selection dgrid


【解决方案1】:

我怀疑这里的一个原因可能是集合中没有idProperty。如果要将集合对象的 id 映射到它,则应该有 id 属性。如果您想将其映射到不同的属性,请在创建集合时定义idProperty: otherProperty

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2019-09-29
    • 2011-12-22
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多