Extending the Dojox Datagrid with Formatters
Posted in Inside TFG
I’m currently trying to create a Dojo widget that extends dojox.grid.DataGrid and has sub grids inside it. It’ll likely expand over time to be somewhat of a complex beast so there wasn’t really any other way than going the self contained widget. Having a bunch of global functions and DOM manipulation would be disgusting.
I’m building this widget in an incremental way so after getting the layout and hooking it up to a data source I moved on to creating a column with an expand/contract link that would trigger the sub grid to expand and contract as required. I ran into a problem when trying to keep my formatting functions contained within the widget definition.
I setup my layout in the following way :
_mainGridLayout : [
{ type : "dojox.grid._RowSelector" },
{
cells : [[
{ name : '', get : this._getBlank, formatter : this._formatExpander },
{ field : "customer_id", name: "Cust ID" },
{ field : "customer_name", name : "Customer" },
{ field : "job_number", name : "Job No." },
...
]]
}
],
I had defined the get function and formatter function in my widget, but whenever I loaded the widget I’d get a blank column as if neither function had been run.
After some poking and prodding it seemed that whatever context this view was being built in, the ‘this’ object was not my widget as I originally expected. The ‘this’ object was actually an instance of dojox.grid.cell. That was okay as a cell has a reference to it’s grid, so what I needed to do was access this.grid._formatExpander and this.grid._getBlank.
I did it in the following way :
{ name : '', get : function() {return this.grid._getBlank(arguments);}, formatter : function() {return this.grid._formatExpander(arguments);} },
It worked perfectly, I’d like to say just as I expected, but alas I’m not that smart :)
It feels to me like there should be a cleaner way to do it, but at least for the time being it works fine and doesn’t required me to move my formatting functions out into some global namespace and outside of my widget’s nice pieces of logic.