Using Checkboxes to sort columns
Using Checkboxes to sort columns
RekhaMani777
Posts: 2Questions: 0Answers: 0
This post is more like a code snippet. I was able to extend the search option to enable check boxes with in the column header and extend search on click
The dynamic search, happens on click and the function looks like this
$('#paginated').DataTable({
"dom": 'B<f<t>ip>',
initComplete: function() {
this.api().columns().every(function() {
var column = this;
var cond=$(column.header()).text();
// alert(cond.length);
if(cond.length >1 )
{
// alert(cond);
if ( cond =='Program ')
{
var ddmenu = cbDropdown($(column.header()).empty())
.on('change', ':checkbox', function() {
var vals = $(':checked', ddmenu).map(function(index, element) {
//alert($(element).val());
return $.fn.dataTable.util.escapeRegex($(element).val());
}).toArray().join('|');
column
.search(vals.length > 0 ? '\W*(' + vals + ')\W*' : '', true, false)
.draw();
//console.log(vals);
// alert(this.value);
if(vals === ""){
$(this).parent().parent().parent().removeClass("factive");
}else{ $(this).parent().parent().parent().addClass("factive");
}
//change callback
// this enables the search
});
// var // wrapped // this section adds the custom check box labels and values
// $label = $('<label>'),
// $text = $('<span>', {
// text: 'All Programs'
// }),
// $cb = $('<input>', {
// type: 'checkbox',
// value: '*',
// checked:true
// });
//
// $text.appendTo($label);
// $cb.appendTo($label);
//
// ddmenu.append($('<li>').append($label));
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: 'check box1'
}),
$cb = $('<input>', {
type: 'checkbox',
value: 'check box value',
checked:true
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: 'check box2'
}),
$cb = $('<input>', {
type: 'checkbox',
value: 'check box value',
checked:true
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: 'check box3'
}),
$cb = $('<input>', {
type: 'checkbox',
value: 'check box value',
checked:true
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: 'check box4'
}),
$cb = $('<input>', {
type: 'checkbox',
value: 'check box value',
checked:true
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
var // wrapped
$label = $('<label>'),
$text = $('<span>', {
text: 'check box5'
}),
$cb = $('<input>', {
type: 'checkbox',
value: 'check box value',
checked:true
});
$text.appendTo($label);
$cb.appendTo($label);
ddmenu.append($('<li>').append($label));
}
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
css for the above
Thanks for posting,
Colin