I'm confused about how to get a value from the table
I'm confused about how to get a value from the table
rslevy
Posts: 5Questions: 0Answers: 0
I am relatively new to jQuery and very new to this plugin. I have created a table using Ajax and I want to be able to click on a cell and use the value in that cell. I have read that I need to use the fnGetData but I can't figure out how to code it so that it works. Every place I try to put the code doesn't work. My code for creating the data is:
$(document).ready(function(){
$.extend( $.fn.dataTable.defaults, {
"bFilter": false,
"bSort": true,
"iDisplayLength": 25
});
$('#example').dataTable({
"bProcessing": true,
"sAjaxSource": 'CreateJSON.asp'
});
});
Where do I put this code:
// Individual cell data
$(document).ready(function() {
oTable = $('#example').dataTable();
oTable.$('td').click( function () {
var sData = oTable.fnGetData( this );
alert( 'The cell clicked on had the value of '+sData );
} );
} );
$(document).ready(function(){
$.extend( $.fn.dataTable.defaults, {
"bFilter": false,
"bSort": true,
"iDisplayLength": 25
});
$('#example').dataTable({
"bProcessing": true,
"sAjaxSource": 'CreateJSON.asp'
});
});
Where do I put this code:
// Individual cell data
$(document).ready(function() {
oTable = $('#example').dataTable();
oTable.$('td').click( function () {
var sData = oTable.fnGetData( this );
alert( 'The cell clicked on had the value of '+sData );
} );
} );
This discussion has been closed.
Replies
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "delservi.php",
"sPaginationType": "full_numbers",
"aoColumnDefs": [
{ "bSearchable": false, "bVisible": false, "aTargets": [ 2 ] }
]
} ),
oTable = $('#example').dataTable();
oTable.$('td').click( function () {
var sData = oTable.fnGetData( this );
alert( 'The cell clicked on had the value of '+sData );
} );
}
);
Thanks for your help!
Use a delegated event:
[code]
$('#example tbody').on( 'click', 'td', function (e) {
...
} );
[/code]
Allan
$(document).ready(function(){
$.extend( $.fn.dataTable.defaults, {
"bFilter": false,
"bSort": true,
"iDisplayLength": 25
});
$('#example').dataTable({
"bProcessing": true,
"sAjaxSource": 'CreateJSON.asp'
});
});
Thanks!
and this displays the row number. It worked fine. It is important to note that the row number refers to actual display order, no idea what data you are accesing to. You'll need some extra data like an identifier.
Thanks a lot!!!!
Thanks.
$('#example tbody').on( 'click', 'td', function (e) {
alert( 'Cell value clicked on: '+this.innerHTML );
} );
[/code]
Live example: http://live.datatables.net/oniyis/edit#javascript,html
Allan
$(document).ready(function(){
$.extend( $.fn.dataTable.defaults, {
"bFilter": false,
"bSort": true,
"iDisplayLength": 25
});
$('#example').dataTable({
"bProcessing": true,
"sAjaxSource": 'CreateJSON.asp'
});
});
Record #
Last Name
First Name
The code, as is, creates the table correctly and that all works fine. I want to be able to click on any row and have the page display the value in the first column of that row in a pop-up box.
Thanks.
Allan
Allan