I am trying to write a formatter function that will italicize the text in a grid cell based on the value of an associated field that is in the model but NOT in the grid itself.
I know that "this" refers to the cell itself when used in the formatter function. Is there some way to get the right "row" in the model using the cell object or to somehow communicate to the model the row this cell represents?

try
try this
getCalculatedPercentage = function(rowIndex) { // This is standard for many grid handlers if (!grid) { return; } var item = grid.getItem(rowIndex); if (!item) { return; } return item.db_fieldOne / 100; }; customFormat= function(value){ return value + "%" }; var structure = [{ cells: [{ name: 'Cell-One', width: 'auto', headerStyles: 'text-align:center;font-size: 1.2em; font-weight:bold;', cellStyles: 'text-align:center;font-size: 1.1em;', field: 'db_fieldOne' }, { name: 'Cell-Two', width: 'auto', headerStyles: 'text-align:center;font-size: 1.2em; font-weight:bold;', cellStyles: 'text-align:center;font-size: 1.1em;', format: customFormat, get: getCalculatedPercentage } ] }];Hi there. Thanks for the
Hi there. Thanks for the response but that didn't really work out for me. Dojo didn't seem to recognize getItem as a supported function of the Grid object.
In any case, what your code is trying to do isn't actually the same as what I'm trying to do. Your code apparently takes the value of Cell-One, puts it in Cell-Two, and styles it. What I'm trying to do is style Cell-One based on the value of a field that isn't even in the Grid in the first place (but in the Grid's Model).
Thanks.
One thing that's confusing
One thing that's confusing me is that the Cell's "field" value is coming back as a string instead of an integer despite The Book of Dojo's insistence that the value should be an integer.
Is there any sort of relationship between the Cell accessible from within a formatter function and the Grid, the Model, etc.? I can't find any documentation that notes how one would "climb" up the ladder, as it were, to, say, get the correct Row starting from the Cell.
Again, my ultimate goal is to style Cell "A" based on the value of Model Field "B" which is not represented as a Cell in the Grid.
Thanks in advance to anyone who can point me in the right direction!
The formatter function
The formatter function actually receives two arguments: the cell value, and the row index. Inside the formatter function, this.grid should get you a reference to the grid object. It appears that this.index is the column index.
All of the above is with dojo 1.2 (current trunk), so if you're using an earlier version, some of the available data could be different.
I think we both got stuck in
I think we both got stuck on this issue...........
partial answer.
to have the field value as an integer, take a look at "Custom Data Type Mappings" on this page http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...
if you find out how to get a rowIndex let me know.
i'll also keep looking and let you know what i can find.
I've changed my approach a
I've changed my approach a little bit to try to do the styling through the onStyleRow extension point for the Grid. This extension point takes a Row object (for which I can find no documentation such as properties and methods) and if you reference "this" from within the function, you are given the Grid object itself.
I was hoping that I would be able to grab the Model from the Grid and then get to the value of the particular field, but no luck there. No matter what field I try to reference, the value always comes back as "?" even when the Grid clearly shows the proper values have loaded.
I tried this......... could
I tried this......... could get the cellNode, but have no idea how to update its style.......... also having problems trying to get column Index
function onCellContentChange(originalStore, newValue, rowIndex, colInattr) { console.debug("rowIndex:"+rowIndex+" colInattr:"+colInattr+" newValue:"+newValue); console.debug(grid.views.views[0].getCellNode(rowIndex,3)); }; dojo.connect(grid, "doApplyCellEdit", dojo.partial(onCellContentChange, dataStore));Hi there. Thanks for the
Hi there. Thanks for the suggestion.
I'm curious... in another thread in which you posted this solution, it seemed to require that a user edit the cell first. We need the style update to happen upon first load as well. Would this kick in during the grid's initialization?
Also, it doesn't seem to have any affordance for a value not in the grid. The style of my cell "A" will be determined by the value of my model field "B" which does not appear in the grid, in the view or in the structure. In fact, it doesn't appear in the code at all (being data that is pulled from the database).
The model knows about the field but the grid and the view have no direct knowledge of the field value.
For example, imagine that I have a grid that has a column labeled "Name" and referencing field "theName" from the model, and that said model also has fields entitled "theCost," "theColor" and "theQuantity," none of which are columns in the grid. My goal is to make each row's "Name" text bold depending on whether "theQuantity" is negative.
I know how to style the Cell... that's not an issue. I just don't know how to get the right "theQuantity" from the model from the cell passed to its formatting function.
var myView = { cells: [
var myView = {
cells: [
[
{ name: "Name", field: "theName", formatter: myNameFormatter },
{ name: "Date", field: "theDate" }
]
]
};
function myNameFormatter(theValue) {
// If "theQuantity," which is in the model and not in the view or the grid,
// is negative, make the text bold. Otherwise, just leave it alone.
// The question is... how exactly do I find the value of "theQuantity"
// since this cell doesn't seem to have a row index value I can use to
// reference in the model?
}
I had to go back to using a
I had to go back to using a Cell formatter function since the team wasn't fond of the overhead associated with onStyleRow (which is called about a dozen times before our application has finished loading and is called several times just when the user mouses over a row).
I used Firefox's Firebug add-on to examine the Cell object attached to the formatter function but could find no indication in which row the cell was. Does anyone know if it's possible to get the Row object's index from within the Cell object?
i've looked and come to the
i've looked and come to the conclusion that there is no way to get the rowIndex. i'm trying to add a new row and then select it after adding it. it doesn't seem possible to do it without the rowIndex and i've looked through the code and can't seem to find a function or attribute that gives the rowIndex of an item or cell.
I have come to the same
I have come to the same conclusion. The architect in charge of the project is still holding out some hope there is a way and doing some research into it. If he finds anything, I'll be sure to post it here. Thanks for your help, all!
var myView = { cells:
var myView = { cells: [ [ { name: "Name", field: "theName", formatter: myNameFormatter }, { name: "Date", field: "theDate" } ] ] }; function myNameFormatter(theValue) { // If "theQuantity," which is in the model and not in the view or the grid, // is negative, make the text bold. Otherwise, just leave it alone. // The question is... how exactly do I find the value of "theQuantity" // since this cell doesn't seem to have a row index value I can use to // reference in the model? }Try adding
Try adding console.log(arguments) inside of myNameFormatter. Notice that the function is actually receiving two arguments? The second one is the row index.
Also, if you inspect the object that "this" references inside of the formatter function, you should find a this.grid that gets you access to the parent grid. And this.index should give you the column index.
Note that these explorations are with dojo 1.2 -- currently trunk. So some of the data on "this" may be different. The formatter function definitely received a row index prior to 1.2, though.
Great! Thanks very much for
Great! Thanks very much for your suggestion. I will try this out immediately.
what can i do outside of the
what can i do outside of the formatter function to get the rowIndex? when the user clicks on an "Add Row" button, i'm using store.newItem(item) to add a new row. then i want to select that new row. I have the index to the item in the new row but there seems to be no function that can get me the rowIndex from the itemIndex.
any suggestions...
formatDate = function(value,
formatDate = function(value, rowIndex){ console.debug(rowIndex); console.debug(this.grid.getItem(rowIndex)); return dojo.date.locale.format(new Date(value), this.constraint); };wow. it does have rowIndex
It looks like you beat me to
It looks like you beat me to trying this out! :-D Thanks for the confirmation! I'm going to incorporate this into my project right now.
Ok store.isDirty method is
Ok store.isDirty method is not helpful. If I edit and the value is still same it marks dirty. So had to fix in my format method. I am almost there. I could compare the original and current values to know if the value is changed. If changed I need to style the cell background, how do I do that?
formatStayDate = function(currentItemValue, rowIndex){ var store = this.grid.store; var currentItem = this.grid.getItem(rowIndex) var formattedCurrentItemValue = dojo.date.locale.format(new Date(currentItemValue), this.constraint) var originalItem = store._pending._modifiedItems[store.getIdentity(currentItem)]; if(originalItem) { if(originalItem['staydate'][0] == formattedCurrentItemValue) { console.debug("not dirty"); } else { console.debug("dirty"); } } return formattedCurrentItemValue; };and my cell is as follows:
name: 'Stay Date', width: 'auto', headerStyles: 'text-align:center;font-size: 1.2em; font-weight:bold;', cellStyles: 'text-align:center;font-size: 1.1em;', editable: true, type: dojox.grid.cells.DateTextBox, formatter: formatStayDate, constraint: {formatLength: 'long', datePattern:'MM/dd/yyyy', selector:'date'}, field: 'staydate',Please help need to change the cell background color when the value is dirty
there are a number of ways
there are a number of ways i've found to change the style in the formatter but they all get overwritten eg -
dojo.style(this.getNode(rowIndex), {"backgroundColor": "red"});orvar currentCell = this.grid.views.views[1].getCellNode(rowIndex, this.index); dojo.addClass(currentCell, "edited");there is an example in dojocampus - http://dojocampus.org/explorer/#Dojox_Grid_Styling which uses onStyleRow to make some changes but it's still only on a complete column or row.
good luck... i don't know if it can be done on an individual cell. it seems that something is overwriting it - if you set a break point, in firebug you can see the change to red and then you see it overwritten when the row completes.
use customStyles
function formatter(f,ind){ var it=this.grid.getItem(ind); if(it){ if(this.grid.store.getValue(it,'some_field')){ this.view.getRowNode(ind).style.color="#ccc"; //set style on the row this.customStyles.push("color:gray;"); //set style on the cell }else this.customStyles.push("font-weight:bold;"); //set style on the cell return f; } return f; }--
http://www.liucougar.net
生于忧患,死于安乐
"People's characters are strengthened through struggle against difficulties; they are weakened by comfort."
- Old Chinese adage
it worked.............
it worked............. awesome
so now my format method looks as follows
formatDate = function(currentItemValue, rowIndex){ var store = this.grid.store; var currentItem = this.grid.getItem(rowIndex) var originalItem = store._pending._modifiedItems[store.getIdentity(currentItem)]; var formattedCurrentItemValue = dojo.date.locale.format(new Date(currentItemValue), this.constraint) if(originalItem) { if(originalItem['staydate'][0] == formattedCurrentItemValue) { this.customStyles.push("color:black;") } else { this.customStyles.push("color:green;font-weight:bold;"); } } return formattedCurrentItemValue; };quick question how can i generalize this method for all dates........ meaning in the above method, I hard coded the column field which is 'staydate', how do i generalized it.
Seriously you saved my 2 weeks worth of work. The client was so strong on this feature. Thanks once again
The value of this.field has
The value of this.field has the current field name, so replacing 'staydate' with this.field in your code should do the trick.
Can we embed the same logic in 'onApplyCellEdit' ?
Can we apply the same logic when we update the grid?
For instance, you have some logic in 'onApplyCellEdit' event handler and depends on cell data need to apply different CSS (color) to the cell.
In formatter method we would have cell context, so we can use 'this.customStyles.push("color:green;font-weight:bold;");'. Wondering how to get cell context in any of the grid event handlers ?
Help is greatly appreciated!!!!
fwiw, the formatter gets
fwiw, the formatter gets called after onApplyCellEdit so you should be able to use the formatter to change any of the styles. if you are needing information about the cell in onApplyCellEdit, the value, inRowIndex and column name are passed to onApplyCellEdit.
note to self and others: since trac wasn't working, this is a reminder that there is a bug with onApplyCellEdit - it passes the column name instead of the column index. this makes it inconsistent with the api http://api.dojotoolkit.org/jsdoc/dojox/1.2/dojox.grid._Events.onApplyCel...