I'm trying to improve DataTables search functionality based on this example . It makes the table footer searchable (as a string). I'd like to be able turn string filtering into numeric filtering by prefixing the search input with <
or >
sign.
I'm not good at JS. If there's a quick win here that would be much appreciated.
<script>
$(document).ready(function() {
$('#{{ v.table_id }} tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
var table = $('#{{ v.table_id }}').DataTable(
{
dom: 'Blfrtip',
buttons: ['copy'],
search: {"caseInsensitive": true},
"lengthMenu": [ [12, 24, 48, -1], [12, 24, 48, "All"] ]
}
);
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
let re_gt = /^>/
let re_lt = /^</
var gt = re_gt.test(this.value)
var lt = re_lt.test(this.value)
/* NEED HELP WITH THIS PART */
if ( re_gt.test(this.value) ) {
var min = parseInt( this.value.slice(1), 10 )
that
.filter( function ( value, index ) { return value < min ? false : true; } )
.draw();
}
else if ( re_lt.test(this.value) ) {
var max = parseInt( this.value.slice(1), 10 )
}
/* THIS PART WORKS */
else {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
}
} );
} );
} );
</script>
This seems awkward but it works. It would be cool if I were able to apply two active filters at the same time. Right now, if doing greater/less than filtering on two columns, switching to a second column resets the filter for the first column.
<script>
$(document).ready(function() {
$('#{{ v.table_id }} tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
var table = $('#{{ v.table_id }}').DataTable(
{
dom: 'Blfrtip',
buttons: ['copy'],
order: [[ {{v.sort_col}} ]],
search: {"caseInsensitive": true},
"lengthMenu": [ [12, 24, 48, -1], [12, 24, 48, "All"] ]
}
);
table.columns().every( function () {
var that = this
$( 'input', this.footer() ).on( 'keyup change', function () {
var colnumber = that[0][0] /* got that by inspecting Object.entries(that) in dev mode */
let gtlt = /^[<>]/
if ( gtlt.test(this.value) ) {
var sign = this.value.slice(0,1)
var min = ( sign === '>' ? parseInt( this.value.slice(1), 10 ) || -Infinity : -Infinity )
var max = ( sign === '<' ? parseInt( this.value.slice(1), 10 ) || Infinity : Infinity )
/* console.log(sign, min, max) */
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
var values = parseFloat( data[colnumber] ) || 0
/* console.log(min, max, values) */
if (min == null && max == null) {
return true;
}
if (values >= min && values <= max) {
return true;
}
return false;
}
);
table.draw()
/* reset min/max filters to default, inactive values */
min = -Infinity
max = Infinity
}
else {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
}
} );
} );
} );
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.