2015-06-29 101 views
1

這裏是columnFilter數範圍過濾器的代碼:數據表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做WHERE animal之間BIRTH_CODE 'BC-01-01' 與 'BC-06-29'

如果用戶輸入

FROM

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

回答

0

過濾是在服務器還是客戶端上完成的?

如果是客戶端,您希望過濾功能最終得到您可以正確排序的內容。所以,如果你願意,你可以去掉的BC- S和parseInt函數的數量...例如

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

然後,你需要去的每一行,並檢查它是否匹配的範圍內,你已經給了它......我認爲這應該起作用。基本上你需要獲得整數,並根據你的範圍進行檢查。此外,該功能不會包含在範圍內,因此我在創建的JSFiddle中更改了它以說明我的意思。