【问题标题】:how to use jquery datatable plugin properly如何正确使用 jquery 数据表插件
【发布时间】:2017-02-06 13:55:26
【问题描述】:

下面是我的示例代码,但它不起作用。有人可以帮助我如何使用这个 jquery 数据表插件吗? 这个数据表插件可以用在sql结果字段表上吗?谢谢

    <html>
    <head>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"> 
    </head>

    <body>
       <table id="example"  style="border:1px solid black;">   
            <thead>


   <tr>
                <th>Column 1</th>         
                <th>Column 2</th>          
                <th>etc</th>       
            </tr>   
        </thead>

        <tbody> 
            <tr>       
                <td>Row 1 Data 1</td>
                <td>Row 1 Data 2</td>   
                <td>etc</td>   
            </tr>
            <tr>
                <td>Row 2 Data 1</td>
                <td>Row 2 Data 2</td> 
                <td>etc</td>  
           </tr>
            <tr>
                <td>Row 2 Data 1</td>
                <td>Row 2 Data 2</td> 
                <td>etc</td>  
           </tr>
       </tbody>
</table>
</body>

<script type="text/javascript">
    $(document).ready(function () {
        $('#example').dataTable();
    });
</script>

</html>

【问题讨论】:

  • 它不应该做什么?
  • 它只显示基本的 html yable,而不是数据表

标签: javascript jquery html datatables


【解决方案1】:

来自文档:https://datatables.net/manual/installation

要使 DataTables 能够增强 HTML 表格,表格必须是 有效的、格式良好的 HTML,带有标题 (thead) 和正文 (tbody)。 也可以使用可选的页脚 (tfoot)。

尝试将thead 添加到具有正确列数的表中。

它应该看起来像这样,但是有你需要的行和列,并且你的td 充满了你的内容。

<table>
    <thead>
        <tr>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
<table>

【讨论】:

  • 我已经更新了我的问题。在表中添加了 thead 和 tbody。仍然显示基本的 html 表。请帮忙
  • 使用相同的代码/html,对我来说效果很好。 jsfiddle.net/guanzo/ay8pwcp5/2
  • 我在 WordPress 中使用它。我正在从 sql server 获取数据并填充到 html 表中。为此,我需要一个数据表,但它不起作用。我需要在我的网站文件夹中添加任何 jquery 吗?
  • 我不了解 WordPress,但您需要 3 个文件才能完成这项工作。 jquery.js、datatables.js、datatables.css,就这些。
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多