PDA

View Full Version : Example: Updating a Grid Cell programatically



Juls
Apr 07, 2009, 8:03 PM
hi,

very happy with coolite. Lets grow the sample code base.

Lets say you have a grid and on each row (record) there is a field (cell) that represents the sum total of a few of the other cells. We'll call that field total. Here is an easy way to do it.

Add a Listener to your grid:



<Listeners>
<ValidateEdit Fn="calcTlt" />
</Listeners>


On each row click you are going to get an event back with the EventArgObject.



var calcTlt = function(e) {

var val = parseFloat(e.value); // this is the value of the current changed field ...
var valOfTlt = parseFloat(e.record.get('Tlt')); // Tlt is the name of the field i want to update
var setTo = valOfTlt + val;
e.record.set('Tlt', setTo);

};

Juls
Apr 08, 2009, 1:44 PM
Following up with some simple modifications that make this work better:

1) Use "AfterEdit" this lets the control value get set


<Listeners>
<CellClick Fn="cellClick" />
<AfterEdit Fn="calcTlt" />
</Listeners>

2) This javascript now calcs the total on the set values in the cells.


var calcTlt = function(e) {
var arrayOfGetLines = new Array('Mon', 'Tu', 'Wed', 'Th', 'Fri', 'Sat', 'Sun');
var iTlt = 0;
for (var l in arrayOfGetLines) {
var line = arrayOfGetLines[l];
var valOf = parseFloat(e.record.get(line));
if (valOf.toString() != 'NaN') {
iTlt += valOf;
}
}

e.record.set('Tlt', iTlt);
};