How to select and show a new row that was entried on database

How to select and show a new row that was entried on database

InKairoInKairo Posts: 5Questions: 2Answers: 0

Hello, thank you all for your time. I know I should put a test case but I don't know how to do it as it's a really particular issue I'm facing.

I have a form which creates with ajax new entries on a database. A datatable with ajax feeds from that database on the same page. So I call $('#table').DataTable().ajax.reload in the success case of the form. I get the ID from the new row as response.

                                

...Ajax Code...
success: function (result) {
                                var idSelected = result['id'];
                                $('#table').DataTable().ajax.reload(function () {

                                    row = tablaOrdenes.row(function (idx, data, node) {
                                        return data['id'] === idSelected;
                                    });
                                    if (row.length > 0) {
                                        row.select()
                                            .show()
                                            .draw(false);
                                    }
                                }, false);
}

The problem is that this code doesn't work even when I'm having the correct id returned and the new row is being rendered but it's not being selected nor shown... But if i change idSelected for an ID that already exist on the table (meaning that is not the new created entry) like return data['id'] === '4';the code works like a charm.

I hope I can solve this issue.
I love datatables so far. It's an amazing tool and would definitelly recommend to anyone!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    Answer ✓

    its hard to say without seeing the problem. If the problem is with return data['id'] === idSelected; then that is where you need to start debugging. What is the value of data['id'] and what is the value of idSelected? Are they the same type, ie, string or numeric? If different then maybe you need to use == instead of ===.

    Kevin

  • InKairoInKairo Posts: 5Questions: 2Answers: 0

    Solved the Issue. The problem was aparrently that idSelected is number. I used return data['id'] === String(idSelected); and that solved the issue. Sorry for the spam.

  • InKairoInKairo Posts: 5Questions: 2Answers: 0

    Thanks Kevin! It indeed was a problem of type. As hard to believe as it might be, the == did not solve the issue when I tried.. but transforming idSelected to String. I appreciate the help!

This discussion has been closed.