Understanding row-selector by column value

Understanding row-selector by column value

xaudixaudi Posts: 4Questions: 1Answers: 0
edited April 2020 in Free community support

I populate a table with a JSON data like this:

{
    'id' : 1,
    'name': 'Jhon',
    'category_id': 12,
},
{
    'id' : 3,
    'name': 'Max',
    'category_id': 12,
},
{
    'id' : 4,
    'name': 'Bob',
    'category_id': 23,
}

I can select a row this way

table.row({id:3})

But I can't use this for select many rows

table.rows({category_id:12})

This return ALL rows, without filter category_id:12

I read the doc at https://datatables.net/reference/type/row-selector and this doesnt help.

I found a this comment and was a similar behavior, but I want know if this is overthinking and I'm doing something wrong.

https://datatables.net/forums/discussion/49916/how-can-i-find-row-and-column-indexes-of-certain-value-with-datatable#Comment_132243

var rowIndexes = [];
table.rows( function ( idx, data, node ) {             
     if(data.somefield === somevalue){
        rowIndexes.push(idx);                  
     }
     return false;
});

I use this.

let rowIndexes = [];
table.rows( function ( idx, data, node ) {             
     if(data.category_id === 12){
        rowIndexes.push(idx);                  
     }
     return false;
});
let myrows = table.rows(rowIndexes);

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765
    edited April 2020

    Try the row-selector function example. Replace .data() with .indexes() to get the indexes.

    Kevin

  • xaudixaudi Posts: 4Questions: 1Answers: 0

    Did yor read the question? Wich .data() ?

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765

    Did yor read the question?

    Yes! Looks like you want to get the row indexes of rows where category_id == 12.

    Here is the example code in the function docs:

    var names = table
        .rows( function ( idx, data, node ) {
            return data.first_name.charAt(0) === 'A' ?
                true : false;
        } )
        .data();
    

    Replace the last line with .indexes() like this:

    var indexes = table
        .rows( function ( idx, data, node ) {
            return data.category_id == 12 ?
                true : false;
        } )
        .indexes();
    

    If you don't want the indexes the return what you do want. If you just want the rows then do this:

    var indexes = table
        .rows( function ( idx, data, node ) {
            return data.category_id == 12 ?
                true : false;
        } );
    

    Kevin

  • xaudixaudi Posts: 4Questions: 1Answers: 0

    ok! Thanks

    But the question is unclear, is more like:

    I need use a function instead a {category_id:12}?
    (Yes/No)

    Because, I can use table.row({category_id:12}) but this only return 1 row. logically

    Actually, I need data from selected rows for more operations.

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765

    table.row({category_id:12})

    You need to use rows(), instead of row(), to return more than one row. As far as I know using {category_id:12} as a row selector isn't going to find the rows based on the data. Did you verify the correct row was returned and not just the first row?

    The row-selector function docs specifically state this:

    That can be particularly useful for finding rows based on the data contained in the rows, or based on properties of the nodes.

    Actually, I need data from selected rows for more operations.

    Are you using the Select Extension? If so this example show how to get selected rows:
    https://datatables.net/extensions/select/examples/api/get.html

    Kevin

  • kthorngrenkthorngren Posts: 20,277Questions: 26Answers: 4,765
    edited April 2020 Answer ✓

    Also note the row-selector does not take an object, like {category_id:12}, as a parameter. However a selector-modifier is an object. What you are passing to the row() API is a selector-modifier. Since its not a valid one it is ignored.

    Effectively table.row({category_id:12}) will result in the same row as table.row() which is the first row.

    Kevin

  • xaudixaudi Posts: 4Questions: 1Answers: 0
    edited April 2020

    I'm a 'edited' asshole, you're right.

    At some point I worked with an external plugin that added this feature, when I tried it now it's just a fluke.

    But now I wrote a function that works how I want.
    (I have not tested if it is completely correct.)

    const searchFunction = function(dictSearch) {
        return function( idx, data, node ) {
            const keys = Object.keys( dictSearch );
            const n = keys.length;
            let k = 0;
            for (let i = 0; i <= keys.length; i++) {
                //intrinsically checks if the key exists
                if ( data[keys[i]] === dictSearch[keys[i]] ) {
                    k++;
                } else {
                    return false;
                }
                if (n === k) {
                    return true;
                }
            }
            return false;
        } 
    }
    

    Usage

    table.rows(searchFunction({category_id:12})).data()
    table.rows(searchFunction({category_id:12,name:'Max'})).data()
    
This discussion has been closed.