2017-08-10 57 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) –