Search for value in cell of table

Search for value in cell of table

jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

Hello,

I have a table where I am adding rows with row.add() function,
Now I need to check in a particular column that x value exists in any of row of the table,
If the value exists, then I will add some action on that row,
and if the value does not exist, then it will add a row.

So how can we search a specific column?

This question has an accepted answers - jump to answer

Answers

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

    Its not clear to me exactly what you want. Maybe createdRow or rowCallback is what you are looking for.

    Kevin

  • jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

    @kthorngren

    Hello Kevin,
    See below code,

                            //Got array of existing entries
                            var itemIdColumnData = table.column(0).data().toArray();
    
                            //ifNotExist will return -1 if value not in array
                            var ifNotExist = jQuery.inArray(data.response.item_id, itemIdColumnData);
    
                            if(ifNotExist === -1) //going good
                            {
                                table.row.add( {
                                    "item_id":   data.response.item_id,
                                    "item_name":  data.response.item_name,
                                    "item_price":     data.response.item_price,
                                    "item_quantity": quantity,
                                    "item_quantity_r": quantity,
                                    "item_total":  total,
                                    "item_action":  'delete'
                                } ).draw(false);
                            }
                            else{ //need to update existing row
    
                                //First we need to find that row and get value for same.
    //Get row data and update with row.add() data
    
                            }
    

    Here I need to find that row which has value and need to update that row data with the newly created row.add()

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

    If I understand correctly you want to add the row if it doesn't exist in the table but if it does then update the existing row. Is this correct?

    You can use row().data() to get the row data. Use the row-selector as a function to find the row matching your criteria. See the function example in the docs.

    Then use row().data() to update the row data.

    Kevin

  • jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

    @kthorngren ,
    Thank you, I could able to get row which is matching criteria with below code,

    var matchdata = table.rows( function ( idx, rowdata, node ) {
        return rowdata.item_id === data.response.item_id ? true : false;
        })
        .data();
    

    Now how can I update this specific fetched row or any of it's cell only?

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    The variable matchdata will contain an array of one or more rows, since you used rows(). If you just want one row then use row().

    For this example I will assume each rowdata.item_id is unique so you will get only one row. Try this:

    var matchRow = table.row( function ( idx, rowdata, node ) {
        return rowdata.item_id === data.response.item_id ? true : false;
        });
    
    // Get row data
    matchData = matchRow.data();
    
    // Update your row data, for example
    matchData.item_name = 'New Name';
    
    // Update the row data and redraw the table
    matchRow.data( matchData ).draw();
    

    If you need to use rows() to get multiple rows then you will need to loop through the matchRow array of rows to update them individually.

    Kevin

  • jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

    Hello Kevin,
    It worked, thank you,
    Can you help me how can I assign data to form field input in this row?
    Below linked discussion is my form field input.

    https://datatables.net/forums/discussion/comment/183746

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

    Start with the jQuery val() method. If you need help with this please start a new thread with your test case showing what you are trying to do.

    Kevin

This discussion has been closed.