Javascript DataGrid内按钮的onClick事件
这是我的数据网格:Javascript DataGrid内按钮的onClick事件,javascript,dojo,dojox.grid.datagrid,Javascript,Dojo,Dojox.grid.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", fie
// $ 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))
})
以及Actions单元格的myformatter
对象:
_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;
}
我试图访问DataGrid上正常的onRowClick
事件传递的事件对象。现在它可以工作了,但是在on(“RowClick”…)
块上,我得到了多个日志。如果没有try…catch
块,我会得到一个错误,因为行索引在e
中不存在,然后在它存在的地方再出现两个日志
这是我包含的第四个想法,包括pub/sub、emit()等。我感觉多个日志是由冒泡行为(Button->Row->DataGrid或诸如此类)引起的,但是要将onRowClick的事件对象传递到在格式化程序中创建的按钮中似乎是不可能的
我只想从按钮小部件的
onClick
事件中访问rowIndex(和其他DataGrid类属性),以便根据按下的按钮进行处理。大致相同,但下面是我想到的,它似乎朝着我设想的方向发展。按钮所在的已调整单元格:
{ name: "Actions", width : '200px', type: dojox.grid.cells._Widget, formatter :
function(){
return $._actionButtons.call($, arguments);
}
}
在返回的按钮小部件中调整了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")
}
}
我可能最终会扩展按钮来更好地处理这个问题,但现在,瞧
有时候,把它写下来,放在那里,让你的大脑在别人之前惊慌失措,找出解决办法,这会有所帮助:)
次要更新…
DataGrid有formatterScope
参数,但它适用于所有formatter
,因此会弄乱任何需要单元格范围而不是DataGrid范围的内容。上述方法允许我访问所需的一切