Datatable rows recorder

Datatable rows recorder

Coder102Coder102 Posts: 78Questions: 2Answers: 0

Hello, I am consuming an API that returns 98 elements, each row has a button. I need to go through each of the rows and depending on the row that I click, it returns the values โ€‹โ€‹of that row. How do I do that?

Replies

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

    Here is an example of buttons in the row:
    http://live.datatables.net/xijecupo/1/edit

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0

    I already have the buttons in the table, I need that according to the row that I touch to return the information of the row. How do I go through all the rows?

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

    The example shows how to get the row data of the clicked button. The key is to get the tr of the clicked row using var row = $(this).closest('tr'); then using var data = table.row( row ).data() to get the data for the row. If this doesn't help please post a link to your page or a running test case showing what you have so we can help.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    How do I go through all the rows?

    Not sure what you are asking for but to iterate all the rows use rows().every().

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    table.rows().eq(0).each(function (index) {
            var row = table.row(index);
            var data = row.data();
            console.log(data);
          });
    

    I did something like that, but it returns all the items I have. I want it to return the elements of the row that I click, in the row there are 5 elements and I want it to return 3 (integer, decimal, natural)

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

    Maybe I don't understand. I have posted an example and code snippets of how to get the row data form the clicked row. Are you looking for something different?

    Please provide a test case of what you have. Maybe it will help my understanding of your question.

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020

    ok

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0

    I can't show you the code

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

    I can't show you the code

    Hmm, that will make it difficult to help. You can't build a simple example, maybe update mine, to show what you have for the buttons?

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020

    It doesn't matter, thanks anyway, I can send it to you by private message if possible

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

    alright I'll look.

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0
    edited October 2020

    I send you a private message

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

    Took a look. You are invoking a modal via the button using data-toggle='modal' data-target='#modalWindow' on the button tag. You are using this modal event $("#modalWindow, body").on("shown.bs.modal", function () {. In the event we need to get the clicked element. This SO thread has some examples of how to do this.

    Using this will give you the row data:

      $("#modalWindow, body").on("shown.bs.modal", function (e) {
        var clickedBtn = $(e.relatedTarget);
        var tr = $(clickedBtn).closest('tr');
        var data = table.row( tr ).data();
    ....
    

    Kevin

  • Coder102Coder102 Posts: 78Questions: 2Answers: 0

    Thanks !!

This discussion has been closed.