DataTables columnFilter文本范围搜索

时间:2015-06-29 12:27:07

标签: javascript jquery filter datatables

以下是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'和'BC-06-29'

如果用户输入

  

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

1 个答案:

答案 0 :(得分:0)

过滤是在服务器还是客户端完成的?

如果是客户端,您希望过滤功能最终获得可以正确排序的内容。所以,如果你想要,你可以删除BC- s并解析数字...例如

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

然后你需要检查每一行并检查它是否与你给出的范围相匹配......我认为这应该有效。基本上你需要得到整数并根据你的范围检查它们。此外,该功能不会包含该范围,因此我在我创建的JSFiddle中对其进行了更改,以说明我的意思。