We help IT Professionals succeed at work.

looping with javascript

MandyProgza
MandyProgza used Ask the Experts™
on
Hi

I need help with creating a loop on an  Infragistics webgrid with javascript. The grid has 3 columns, I add values in column 1 and 2 and put the result in column 3, for each row.
Then I add values on the same column, row 1 and row 2 and put the result on row 0. I want it to be automatically done as values change on different rows/columns.

I used the attached and was only able to get part of the expected outcome.
I attached the code that I am currently using and the desired outcome on the excel spreadsheet.

Assistance will be greatly appreciated.

Regards
MandyProg
CellUpdateHandler.txt
example-of-grid.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
First off, this:
            if (Col10 == null) Col1 = 0;
            if (Col20 == null) Col2 = 0;

Open in new window

should be this:
            if (Col10 == null) Col10 = 0;
            if (Col20 == null) Col20 = 0;

Open in new window

Top Expert 2007

Commented:
Second, you need to use the same variable names.

This:
var total = 0;

Open in new window

should be this:
var Total = 0;

Open in new window


Further, I'd change the code as follows:
 function Grid1AfterCellUpdateHandler(gridName, cellId) {

            var activeRow = igtbl_getActiveRow(gridName);

            // Calculate Total for current year
            // Get values from the grid and populate the variables    	    	
            var Col1 = activeRow.getCell(9).getValue() || 0;
            var Col2 = activeRow.getCell(10).getValue() || 0;

            activeRow.getCell(11).setValue(parseFloat(Col1) + parseFloat(Col2));

            // Calculate total for previous year
            var Col10 = activeRow.getCell(13).getValue() || 0;
            var Col20 = activeRow.getCell(14).getValue() || 0;

            activeRow.getCell(15).setValue(parseFloat(Col10) + parseFloat(Col20));
        }

Open in new window

Author

Commented:
Hi

Thanks for the recommendations. The column on the last row only gets the correct total if I tab into the column, else it displays the old figure. Anyway to have it updated? if the cell on the column gets updated regardless of which column/cell has the focus?

e.g. I change row2 on column 1, then row 0 of column 1 gets the new column total, third column on row 2 also gets updated but the grand total for the last column on row 0 doesn't get updated.

MandyProg.
Top Expert 2007
Commented:
This is untested, as I am uncertain which cells in the grid are affected.

Assuming that the grid values are by row, this should do what you want. It updates an arbitrary number of cells in a row (initially set to 3).
// =====================================

function update_grid(gridName, cellId, limit) {
    
    var activeRow = igtbl_getActiveRow(gridName),
        ra = [],
        i = 0,
        j = 1,
    //
    xknb; // Dummy final var declaration
    
/*
    This will update 3 cells in a given row 
    (add the values in cells #1 and #2 and place that value in cell #3)

    To increase the number of cells to total, simply add 1 to "limit" above
    
    limit = limit || 10; // Minimum calls to update
*/
    
    limit = limit || 3; // Minimum calls to update
    ra[0] = 0; // Set value to a known state
    
    // Calculate Total for current year
    // Get values from the grid and populate the variables
    for (i = cellId; i < limit; i += 1) {
        ra[j] = parseFloat(activeRow.getCell(i).getValue()) || 0; // Cell value
        ra[0] += ra[j]; // Total value
        j += 1;
    }
    return ra;
}
// =====================================

function Grid1AfterCellUpdateHandler(gridName, cellId) {
    
    cellId = cellId || 9; // Default value
    
    var val = [], // Updated cell values
        i = 0, // Loop counter
        bump = 4, // Distance between cell groups
        limit = 1, // Number of cell groups (zero-based)
        here = 0, // Offset into the grid
        there = 2, // Distance from first cell to total cell
    //
    xknb; // Dummy final var declaration
    
/*
    This will update a total cell in a row 
    
    For each group of cells you wish to total, simply add 1 to "limit" above
*/
    
    for (i = 0; i < limit; i += 1) {
        here = cellId + (i * bump); // Calculate total destination
        val = update_grid(gridName, here); // Values for cells
        activeRow.getCell(here + there).setValue(val[0]); // Stuff total value into grid
    }
}
// =====================================

Open in new window

Author

Commented:
Thanks a lot!
Top Expert 2007

Commented:
No worries - glad to help.