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

时间:2014-01-04 08:35:28

标签: 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>

1 个答案:

答案 0 :(得分:3)

您可以从现有的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);