2017-08-10 27 views
0

我正在嘗試獲取示例(此線程中的第二個代碼片段Bootstrap - How to sort table columns)運行。我不應該只能將js代碼片段放在HTML中的腳本標記中,還是完全誤解它的工作原理?我也試過https://datatables.net/的一些例子,但是沒有一個可排序的功能出現。任何幫助將不勝感激。無法獲取引導程序4 Datatables示例運行

編輯:添加下面的代碼,現在爲什麼不使用數據表API進行分揀工作

<head> 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet" /> 
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.bootstrap4.min.css" rel="stylesheet" /> 

</head> 
<body> 
<div class="container"> 
    <h1>Bootstrap 4 DataTables</h1> 
    <table id="example" class="table table-striped table-inverse table-bordered table-hover" cellspacing="0" width="100%"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Position</th> 
     <th>Office</th> 
     <th>Age</th> 
     <th>Start date</th> 
     <th>Salary</th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
     <th>Name</th> 
     <th>Position</th> 
     <th>Office</th> 
     <th>Age</th> 
     <th>Start date</th> 
     <th>Salary</th> 
     </tr> 
    </tfoot> 
    <tbody> 
     <tr> 
     <td>Tiger Nixon</td> 
     <td>System Architect</td> 
     <td>Edinburgh</td> 
     <td>61</td> 
     <td>2011/04/25</td> 
     <td>$320,800</td> 
     </tr> 
     <tr> 
     <td>Garrett Winters</td> 
     <td>Accountant</td> 
     <td>Tokyo</td> 
     <td>63</td> 
     <td>2011/07/25</td> 
     <td>$170,750</td> 
     </tr> 
     <tr> 
     <td>Ashton Cox</td> 
     <td>Junior Technical Author</td> 
     <td>San Francisco</td> 
     <td>66</td> 
     <td>2009/01/12</td> 
     <td>$86,000</td> 
     </tr> 
    </tbody> 
    </table> 
</div> 
</body> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/jquery.dataTables.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.13/js/dataTables.bootstrap4.min.js"></script> 
<script> type="text/javascript" 
    $(document).ready(function() { 
    $('#example').DataTable(); 
    }); 
</script> 
+0

我運行的例子,沒有任何問題!你能告訴我們你的代碼嗎?到目前爲止你做了什麼?如果有錯誤(在您的瀏覽器的控制檯中) – moay

+0

我更改了腳本標記在代碼中的位置,現在它可以工作。腳本標籤需要的地方是什麼規則?我正在閱讀其他地方的自動運行js需要去追趕身體? – RB34

+0

把你的JS代碼放在文件末尾是個好習慣。但真正的問題是,一些圖書館使用某些依賴關係,比如dataTable如何使用jQuery,在這種情況下,您必須在dataTable之前導入jQuery,並且您希望使用'dataTable()'函數時,必須先導入該庫,然後才能使用它。 (對不起,我的英語,這不是我的母語) – moay

回答

1
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset = "UTF-8"> 
     <meta http-equiv = "X-UA-Compatible" content = "IE = edge,chrome = 1"> 
     <title>Hello World using Backbone.js</title> 
     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"/> 
     <link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.bootstrap4.min.css" rel="stylesheet"/> 
    </head> 

    <body> 
     <div class="container"> <h1>Bootstrap 4 DataTables</h1> <table id="example" class="table table-striped table-inverse table-bordered table-hover" cellspacing="0" width="100%"> 
     <thead> 
     <tr> 
      <th>Name</th> 
      <th>Position</th> 
      <th>Office</th> 
      <th>Age</th> 
      <th>Start date</th> 
      <th>Salary</th> 
     </tr> 
     </thead> 
     <tfoot> 
      <tr> 
      <th>Name</th> 
      <th>Position</th> 
      <th>Office</th> 
      <th>Age</th> 
      <th>Start date</th> 
      <th>Salary</th> 
      </tr> 
     </tfoot> 
     <tbody> 
      <tr> 
      <td>Tiger Nixon</td> 
      <td>System Architect</td> 
      <td>Edinburgh</td> 
      <td>61</td> 
      <td>2011/04/25</td> 
      <td>$320,800</td> 
      </tr> 
      <tr> 
      <td>Garrett Winters</td> 
      <td>Accountant</td> 
      <td>Tokyo</td> 
      <td>63</td> 
      <td>2011/07/25</td> 
      <td>$170,750</td> 
      </tr> 
      <tr> 
      <td>Ashton Cox</td> 
      <td>Junior Technical Author</td> 
      <td>San Francisco</td> 
      <td>66</td> 
      <td>2009/01/12</td> 
      <td>$86,000</td> 
      </tr> 
     </tbody> 
     </table> 
     </div> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/jquery.dataTables.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.13/js/dataTables.bootstrap4.min.js"></script> 
     <script type="text/javascript"> 
     $(document).ready(function() { 
    $('#example').DataTable(); 
}); 
     </script> 
    </body> 
</html> 
+0

這並沒有提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16988565) –