2013-06-21 38 views
5

我使用dataTables jquery。我想將排序圖像添加到列中,圖像shd在排序時也會發生變化。也就是說,如果圖像顯示降序圖標,並且點擊它應該更改爲升序圖標。如何使用dataTables jQuery來完成?dataTables jQuery - 如何添加排序圖像/圖標?

我的代碼:

$("#rates").dataTable({ 
     "bPaginate": false, 
     "sScrollY": "250px", 
     "bAutoWidth": false, 
     "bScrollCollapse": true, 
     "fnInitComplete": function() { 
       this.css("visibility", "visible");}, 
       "bLengthChange": false 
    }); 

回答

6

$(document).ready(function() { 
 
    $("#tblVal").dataTable({ 
 
    "bPaginate": false, 
 
    "sScrollY": "250px", 
 
    "bAutoWidth": false, 
 
    "bScrollCollapse": true, 
 
    "fnInitComplete": function() { 
 
     this.css("visibility", "visible"); 
 
    }, 
 
    "bLengthChange": false 
 
    }); 
 
});
<html xmlns="http://www.w3.org/1999/xhtml"> 
 

 
<head> 
 
    <title>new document</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
 
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script> 
 
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" rel="stylesheet" /> 
 
</head> 
 

 
<body> 
 
    <div id="foo"> 
 
    <table id="tblVal" class="data display datatable"> 
 
     <thead> 
 
     <tr> 
 
      <th>s.no</th> 
 
      <th>ID</th> 
 
      <th>Name</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td>1</td> 
 
      <td>100</td> 
 
      <td>vsa</td> 
 
     </tr> 
 
     <tr> 
 
      <td>2</td> 
 
      <td>101</td> 
 
      <td>asa</td> 
 
     </tr> 
 
     <tr> 
 
      <td>3</td> 
 
      <td>102</td> 
 
      <td>nfsa</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    </div> 
 
</body> 
 

 
</html>

確保u必須添加適當的JS和CSS文件。試試這段代碼對我有用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title> new document </title> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script> 
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#tblVal").dataTable({ 
      "bPaginate": false, 
     "sScrollY": "250px", 
     "bAutoWidth": false, 
     "bScrollCollapse": true, 
     "fnInitComplete": function() { 
       this.css("visibility", "visible");}, 
       "bLengthChange": false 
    }); 
    }); 
</script> 
</head> 
<body> 
<div id="foo"> 
    <table id="tblVal" class="data display datatable"> 
     <thead> 
      <tr> 
       <th>s.no</th> 
       <th>ID</th> 
       <th>Name</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>1</td> 
       <td>100</td> 
       <td>vsa</td> 
      </tr> 
      <tr> 
      <td>2</td> 
      <td>101</td> 
      <td>asa</td> 
      </tr> 
      <tr> 
      <td>3</td> 
      <td>102</td> 
      <td>nfsa</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 
</body> 
</html> 
+0

你在哪裏添加排序圖像? – kishore

+0

我沒有包含CSS。所以這是問題。謝謝 – kishore

3

在項目中包含了images文件夾之後,調整CSS中的鏈接指向你的圖片。在CSS中尋找下面的代碼:

table.dataTable thead .sorting { 
    background-image: url("../images/sort_both.png"); 
}