2016-10-01 26 views
1

我被困在jquery數據表中。如果我們使用jQuery數據表,那麼它默認提供了搜索選項。但問題是,如果我搜索特定的記錄,如果內容不匹配或我發現單個記錄。那麼我需要刪除排序圖標。它會工作,但當我按回空間並刪除搜索內容,然後照常顯示所有記錄。但現在可以排序的圖標是禁用它需要再次啓用,那麼解決方案是什麼。jquery數據表搜索選項不啓用排序圖標

這是函數調用: -

$('#datatable-information').on('draw.dt', function() { 

    disableSortingSearchOption(oTable, 'datatable-information_filter input'); 

這是功能認定中: -

function disableSortingSearchOption(oTables, tableClass) { 

    if (oTables != null) { 
     var rowCount = oTables.fnSettings().fnRecordsDisplay(); 
     { 

      if (rowCount == 0 || rowCount == 1) { 


       var oSettings = oTables.fnSettings(); 

       //Remove sort icon 
       $('.DataTables_sort_icon').remove(); 
       //Remove hand cursor 
       $('.datatable th').css('cursor', 'default'); 
       //Iterate through each column and disable sorting 
       $('.' + tableClass + ' th').each(function (index) { 
        if ((oSettings.aoColumns[index]) != undefined) { 
         oSettings.aoColumns[index].bSortable = false; 
        } 
       }); 
      } 
     } 
    } 
} 

回答

0

這裏有一個簡單的方法來去除表格標題排序箭頭和改變指針爲默認當繪製的表格有1行或更少時。

在多行上,表格排序和指針回到「正常」。

我的解決方案與您提供的代碼完全不同。

function disableSortingSearchOption(oTables) { 

    if (oTables != null) { 
    var colCount = 0; 
    $(oTables).find('th').each(function(){ 
     colCount++; 
    }); 
    //console.log(colCount+" colunms"); 

    var rowCount = 0; 
    $(oTables).find('td').each(function(){ 
     rowCount++; 
    }); 
    rowCount = rowCount/colCount; 
    //console.log(rowCount+" rows"); 

    if (rowCount <= 1) { 
     //Remove hand cursor 
     $(oTables).find('th').css('cursor', 'default'); 

     //Remove sort arrows 
     $(oTables).find('th').removeClass('sorting'); 

    }else{ 
     //Add hand cursor 
     $(oTables).find('th').css('cursor', 'pointer'); 

     //Add sort arrows 
     $(oTables).find('th').addClass('sorting'); 
    } 
    } 
} 

我們需要知道的行量...
要做到這一點,我們先算列數量和整個表的td量。

行數量是td數量除以列數量。
根據這個數字,我們可以在所有的th上添加或刪除sorting類並設置遊標。

請注意,當沒有結果時,仍然有一行顯示「找不到匹配記錄」。但由於在這種情況下只有1個td ......除以柱子數量,我們必須考慮「一條或少於一條線」。
;)

看看這個CodePen

+0

Thank's It work's Perfectly – Sagar

+0

;)你會接受答案嗎? –

+0

是的很明顯我接受它 – Sagar