2013-01-02 88 views
13

我正在使用數據表,我有以下代碼來生成表。我想顯示讀取,寫入,執行和管理值的複選框。 如果該值等於1,我希望複選框被選中。如果0複選框未選中。如何在jquery.datatables中顯示覆選框?

的Javascript

<script type="text/javascript" charset="utf-8"> 
      $(document).ready(function() { 
       var oTable = $('#example').dataTable({ 
        "sScrollY": "500px",         
        "bPaginate": false, 
        "bProcessing": true, 
        "sAjaxSource": "sources/sample.json" 
       }); 


      }); 
     </script> 

HTML

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> 
        <thead> 
         <tr> 
          <th width="20%">Browser</th> 
          <th width="25%">Read</th> 
          <th width="25%">Write</th> 
          <th width="15%">Execute</th> 
          <th width="15%">Admin</th> 
         </tr> 
        </thead> 
        <tbody> 

        </tbody> 
        </table> 

JSON

{ "aaData": [ 
    ["Trident","0","0","0","1"], 
    ["Trident","0","1","0","0"], 
    ["Trident","0","0","1","1"], 
    ["Trident","0","0","1","1"], 
    ["Trident","0","0","1","1"], 
    ["Trident","0","0","0","0"], 
    ["Gecko","1","1","1","1"], 
    ["Gecko","0","0","0","1"], 
    ["Other browsers","1","0","0","U"] 
] } 
+0

它認爲你正在尋找將內聯控件添加到數據表:[datatable inline controls](http://editor.datatables.net/release/DataTables/extras/Editor/examples/inlineControls.html).also [related ](http://stackoverflow.com/questions/3444339/jquery-datatables-plugin-adding-a-checkbox-dynamically) – zer0bit

回答

22

我能夠得到它使用datables mrenderer

$(document).ready(function() { 
    var oTable = $('#example').dataTable({ 
     "aoColumnDefs": [{ 
      "aTargets": [0], 
      //"mData": "download_link", 
      "mRender": function (data, type, full) { 
       if (data == "Gecko") { 
        return '<a href="' + data + '">' + data + ' Download Gecko</a>'; 
       } else { 
        return '<a href="' + data + '">' + data + ' Download</a>'; 
       } 
      } 
     }, { 
      "aTargets": [1], 
      //"mData": "download_link", 
      "mRender": function (data, type, full) { 
       if (data == "1") { 
        return '<input type=\"checkbox\" checked value="' + data + '">'; 
       } else { 
        return '<input type=\"checkbox\" value="' + data + '">'; 
       } 
      } 
     }, { 
      "aTargets": [2], 
      //"mData": "download_link", 
      "mRender": function (data, type, full) { 
       if (data == "1") { 
        return '<input type=\"checkbox\" checked value="' + data + '">'; 
       } else { 
        return '<input type=\"checkbox\" value="' + data + '">'; 
       } 
      } 
     }, { 
      "aTargets": [3], 
      //"mData": "download_link", 
      "mRender": function (data, type, full) { 
       if (data == "1") { 
        return '<input type=\"checkbox\" checked value="' + data + '">'; 
       } else { 
        return '<input type=\"checkbox\" value="' + data + '">'; 
       } 
      } 
     }, { 
      "aTargets": [4], 
      //"mData": "download_link", 
      "mRender": function (data, type, full) { 
       if (data == "1") { 
        return '<input type=\"checkbox\" checked value="' + data + '">'; 
       } else { 
        return '<input type=\"checkbox\" value="' + data + '">'; 
       } 
      } 
     }], 
     "bFilter": false, 
     "sScrollY": "500px", 
     "bPaginate": false, 
     "bProcessing": true, 
     "sAjaxSource": "sources/sample.json" 
    }); 
}); 
工作
+2

我正在尋找一個關於如何從json顯示覆選框的例子,感謝這個解決方案,我想知道如何在複選框更新後創建保存按鈕以將數據保存回服務器。謝謝。 –