"; }, setCellValue: function(gridData, storeData, cellWidget){ var data = doSomethingIntersting(gridData); cellWidget.progBar.set('value', data); // cellWidget.cell give you full access to everything you want. var rowIndex = cellWidget.cell.row.index(); } } setCellValue 方法会在每一行被render的时候被调用。当这个方法被调用时,widget已经被创建并且你能完全控制widget。你可以在widgets上设置值,改变css,或者操纵dom节点并且添加事件。这个方法中的第三个参数'cellWidget'引用了cell widget本身,这是拥有从'decorater'方法中返回的template string的widget,所以你可以访问任何的在控件中定义的“dojo attach point”。第一和第二个参数是当前cell的grid data与store data。他们只有当'formatter'方法被提供时才会有差别。你可以通过cellWidget.cell来获取当前的cell,从中你可以获取任何你需要的东西。
{ id: 'progress', field: 'progress', name: 'Install Progress', widgetsInCell: true, decorator: function(){ return ""; }, setCellValue: function(gridData, storeData, cellWidget){ cellWidget.btn.set('label', gridData); }, getCellWidgetConnects: function(cellWidget, cell){ // return an array of connection arguments return [ [cellWidget.btn, 'onClick', function(e){ alert(cell.data()); // do your job here..... }] ]; }, initializeCellWidget: function(cellWidget, cell){ // create extra widgets or manipulate dom nodes that depends on current cell context. cellWidget.anotherButton = new Button({...}); cellWidget.domNode.append(cellWidget.anotherButton.domNode); }, uninitializeCellWidget: function(cellWidget, cell){ // don't forget to undo the changes you made in initializeCellWidget, so that it can be reused among different rows. cellWidget.anotherButton.destroy(); } } 如你所见,事实上你可以在setCellValue方法中做这些事情。这个新方法只是提供了更多的语义,使你的代码更加易于阅读并且省去了你添加额外的注释的功夫。
创建控件的编程方式
如果你 曾经用过DataGrid , 你可能会对于在 f ormatter 方法中返回widget的方式感到熟悉,并且对于写 t emplate string的方式感到 变扭。因此你可以使用 onCellWidgetCreated 事件,甚至 省略“ decorator ” 方法(gridx 1.2之后) : { id: 'progress', field: 'progress', name: 'Install Progress', widgetsInCell: true, onCellWidgetCreated: function(cellWidget, column){ var btn = new Button({...}); btn.placeAt(cellWidget.domNode); } }