How to collect all data from selected rows?

How to collect all data from selected rows?

jtr1812jtr1812 Posts: 11Questions: 4Answers: 0

Hi! I'm very new to this so please excuse me if this has been answered before. I have this data table with multiple columns and checkbox, and Submit button below the table. Can I get help on how to collect all values in the rows with the selected checkboxes so I can send them via REST POST as JSON?

For example, here is my table
Product. Description. Type.
P1 Product 1 description A
P2 Product 2 description B
P3 Product 3 description C
P4 Product 4 description A

Let's say that I have selected P1 and P2 and click on Submit button, I would like to have a json as followed
{
{"Product":"P1", "Description":"P1 description", "Type":"A"},
{"Product":"P2", "Description":"P2 description", "Type":"B"}
}

Please help!!!

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    This example shows how to get the selected items.

    Kevin

  • jtr1812jtr1812 Posts: 11Questions: 4Answers: 0

    Thanks, I came across that example and followed the instruction as well. I'm not stuck at the part where it only seems to return the first row even though I have selected multiple row

    var count = myTable.row({ checkboxes: true }).data();

    If I tried this, var count = myTable.rows({ checkboxes: true }).data(); then it return all rows.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    You need to use { selected: true } not { checkboxes: true }. Use rows() to get multiple rows or use row() for only one row.

    Kevin

  • jtr1812jtr1812 Posts: 11Questions: 4Answers: 0

    Even I change to selected, I would see the behavior where row() API only return first selected row and rows API returns all rows

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin

    That is correct - if you read over the row() and rows() documentation, you’ll see that row() only ever returns a single row (hence the singular row). The plural rows() will return multiple rows if matched.

    Allan

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin

    table.rows({selected: true}).data() will get the data for all selected rows.

    Allan

This discussion has been closed.