Change data value to order table

Change data value to order table

ritaalaminoritaalamino Posts: 2Questions: 1Answers: 0

I am trying to change the value of the cell using
table.row(tr).data on click; but I can't find how I am able to do that.

 $('#cautela_table tbody').on('click', 'td.star-control', function () {
                                    var tr = $(this).closest('tr');
                                    var row = table.row(tr);

                                    var cotista_id = row.data().cotista__id;
                                    var status = row.data().favorito;
                                    atualizaStatus(cotista_id, !status);
                                    
                                    if (status == true) {
                                        atualizaStatus(cotista_id, false);
                                        row.node().cells[0].innerHTML = '<i class="far fa-star"></i>';
                                    } else {
                                        atualizaStatus(cotista_id, true);
                                        row.node().cells[0].innerHTML = '<i class="fas fa-star"></i>';

                                    }

I already did this, but I want to change the internal value so the person doesn't have to reload the page.
Can you help me?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 14,172Questions: 1Answers: 2,400
    Answer ✓

    This example is showing how to cell().data(), the same principles would apply for row().data(). Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • ritaalaminoritaalamino Posts: 2Questions: 1Answers: 0

    I have done it! But i have a new problem, after I change the value, the ordering filter is not working to the recently changed cell. Is there something I am forgetting?

                                    var tr = $(this).closest('tr');
                                    var row = table.row(tr);
    
                                    var cotista_id = row.data().cotista__id;
                                    var status = row.data().favorito;
    
                                    row.data().favorito = !status;
                                    # row.draw(); 
    
  • kthorngrenkthorngren Posts: 15,739Questions: 25Answers: 3,730

    row().data() is both a getter and a setter. You need to pass the updated data as a parameter to row().data() to use it as a setter. Something like this:

    var tr = $(this).closest('tr');
    var row = table.row(tr);
    var data = row.data();
    data.favorito = ! data.favorito;
    row.data( data).draw();  // Update the row data
    

    Kevin

Sign In or Register to comment.