2012-03-30 85 views
2

我想在數據表的頭部建立一些固定的行。固定行在數據表中排序

這是我的數據表設置:

var oTable = $('#transactions').dataTable({ 
    "aaSorting": [ [0,'desc'] ], 
    "bFilter": false, 
    "bSort": true, 
    "aaSorting": [[3,'desc']], // default search colums 
    //   "aaSortingFixed": [[3,'desc']], 
    "bPaginate": true, 
    "bProcessing": true, 
    "sPaginationType": "full_numbers", 
    "asStripeClasses": [ 'monitoring-table-new' ], 
    "bAutoWidth": false, 
    "aoColumns": [ 
     { "sType": "custom", 
      "sClass": "td-date-size-cell", 
      "fnRender": function (oObj, sVal) { 
       return '<div class="monitoring-head-new leadencolor"><div class="form-border"><span class="date"><em>' + sVal + '</em></span></div></div>'; 
      } 
     }, 
     { "sType": "custom", 
      "sClass": "td-transaction-size-cell", 
      "fnRender": function (oObj, sVal) { 
       return '<div class="monitoring-head-new leadencolor"><div class="form-border"><span class="transaction"><em>' + sVal + '</em></span></div></div>'; 
      } 
     }, 
     { "sType": "custom", 
      "sClass": "td-client-size-cell", 
      "fnRender": function (oObj, sVal) { 
       return '<div class="monitoring-head-new leadencolor"><div class="form-border"><span class="client"><div>' + sVal + '</div></span></div></div>'; 
      } 
     }, 
     { "sType": "custom", 
      "sClass": "td-value-size-cell", 
      "fnRender": function (oObj, sVal) { 
       return '<div class="monitoring-head-new leadencolor"><div class="form-border"><span class="value"><em>' + sVal + '</em></span></div></div>'; 
      } 
     }, 
     { "sType": "custom", 
      "sClass": "td-status-size-cell", 
      "fnRender": function (oObj, sVal) { 
       return '<div class="monitoring-head-new leadencolor"><div class="form-border"><span class="status"><div>' + sVal + '</div></span></div></div>'; 
      } 
     }, 
    ], 
    "sAjaxSource": '<?php echo url_for('@test?sf_format=json'); ?>', 

}); 

我在以下方式進行:

jQuery.fn.dataTableExt.oSort['custom-asc'] = function(x,y) { 
     if (x.indexOf("MY VALUE") != -1) return -1; // keep this row at top 
     if (y.indexOf("MY VALUE") != -1) return 1; // keep this row at top 

     return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
    }; 

    jQuery.fn.dataTableExt.oSort['custom-desc'] = function(x,y) { 
     if (x.indexOf("MY VALUE") != -1) return 1; // keep this row at top 
     if (y.indexOf("MY VALUE") != -1) return -1; // keep this row at top 

     return ((x < y) ? 1 : ((x > y) ? -1 : 0)); 
    }; 

這將保持在榜首位置其中有「我的價值」,在文本的行。但問題是,當我在其他列上排序時,「固定」行不在表格頂部。

任何解決方案?

+0

你能上的jsfiddle或jsbin張貼的例子嗎? – 2012-03-30 09:00:06

+0

這裏是jsbin中的代碼示例 - http://jsbin.com/eroyac/2 – 2012-04-02 08:09:27

回答

2

這是不容易的,有一兩件事你可以做的是混合您的自定義與custom datasource sorting THA排序您提供個性化的數據源

// var oTable; 
var onTopValues = new Array("Litige", "5410"); 

/* disable asc sorting for specific rows */ 
jQuery.fn.dataTableExt.oSort['custom-asc'] = function(x,y) { 

     if (isOnTop(x)) return -1; // keep this row at top 
     if (isOnTop(y)) return 1; // keep this row at top 

     return ((x < y) ? -1 : ((x > y) ? 1 : 0)); 
}; 

jQuery.fn.dataTableExt.oSort['custom-desc'] = function(x,y) { 
     if (isOnTop(x)) return -1; // keep this row at top 
     if (isOnTop(y)) return 1; // keep this row at top 

     return ((x < y) ? 1 : ((x > y) ? -1 : 0)); 
}; 

function isOnTop(s) { 
     // search for on top values 
     for (var i = 0; i < onTopValues.length; i++) { 
      if (s === onTopValues[i]) { 
      return true; 
      } 
     } 

     return false; 
} 
//custom datasource 
$.fn.dataTableExt.afnSortData['custom-ontop'] = function (oSettings, iColumn) 
{ 
    var aData = []; 
    //prepare the data 
    $('td:eq('+iColumn+')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function(){ 
    //If the last column of this row should stay on top, return the value of the 
    //last row as datasource 
    if(isOnTop($(this).closest('tr').find('td:last').text())){ 
     aData.push($(this).closest('tr').find('td:last').text()); 
    }else{ 
     //otherwise return this row as datasource 
     aData.push($(this).text()); 
    } 
    }); 

    return aData; 
}; 


$(document).ready(function() { 

      /* Apply custom classes to table headers */ 
     $.fn.dataTableExt.oStdClasses.sSortAsc = "custom_asc"; 
     $.fn.dataTableExt.oStdClasses.sSortDesc = "custom_desc"; 


    oTable = $('#transactions').dataTable({  
     "aoColumnDefs": [ { aTargets: [0,1,2,3], sType: "custom"}] , 
     "aoColumns": [ 
       { "sSortDataType": "custom-ontop" }, 
       { "sSortDataType": "custom-ontop" }, 
       { "sSortDataType": "custom-ontop" }, 
       { "sSortDataType": "custom-ontop" }, 
       { "sSortDataType": "custom-ontop" } 
      ], 
     "aaSorting": [], 
     "aaData": [ 
      [ "2012-01-2", "2", "Local <br>0312313213", 20, "Litige" ], 
      [ "2012-03-22", "25", "Local <br>35313313213", 5540, "Valid" ], 
      [ "2012-01-2", "2", "Local <br>0312313213", 10, "Valid" ], 
      [ "2012-03-22", "25", "Local <br>35313313213", 5410, "Valid" ] 
     ] 
    });  
}); 

基本上你仍在使用自己的排序功能,但你通過他們的自定義數據源是力量值保持在最高。這假設你正在對最後一列進行排序,但你可以添加你的邏輯。

http://jsbin.com/eroyac/4/edit#preview

+0

是的,這是做到這一點的方式 - 在DataTable中,所有行都可以排序,不能'凍結'到特定行(即始終位於0),除非您自定義排序。因此,要改變行的排序,您需要根據需要對分類功能進行關聯。 sType和一個自定義排序函數在大多數情況下可能已經足夠,但您可以使用aaSortingFixed,aDataSort和其他各種選項來幫助排序。 – 2012-04-02 09:57:19

3

只是不排序。使用tfoot像這樣 -

<thead> ... </thead> 
<tfoot style="display: table-row-group"> ... </tfoot> 
<tbody> ... </tbody> 
0

我有一個簡單的解決方案。

我在表中創建第二tbody和正義之舉,我需要在堅持另一個tbody像這樣的所有行:

initComplete: function() { 
    var self = this; 

    var api = this.api(); 
    api.rows().eq(0).each(function(index) { 
     var row = api.row(index); 
     if (row.data()...) { // condition here 
      var $row_to_persist = $(row.node()); 
      var $clone = $row_to_persist .clone(); 

      $(self).find('tbody:last').append($clone); 

      $total_row.remove(); 
      row.remove(); 
      api.draw(); 
     } 
    }); 
} 
1

這樣我可以鎖定第一行,排序僅影響在這之後排。查看此網頁的其他選項:http://datatables.net/reference/option/orderFixed

$('#budgytbl').dataTable({ 
 
    "orderFixed": [ 0, 'asc' ] 
 
})
<table id="budgytbl"... 
 

 
<tbody> 
 
    <tr> 
 
    <td></td> 
 
    <td id="input-name"></td> 
 
    <td id="input-parent"></td> \t 
 
    </tr> 
 

 
    @foreach ($categories as $category) 
 
    <tr> 
 
    ... 
 
    </tr> 
 
    @endforeach 
 
...