I'm getting multiple outputs of rows when I use row().data()

I'm getting multiple outputs of rows when I use row().data()

thehamithehami Posts: 4Questions: 1Answers: 0
edited November 2019 in Free community support

Hello everyone,
I have a problem with getting one value from a row. I can't seem to find any anwsers. So here goes my first post ever.
The code is supposed to give me an ID to work with but it's giving me all data/IDs from the table.

Here is the code:

I think the problem arises because the button that's called is a class.

I'm sorry if this is an easy problem with an easy solution. I just began coding a while back and I'm still learning.
If anything is unclear or I forgot a part of the important code I'll add it.

Fabian

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    I think the problem is due to having the click event in the columns.render function. This is creating multiple tr click events; one for each row. Meaning if you have 100 rows when you click a row your script will execute 100 click events. Move the events outside of the Datatables init code sow the event initialization code executes only once.

    Kevin

  • thehamithehami Posts: 4Questions: 1Answers: 0
    edited November 2019

    Hey Kevin,
    That was the way I originally wrote it. The problem I ran into with that way was and still is that the modul thats supposed to open doesn't open.

    Fabian

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    You need to use delegated events. See this FAQ. Maybe this example will help:
    http://live.datatables.net/qemodapi/1/edit

    Kevin

  • thehamithehami Posts: 4Questions: 1Answers: 0

    Hello Kevin,

    i've tried your live datatable but im getting 'Uncaught SyntaxError: Unexpected string' for the lines 274 and 290.

    Fabian

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited November 2019 Answer ✓

    That on events aren't in a code block - they're just dangling. You wouldn't want to put those into columns.render anyway, I suspect they belong in initComplete but without seeing your code it's hard to tell.

    If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • thehamithehami Posts: 4Questions: 1Answers: 0
    edited November 2019

    Hey Colin,

    sorry probably a really basic rookie question but how do i give u guys a test case when all my data is pulled inot the table via php from a local databank?

    Fabian

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Get an example of your data, place it in a Javascript variable and use data instead of ajax. As Colin said lines 290-294 need to be moved outside the Datatables init code. The selector you have for $('#tbody').on is not correct. My example has $('#example tbody').on. example is the ID of the table. You need to include your table's ID.

    Kevin

This discussion has been closed.