jqueryjqgridjqgrid-php

JqGrid: Fetch count of selected rows and add Column value


Below code is used to fetch count of multiple rows selected and add the value based on column name dm

var myrow;
var id = jQuery('#grid').jqGrid('getGridParam', 'selarrrow');
console.log(id.length);
if(id.length)
{
    for (var i=0;i<id.length;i++)  
        {
            myrow = jQuery("#grid").jqGrid('getCell',id[i],'dm'); 
        }
}

The row count is correct, but how could I add all the values from column based on row selection? myrow gives the value of last selected row, but not the addition of all the selected rows.


Solution

  • I'm not sure what value you have to fill in myrow, but I suppose that you need modify you code to use something like the following

    var myrow = [], i;
    ...
    for (i=0; i<id.length; i++) {
        myrow.push(jQuery("#grid").jqGrid('getCell', id[i], 'dm'));
    }
    myrow = myrow.join(); // create comma separated list with values
    

    UPDATED: If dm column has numeric values like 25.00, 5.00 and you need to have the sum of the values from dm column for selected rows then the code could be

    var myrow = 0, i;
    ...
    for (i=0; i<id.length; i++) {
        myrow += parseFloat($("#grid").jqGrid('getCell', id[i], 'dm'));
    }
    alert("The sum is: " + myrow);