2017-01-23 88 views
1

我使用DataTables jQuery plugin將排序/排序添加到我的表中。我不得不在專欄中添加特殊字符*。當我做到這一點時,排序/排序變得不正確。 As you can see, first value on <code>korteri nr</code> is 1 then 10 and etc.. but there is 2, 3 and others which are on lower position if scroll 正如你所看到的,在korteri nr第一個值是1,那麼10等。但有2,3,另一些是在較低的位置,如果滾動
具有特殊字符的DataTables排序/排序列

我曾嘗試這個代碼實現正確的排序,但它並沒有幫助

$.fn.dataTableExt.oSort['custom-sorting-asc'] = function(a,b) { 
     a = escapeRegExp(a); 
     b = escapeRegExp(b); 
     return (a == b) ? 0 : (a > b) ? 1 : -1; 
    }; 
    $.fn.dataTableExt.oSort['custom-sorting-desc'] = function(a,b) { 
     a = escapeRegExp(a); 
     b = escapeRegExp(b); 
     return (a == b) ? 0 : (a > b) ? -1 : 1; //reverse sorting 
    }; 


    function escapeRegExp(string){ 
     return string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
    } 

    $('.handp-table').DataTable({ 
      searching: false, 
      paging: false,   
      aoColumns: [ 
       { "sType": "custom-sorting" }, //custom sorting 
       null, //default sorting 
       null, 
       null, 
       null, 
       null, 
       null 
      ] 
     }); 

回答

0

找到了解決這個問題的另一種方法。我改變了生成列內容的邏輯。而不是將*添加到應該是此字符的每個數字中,我將類添加到此元素some_class,然後使用CSS pseudo element :after添加*作爲內容。 DataTables不計算CSS,因此它現在正在工作。

0

問題是數據表將字段中的值解釋爲字符串,並按字母順序對它們進行排序。

您可以嘗試使用渲染功能在相應的列:

aoColumns: [ 
       { fnRender: function(data,type,full){ 
         if(type==='sort'){ 
          return data; 
         } 
         return data+'*'; 
        } 
       } 
       null, //default sorting 
       null, 
       null, 
       null, 
       null, 
       null 
      ] 

我假設你從服務器獲取的數據是純整數。如果是這種情況,那麼Datatables將足夠聰明,可以投射和使用整數值進行排序,並使用整數值與級聯的*(即:字符串)進行顯示,過濾等。

由如果你使用的是Datatables 1.10+,我建議你使用新的語法(更清晰易用)

+0

我正在使用'DataTables v 1.10.11'。有一種情況,當我可以使用counter來設置每個內部循環的編號時,但是取決於來自服務器的數據,我應該添加'*'。所以現在就像'if(true)$ counter一樣簡單的連接。 '*''。後端在'PHP'上 – vladja