Select row using a checkbox
Select row using a checkbox
Daniel Codrea
Posts: 2Questions: 0Answers: 0
Hi,
I am new to jQuery and to DataTables, and for several days I've been trying to figure out how to solve my problem.
I have a table in which a column contains check boxes.
I use the following code to select all check boxes:
[code]
$('#check_all').click( function() {
$('input', oTable.fnGetNodes()).attr('checked',this.checked);
if ( $('#orders tr').hasClass('row_selected') )
$('#orders tr').removeClass('row_selected');
else
$('#orders tr').addClass('row_selected');
} );
[/code]
As seen, when the check box is "checked" each row gets a class"row_selected". What I want is to be able to set this function to a single check box, as well. I mean, to be able to add the "row_selected" class to a table row when a check box is "checked" or to remove the class when the check box is unchecked.
I tried several ways but none worked.
Can anyone show me in the right way to do this?
Thanks,
Daniel
I am new to jQuery and to DataTables, and for several days I've been trying to figure out how to solve my problem.
I have a table in which a column contains check boxes.
I use the following code to select all check boxes:
[code]
$('#check_all').click( function() {
$('input', oTable.fnGetNodes()).attr('checked',this.checked);
if ( $('#orders tr').hasClass('row_selected') )
$('#orders tr').removeClass('row_selected');
else
$('#orders tr').addClass('row_selected');
} );
[/code]
As seen, when the check box is "checked" each row gets a class"row_selected". What I want is to be able to set this function to a single check box, as well. I mean, to be able to add the "row_selected" class to a table row when a check box is "checked" or to remove the class when the check box is unchecked.
I tried several ways but none worked.
Can anyone show me in the right way to do this?
Thanks,
Daniel
This discussion has been closed.
Replies
most certainly there is a better way to do this but, anyway, for now, I am satisfied that it works as expected.
here is the code, maybe it helps someone:
[code]
$(document).ready(function() {
$('#orders tr').click( function() {
if ( $(this).hasClass('row_selected') )
$(this).removeClass('row_selected');
else
$(this).addClass('row_selected');
} );
$('#check_all').click( function() {
$('input', oTable.fnGetNodes()).attr('checked',this.checked);
if ( $('#orders tr').hasClass('row_selected2') )
$('#orders tr').removeClass('row_selected2 , row_selected');
else
$('#orders tr').addClass('row_selected2');
} );
oTable = $('#orders').dataTable( );
} );
[/code]