how can I get value of td at specific row?
how can I get value of td at specific row?
gnomx
Posts: 14Questions: 2Answers: 0
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
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
#compile
- do you have an element withid="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 rowrow().data()
.If you link to a test case showing the issue, I'll be able to help further.
Allan
but if I use clause
$(this)
it doesn't referred at that specific row?Yes I've id inside each TR
Maybe use jQuery closest(), like this
$(this).closest('tr)
.Kevin
I've already tried and didn't work. I don't what call server to retrieve information. I want get it from table.
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
How can I do it?
Are you asking how to create a test case?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
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
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>
Thanks a lot kevin. I'll try it tomo.
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?
Use
cell().node()
.Kevin
No works...td isn't in the DOM but is in the cache.
That is why you use
cell().node()
, like this:http://live.datatables.net/virikuro/1/edit
Kevin
No works... I'm sorry
I leave it not hidden
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
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()