Is there a version of rows().ids() that will return specified columns' data of selected rows?

Is there a version of rows().ids() that will return specified columns' data of selected rows?

jibranbjibranb Posts: 8Questions: 3Answers: 1

Hello. I am trying to get 2 specific columns' data of selected rows on click of a custom action. Consider the following:

// not enough data
var product_ids = dt.rows( { selected: true } ).ids().toArray(); 

// too much data
var products_lots_of_data = dt.rows( { selected: true } ).data().toArray(); 

Or do I have to loop through selected rows and pick which columns I want? Like this:

var products = new Array( products_lots_of_data.length );
for( var i = 0; i < products_lots_of_data.length; i++ ) {
    var product = products_lots_of_data[i];
    products.push( { internalId: product['internalId'], itemId: product['itemId'] } )
}

The loop sample is working, but curious if there's a method in Datatables for this purpose. Apologies if this is in the docs. I've been searching but haven't found anything.

Thanks,
-Jibran

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    A call to map() is probably what you want here - take the row data and pluck out just the parts you want:

    var product_ids = dt.rows( { selected: true } ).data().map( r => {
      return {
        internalId: r.internalId,
        itemId: r.itemId,
      };
    } );
    

    Allan

  • jibranbjibranb Posts: 8Questions: 3Answers: 1

    Ahhh thank you! That is what I was looking for.

This discussion has been closed.