javascript - 如何创建一个 dojo 数据网格,其中一列是标题行中的按钮?

标签 javascript grid dojo dojox.grid.datagrid

我有一个 dojo(v1.6) 数据网格,它有复选框来选择行作为最左边的列。我需要将这些复选框的标题列作为删除按钮而不是全选复选框。单击删除按钮时,所有选定的行都会被删除。

请找到Data grid Demo .

我不知道如何将复选框的标题列作为按钮。请帮助我自定义小部件。 这是网格 js 代码

dojo.ready(function(){
    /*set up data store*/
    var data = {
      identifier: 'id',
      items: []
    };
    var data_list = [
      { col1: "normal", col2: false, col3: 'But are not followed by two hexadecimal', col4: 29.91},
      { col1: "important", col2: false, col3: 'Because a % sign always indicates', col4: 9.33},
      { col1: "important", col2: false, col3: 'Signs can be selectively', col4: 19.34}
    ];
    var rows = 10;
    for(var i=0, l=data_list.length; i<rows; i++){
      data.items.push(dojo.mixin({ id: i+1 }, data_list[i%l]));
    }
    var store = new dojo.data.ItemFileWriteStore({data: data});

    /*set up layout*/
    var layout = [
        { type: "dojox.grid._CheckBoxSelector" },
        [
          {'name': 'Column 1', 'field': 'id', 'width': '20%'},
          {'name': 'Column 2', 'field': 'col2', 'width': '20%'},
          {'name': 'Column 3', 'field': 'col3', 'width': '25%'},
          {'name': 'Column 4', 'field': 'col4', 'width': '20%'}
        ]
    ];
    /*create a new grid:*/
    var grid = new dojox.grid.DataGrid({
        id: 'grid',
        store: store,
        structure: layout,
        cellOverClass: 'cellover'
      },
      document.createElement('div')
    );
    /*append the new grid to the div*/
    dojo.byId("gridDiv").appendChild(grid.domNode);
    /*Call startup() to render the grid*/
    grid.startup();
});

HTML

<div id="gridDiv"></div>

最佳答案

您可以从现有 CheckBoxSelector 实现自己的 CheckBoxSelector。我通过查看 _Selector.js 源代码发现了这些方法。要覆盖的有趣方法是 generateHtmldoclick

参见 updated fiddle .

    dojo.declare('my.grid._CheckBoxSelector', [dojox.grid._CheckBoxSelector], {
    _headerBuilderClass: dojo.extend(function (view) {
        dojox.grid._HeaderBuilder.call(this, view);
    }, dojox.grid._HeaderBuilder.prototype, {
        generateHtml: function () {
            var w = this.view.contentWidth || 0;
            return '<table style="width:' + w + 'px;" ' +
                'border="0" cellspacing="0" cellpadding="0" ' +
                'role="presentation"><tr><th style="text-align: center;">' +
                '<button data-dojo-type="dijit.form.Button" type="button">x</button><div class="dojoxGridCheckSelector dijitReset dijitInline dijitCheckBox" style="display:none"></div></th></tr></table>';
        },
        doclick: function (e) {
            this.view.grid.removeSelectedRows();
            return true;
        }
    })
});

/*set up layout*/
    var layout = [{
        type: "my.grid._CheckBoxSelector"
    },

    ...

    }]];

删除行

    this.view.grid.removeSelectedRows();

您可以在启动后解析网格以创建 dijit 小部件。

grid.startup();
// Not the best practice here but it should give you a starting point
// on where to find the header node.
dojo.parser.parse(grid.views.views[0].headerContentNode);

关于javascript - 如何创建一个 dojo 数据网格,其中一列是标题行中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918759/

相关文章:

javascript - 找不到分组功能标题的样式

javascript - 确认使用工厂是创建通用多用途点击计数监听器的最佳(唯一?)方法

jquery - Kendo网格detailInit访问父网格

html - Safari 浏览器问题 : Fixed footer is hiding the bottom part of my content

android - 可以使用 ListView 进行内联编辑吗?

javascript - 使用socket.io与dojo MultipleDefine错误

javascript - 鼠标悬停时滑动生效

javascript - 如何将 Dragula 与 Googlepolymer 结合使用

javascript - 观察或通知属性变化的推荐方式?道场MVC

javascript - 将 dojo.declare 更改为 "native Javascript class decleration"