【发布时间】:2018-08-28 20:42:12
【问题描述】:
我正在使用 dataTables 在我的 HTML 表格中搜索数据。
当我尝试按数字过滤时出现问题。这些数字已格式化,因此不会按应有的方式过滤。
一个例子:
在表格中,我有一个包含“123,456”(而不是“123456”)的单元格。
如果我在搜索框中输入“1234”,它将过滤掉这个数字。如果我输入“3,456”,它会找到我要查找的内容。
我尝试过格式化输入的数字,但效果不够好(它会将“1234”格式化为“1,234”,因此找不到“123,456”,因为逗号放错了位置。另一个例子- 如果我要查找“50”,它会过滤掉 5,000):
$( "#activity_table_filter input" ).keydown( function(){
var current_value = $( "#activity_table_filter input" ).val();
current_value = current_value.replace(/,/g, ''); //remove any commas from current value.
var key = event.keyCode;
var entered_value = String.fromCharCode( (96 <= key && key <= 105) ? key - 48 : key ); //charCode doesnt really get numpad numbers
if( !isNaN( current_value ) && current_value != '' && !isNaN( entered_value ) ){ //if current and entered value are numeric, and current value is not an empty string
if( current_value % 1 === 0 && current_value.length >= 3 ){ //if the number is an integer and has 3 digits
current_value = current_value + '0'; //add a last char
var formatted_value = parseInt(current_value).toLocaleString(); //format to beautiful
formatted_value = formatted_value.toString(); //Convert to string
formatted_value = formatted_value.slice(0,-1); //remove added '0' from the end
$( "#activity_table_filter input" ).val( formatted_value ); //change value of input to formatted input.
}else{ //if the number is a float
}
}
});
【问题讨论】:
-
请分享您到目前为止尝试过的代码
-
已添加,但我真的看不出它有什么帮助。如果您要查找 50,它不会找到写在某处的 5,000。
标签: javascript jquery html filter datatables