简体   繁体   English

DataGrid中Button的onClick事件

[英]onClick event of Button inside DataGrid

Here's my DataGrid: 这是我的DataGrid:

// $ is a reference to `this` as it lies in an anonymous function
$.grid = new DataGrid({
    store : $.dataStore,
    query : {id : "*"},
    structure : [
        { 
            noscroll : true,
            cells : [{ name : "Recipe", field : 'name', width : '200px' }],
        },
        {
            cells : [
                [
                 { name : 'ID#', field : 'id', width : '50px'},
                 { name : 'Category', field : 'category', width : '100px'},
                 { name : 'Status', field : 'status', width : '100px'},
                 { name: "Actions", width : '200px', type: dojox.grid.cells._Widget, formatter : $._actionButtons}
                ]
            ] // end cells
        }
    ]
}, $.targetNode)
$.grid.startup();

$.grid.on("RowClick", function(e){
    console.log(this.getItem(e.rowIndex))
})

And my formatter object for the Actions cell: 我的“动作”单元格的formatter对象:

_actionButtons : function(){
    var _self = this;
    var _args = arguments;
    this.group = new Pane()

    var full = new Button({ 
        label: 'View Full',
        style : { fontSize : '80%'},
        onClick : function(){
            try {
                _self.grid.onRowClick.apply(this, arguments)
            }catch(e){}
        }
    });
    full._destroyOnRemove = true;

    var edit = new Button({
        label : 'Edit',
        style : {fontSize: '80%'}
    });
    edit._destroyOnRemove = true;

    construct.place(full.domNode, this.group.containerNode)
    construct.place(edit.domNode, this.group.containerNode)

    return this.group;
}

I'm trying to get access to the event object that would be passed by a normal onRowClick event on the DataGrid. 我正在尝试访问将由DataGrid上的常规onRowClick事件传递的事件对象。 As it sits now this kinda works, but on the on("RowClick"...) block I get multiple logs. 现在就可以了,但是在on("RowClick"...)块上,我得到了多个日志。 Without the try...catch block I get an error as the rowIndex doesn't exist in e , then 2 more logs where it does exist. 没有try...catch块,我会收到一条错误消息,因为rowIndex在e中不存在,然后再有2个日志存在。

This is the 4th or so idea I've had included pub/sub, emit(), etc. I have a feeling that the multiple logs are caused by the bubbling behavior (Button -> Row -> DataGrid or somesuch), but getting the onRowClick's event object to get passed into the Buttons created in the formatter seems impossible. 这是我包含pub / sub,emit()等的第4个左右的想法。我感觉多个日志是由冒泡行为引起的(按钮->行-> DataGrid等),但是将onRowClick的事件对象传递到格式化程序中创建的Button中似乎是不可能的。

I just want to access the rowIndex (and other DataGrid-esque properties) from the Button widget's onClick event to process according to the button pressed. 我只想从Button小部件的onClick事件访问rowIndex(以及其他DataGrid式属性),以根据按下的按钮进行处理。

Along the same lines, but here's what I came up with that seems to be working in a direction where what I'm envisioning will happen. 遵循相同的思路,但这就是我所想的,似乎正在朝着我所设想的方向发展。 Adjusted cell where the buttons will be: 调整后的单元格,其中的按钮将是:

{ name: "Actions", width : '200px', type: dojox.grid.cells._Widget, formatter : 
    function(){
        return $._actionButtons.call($, arguments);
    }
}

Adjusted onClick function in the returned Button widget: 在返回的Button小部件中调整了onClick函数:

_actionButtons : function(){
    var _index = arguments[0][1],
        _item  = this.grid.getItem(_index)
        _self  = this;

    // some code removed

    onClick : function(){
        console.log(_self.dataStore.getValue(_item, 'name'), "clicked")
    }
}

I'll probably end up extending Button to handle this a bit better, but for now, voila! 我可能最终会扩展Button以更好地处理此问题,但是现在,瞧!

Sometimes it just helps to write it down and put it out there for your brain to panic and figure out the solution before anyone else does :) 有时候,将它写下来然后放到外面让您的大脑惊慌并在其他任何人之前找出解决方案是有帮助的:)

Minor update... 小更新...

There is the formatterScope parameter for the DataGrid, but it applies to all formatter 's and would therefore mess up anything requiring cell scope and not DataGrid scope. DataGrid有formatterScope参数,但是它适用于所有formatter ,因此会弄乱需要单元格范围而不是DataGrid范围的所有内容。 The above method allows me to access everything I need. 上面的方法使我可以访问所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM