0

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.

Slimshadddyyy
  • 4,085
  • 5
  • 59
  • 121
  • You assign `myrow` different values in the loop. The last value (`Query("#grid").jqGrid('getCell',id[id.length-1],'dm')`) will be in `myrow` after the loop is finished. It's unclear what you want to implement... – Oleg Jul 02 '15 at 10:39
  • I need to display the total number of rows selected and addition of column values of that selected rows – Slimshadddyyy Jul 02 '15 at 10:45

1 Answers1

1

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);
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg: `dm` column has numeric values like `25.00, 5.00` for different rows. When user selects say two rows having above column values, I need to display its sum i.e. `25.00+5.00=30.00` – Slimshadddyyy Jul 02 '15 at 10:58
  • `myrow += jQuery("#grid").jqGrid('getCell',id[i],'dm'); ` to add both the values but it says `undefined 25.005.00` – Slimshadddyyy Jul 02 '15 at 11:10
  • @Slimshadddyyy: If you need the sum then you should convert the returned values to numbers: `myrow += parseFloat(jQuery("#grid").jqGrid('getCell',id[i],'dm'));` and initialize the `myrow` to `0`. See **UPDATED** part of my answer. – Oleg Jul 02 '15 at 11:33
  • @Slimshadddyyy: Sorry, but I see no important differences in the code. Do you have some problems with the code? – Oleg Jul 02 '15 at 11:52
  • Oleg: Did the same, pls see update `var myrow= 0; for (var i=0;i – Slimshadddyyy Jul 02 '15 at 11:53
  • @Slimshadddyyy: **What problem you have?**. I wrote you before that I see no important differences between the code which I posted and the code which you posted in the comment. You use `jQuery` instead of `$` and includes `var` on another place. So I don't understand your question. **If you have some problem you should describe the problem instead of asking me to find the differences in almost the same fragments of JavaScript code.** – Oleg Jul 02 '15 at 12:01