简体   繁体   English

jQuery DataTable插件不起作用

[英]jquery DataTable plugin is not working

I'm using jquery datatable plugins to achieve pagination, individual column filter functionalities. 我正在使用jquery数据表插件来实现分页,单个列过滤器功能。 I've added the below files: 我添加了以下文件:

  1. jquery.dataTables.js jquery.dataTables.js
  2. jquery.dataTables.min.css jquery.dataTables.min.css

and my code looks like this: 我的代码如下所示:

  $('#tcExecDetails').DataTable( {
      initComplete: function () {
          this.api().columns().every( function () {
              var column = this;
              var select = $('<select><option value=""></option></select>')
                  .appendTo( $(column.footer()).empty() )
                  .on( 'change', function () {
                      var val = $.fn.dataTable.util.escapeRegex(
                          $(this).val()
                      );

                      column
                          .search( val ? '^'+val+'$' : '', true, false )
                          .draw();
                  } );

              column.data().unique().sort().each( function ( d, j ) {
                  select.append( '<option value="'+d+'">'+d+'</option>' )
              } );
          } );
      }
  } );


 <table id="tcExecDetails"  class="display" style= "width:85%;font-size:11px;">
    <thead>
      <tr class="success">
        <th>Execution Id</th>
        <th>Test Case Name</th>
        <th>Start Time</th>
        <th>Actual End Time</th>
        <th>Planned End Time</th>
        <th>Execution Type</th>
        <th class="status">Status</th>
      </tr>
    </thead>
    <tbody id='tcExecDetailsTable' class="alt_content" >

    </tbody>
  </table>

Here's a sample code for DataTable using your dataset. 这是使用您的数据集的DataTable的示例代码。 You can look at the DataTable Extensions and Plugins 您可以查看DataTable扩展和插件

https://www.datatables.net/extensions/index https://www.datatables.net/extensions/index

https://www.datatables.net/plug-ins/index https://www.datatables.net/plug-ins/index

Code Sample 代码样例

http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"" /> http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css“” />

        $(document).ready(function () {

            try {

                var mydata = [
                                   {
                                       id: 1,
                                       name: "Test 1",
                                       startTime: '11/01/2013 00.00',
                                       actualEndTime:  '11/01/2013 12.00',
                                       plannedEndTime:  '11/01/2013 12.30',
                                       executionTime: 5.00,
                                       status: "Success"
                                   },
                                   {
                                       id: 2,
                                       name: "Test 2",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 11.00,
                                       status: "Failed"
                                   },
                                   {
                                       id: 3,
                                       name: "Test 3",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 33.00,
                                       status: "Failed"
                                   },
                                   {
                                       id: 4,
                                       name: "Test 4",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 27.00,
                                       status: "Failed"
                                   },
                                   {
                                       id: 5,
                                       name: "Test 5",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 32.00,
                                       status: "Failed"
                                   },
                                   {
                                       id: 6,
                                       name: "Test 6",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 5.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 7,
                                       name: "Test 7",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 1.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 8,
                                       name: "Test 8",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 2.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 9,
                                       name: "Test 9",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 8.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 10,
                                       name: "Test 10",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 11.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 11,
                                       name: "Test 11",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '11/08/2015 12.00',
                                       plannedEndTime:  '11/09/2015 12.30',
                                       executionTime: 7.00,
                                       status: "Failed"
                                   }
                                   ,
                                   {
                                       id: 12,
                                       name: "Test 12",
                                       startTime: '11/01/2015 00.00',
                                       actualEndTime:  '1/09/2015 12.00',
                                       plannedEndTime:  '1/9/2015 12.30',
                                       executionTime: 3.00,
                                       status: "Success"
                                   }
                                ];


                $('#tcExecDetails').dataTable(
                                        {
                                            "aaData": mydata,
                                            "aoColumns":
                                            [
                                                { "mData": 'id', sDefaultContent: "" },
                                                { "mData": 'name', sDefaultContent: "" },
                                                { "mData": 'startTime', sDefaultContent: "" },
                                                { "mData": 'actualEndTime', sDefaultContent: "" },
                                                { "mData": 'plannedEndTime', sDefaultContent: "" },
                                                { "mData": 'executionTime', sDefaultContent: "" },
                                                { "mData": 'status', sDefaultContent: "" }
                                            ],

                                            "bPaginate": true,
                                            "bLengthChange": true,
                                            "bFilter": true,
                                            "bSort": true,
                                            "bInfo": true,
                                            "bAutoWidth": false,
                                            "bStateSave": false,
                                            "idisplayLength": 2
                                        });



            }
            catch (err) {
                console.log("Error in operation! " + err);
            }

        });

    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM