gpt4 book ai didi

javascript - 数据表列过滤器文本范围搜索

转载 作者:行者123 更新时间:2023-11-28 07:13:06 25 4
gpt4 key购买 nike

这里是columnFilter数字范围过滤器的代码:

   function fnCreateRangeInput(oTable) {

//var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
th.html(_fnRangeLabelPart(0));
var sFromId = oTable.attr("id") + '_range_from_' + i;
var from = $('<input type="text" class="number_range_filter form-control" id="' + sFromId + '" rel="' + i + '"/>');
th.append(from);
th.append(_fnRangeLabelPart(1));
var sToId = oTable.attr("id") + '_range_to_' + i;
var to = $('<input type="text" class="number_range_filter form-control" id="' + sToId + '" rel="' + i + '"/>');
th.append(to);
th.append(_fnRangeLabelPart(2));
th.wrapInner('<span class="filter_column filter_number_range form-control" />');
var index = i;
aiCustomSearch_Indexes.push(i);



//------------start range filtering function


/* Custom filtering function which will filter data in column four between two values
* Author: Allan Jardine, Modified by Jovan Popovic
*/
//$.fn.dataTableExt.afnFiltering.push(
oTable.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
if (oTable.attr("id") != oSettings.sTableId)
return true;
// Try to handle missing nodes more gracefully
if (document.getElementById(sFromId) == null)
return true;
var iMin = document.getElementById(sFromId).value * 1;
var iMax = document.getElementById(sToId).value * 1;
var iValue = aData[_fnColumnIndex(index)] == "-" ? 0 : aData[_fnColumnIndex(index)] * 1;
if (iMin == "" && iMax == "") {
return true;
}
else if (iMin == "" && iValue <= iMax) {
return true;
}
else if (iMin <= iValue && "" == iMax) {
return true;
}
else if (iMin <= iValue && iValue <= iMax) {
return true;
}
return false;
}
);
//------------end range filtering function



$('#' + sFromId + ',#' + sToId, th).keyup(function () {

var iMin = document.getElementById(sFromId).value * 1;
var iMax = document.getElementById(sToId).value * 1;
if (iMin != 0 && iMax != 0 && iMin > iMax)
return;

oTable.fnDraw();
fnOnFiltered();
});


}

我如何编辑它,以便它过滤的不是数字范围而是文本范围?就像我们在 sql 中说的那样

SELECT * FROM animal WHERE BIRTH_CODE BETWEEN 'BC-01-01' AND 'BC-06-29'

如果用户输入

FROM: BC-01-01 TO: BC-06-29

最佳答案

过滤是在服务器端还是客户端进行?

如果是客户端,您希望过滤功能最终得到可以正确排序的内容。因此,如果您愿意,您可以去掉 BC- 并 parseInt 数字...例如

var bcInt = parseInt(data.replace(/\D/g, ''), 10);

然后你需要遍历每一行并检查它是否与你给定的范围匹配......我认为这应该可行。基本上,您需要获取整数并根据您的范围检查它们。另外,该函数不会包含该范围,因此我在 JSFiddle 中对其进行了更改我创建是为了说明我的意思。

关于javascript - 数据表列过滤器文本范围搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31115708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com