how can I get value of td at specific row?

how can I get value of td at specific row?

gnomxgnomx Posts: 14Questions: 2Answers: 0
edited April 2022 in DataTables

Hi there. I'd like to know how can I get a text fro a TD by using datatable.
I have a datatable called #myTable. On last TD there's a button that call a modal. I need to know how can I get a text on TD that belong that row.
I've tried a lot of code but every time i got undefined! I'm gonna crazy!

$('#myTable').on('click','#compile',function(){ debugger })

compile is id of my btn that call modal

each row ha its button

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Start with this delegated events example. Here is an example with buttons:
    http://live.datatables.net/xijecupo/1/edit

    You have #compile which doesn't seem right as the IDs need to be unique on the page. If you still need help please build a simple test case showing what you are trying to do so we can offer specific suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    #compile - do you have an element with id="compile" in every row? That isn't valid HTML, each id must be unique.

    I would suggest you use a class name rather than an id.

    To get the data of a cell you can use cell().data() or for the whole row row().data().

    If you link to a test case showing the issue, I'll be able to help further.

    Allan

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    but if I use clause $(this) it doesn't referred at that specific row?
    Yes I've id inside each TR

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Maybe use jQuery closest(), like this $(this).closest('tr).

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    I've already tried and didn't work. I don't what call server to retrieve information. I want get it from table.

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    The best next step is for you to build a simple example showing what you have or provide a link to your page. This way we can have an understanding of your solution to provide specific suggestions.

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    How can I do it?

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Are you asking how to create a test case?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    Answer ✓

    I updated my above example so that each row has an id. When clicking the button it will output the row id to the console:
    http://live.datatables.net/xijecupo/1282/edit

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    I got it.
    Here the working code , I need to test into my modal , but when I click I get TD desired.
    <script type="text/javascript"> $(document).ready(function(){ $('#elenco_bonifici_fase2 tbody').on( 'click', 'a', function () { var id = $(this).closest('tr').find('td.ufficio').text(); console.log(id); }); }) </script>

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    Thanks a lot kevin. I'll try it tomo.

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    ok it works! Now i need to know if I hide an cell from Datatable by using
    "targets": [ 6 ],
    "visible": false
    Is it possible get that information inside it?

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Use cell().node().

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0
    edited April 2022

    No works...td isn't in the DOM but is in the cache.

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    That is why you use cell().node(), like this:
    http://live.datatables.net/virikuro/1/edit

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    No works... I'm sorry

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    I leave it not hidden :(

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    edited April 2022 Answer ✓

    My example works. If you are having difficulties using cell().node() then provide an example of what you are trying to use so we can help. Or if my test case is not doing what you want then let us know what you are expecting.

    Kevin

  • gnomxgnomx Posts: 14Questions: 2Answers: 0

    I'm so sorry about it. I've moved all code together in js file. So before code was split in two parts.Now works , i got .data() instead .node()

Sign In or Register to comment.