Author Topic: Detect Selected Rows  (Read 3129 times)

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Detect Selected Rows
« on: September 20, 2021, 04:17:23 pm »
I'm iterating the grid rows and updating the row, if it is currently selected.
-How do I determine if the row is currently selected?
-Is below the best way to iterate/update specific rows in the grid?

                var data = $gridMain.pqGrid('getData');

                for (i = 0; i < data.length; i++) {
                    if ( //how to determine if row is selected??) {
                                $gridMain.pqGrid('updateRow', { checkEditable: false, rowIndx: i, row: { [key]:newvalue } });
                    }
                }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #1 on: September 20, 2021, 08:51:48 pm »
Assuming selectionModel.type = 'row', selected rows have pq_rowselect property set to true.

BTW it's not good to call updateRow in a loop for performance reasons, rather rowList could be prepared in a loop and updateRow method be called once.

https://paramquery.com/pro/api#method-updateRow (update multiple rows at once.)

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #2 on: September 20, 2021, 09:28:56 pm »
Thanks, Are you able to update the example below to show this?

https://jsfiddle.net/yn1km54b/


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #3 on: September 21, 2021, 08:06:56 pm »
Please give it a try and let me know if you face any difficulty.

Code: [Select]
selectionModel: {type: 'row'}

Code: [Select]
if( data[i].pq_rowselect ){ //row is selected.
  //construct the rowList.
}

Multiple rows update at once example is there in the API:

https://paramquery.com/pro/api#method-updateRow

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #4 on: September 22, 2021, 01:11:19 am »
Thanks.  I need to understand how to construct the rowList based on the data index. The updateRow is not updating the correct row for me when when I have the grid grouped, so seeing a working example in the JSFiddle file I sent would help me.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #5 on: September 22, 2021, 11:10:04 am »
No problem, please check this: https://jsfiddle.net/ntkmosa3/

I've used grid SelectRow API to get selected rows.

https://paramquery.com/pro/api#method-SelectRow

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #6 on: September 22, 2021, 05:23:50 pm »
Thanks! If I want to iterate all rows and not just the selected rows, how would this line change?

var allRows= grid.SelectRow().getSelection();

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #7 on: September 22, 2021, 05:37:13 pm »
Code: [Select]
var allRows= grid.pageData();

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #8 on: October 19, 2021, 04:00:18 pm »
Changing the selectionModel to { type: 'row' }, doesn't help because I need the cell drag and fill, copy and paste, and delete capability. Can you update the function 'UpdateSelectedRows' in the example below, if the selectionModel is not 'row'?

https://jsfiddle.net/ntkmosa3/

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #9 on: October 19, 2021, 10:54:53 pm »
In that case, we can use eachRow method of Selection object.

Code: [Select]
grid.Selection().eachRow(function(rowData, rowIndx){
   rowList.push({
     rowIndx: rowIndx,
     newRow: {company: 'ABC'}
   })
})

https://jsfiddle.net/p0qn3x6o/

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #10 on: October 20, 2021, 04:05:08 pm »
Any idea why I get this error? Code seems identical to this example. https://jsfiddle.net/h02gs4Lk/
« Last Edit: October 20, 2021, 04:51:17 pm by mikep »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #11 on: October 20, 2021, 05:11:48 pm »
https://paramquery.com/pro/api#method-Range

API states that eachRow method is available since v8.0.0

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Detect Selected Rows
« Reply #12 on: October 20, 2021, 06:22:04 pm »
Thanks for quick reply. It works great!

I'm updating all my code to account for this.
-what's the best way to get a count of selected rows now? (I'm creating a counter and iterating the foreach, not sure if there's a better way.)
-what the best way to reference the selected row, if the count is 1. (I'm using the foreaach, even though only 1 record, not sure if there's a better way)

--new code
                var selCount = 0;
                grid.Selection().eachRow(function (rowData, rowIndx) {
                    selCount += 1;
                })

--old code
                var grid = $gridMain.pqGrid('instance');
                var selection = grid.SelectRow().getSelection();

                if (selection.length > 1) {
                    showNotify("Only 1 row in the grid can be selected.");
                }
                else if (selection.length == 1) {
                        DoAction(selection[0].rowData)
                }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Detect Selected Rows
« Reply #13 on: October 21, 2021, 09:36:09 am »
1. It's correct to count the no of selected rows.

2.

Code: [Select]
grid.Selection().eachRow(function (rowData, rowIndx) {
     //rowData is reference to each selected row.
 })