在动态使用javascript和表格分类器时无法对日期列进行排序

时间:2018-01-06 08:55:10

标签: javascript sorting datetime dynamic tablesorter

动态通过json并将其添加到表中。当我试图通过发布日期对表进行排序时,由于无法使用表分类器插件读取未定义的属性'2',因此会出现错误。这是我的代码..

   <html>
  <head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <Script src="http://tablesorter.com/__jquery.tablesorter.min.js"></Script>
    <script>
        $(function(){
     $('#keywords').tablesorter(); 
  });

    </script>
  <style>
       #keywords thead {
          cursor: pointer;
        background: #c9dff0;
      }
  </style>
 </head>
 <body>
<center style="padding-top:10px;">
  <input type="text" name="keyword" id="keyword" placeholder="playlistid">
  <input type="number" name="max_results" id="max_results" placeholder="max results">
   <button type="submit" name="results" id="results" onclick="searchVideo($('#keyword').val())">Submit</button> <br><br>
 <!-- <textarea id="s_results" rows="10" cols="150"></textarea><br><br>-->
</center>
<table class="table table-bordered sortable" style="width:90%" id="keywords">
<thead>
    <tr>
       <!-- <th>VIDEO INDEX</th>-->
        <th>VIDEO ID</th>
       <th>VIDEO TITLE</th>
        <th>PUBLISHED DATE</th>
        <th>CHANNELNAME</th>
         <th>VIEWS</th>
        <!--<th>CHANNEL SUBSCRIBERS</th>-->
    </tr>
</thead>
    <tbody>
    </tbody>
</table>
<script>
  var apiKey = 'apikey';
   var pageToken = '';
   var numOfResult = 0;
    function searchVideo(keyword) {
      var maxResults = parseInt($('#max_results').val());;
       var type = 'video';
        var separator = "\n";
       $.getJSON('https://www.googleapis.com/youtube/v3/playlistItems?playlistId=' + keyword + '&key=' + apiKey + '&maxResults=50&pageToken=' + pageToken + '&part=snippet&callback=?',function(data){

        var l = data.items.length;
        pageToken = data.nextPageToken;
        numOfResult += l;
        var itemUrl = '';

        for(var i = 0; i < l; i++) {
          if( i == 0) {
            separator = '\n';
          }
          else {
            separator = '\n';
          }

            var position = data.items[i].snippet.position;
            $.getJSON('https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id='+data.items[i].snippet.resourceId.videoId+'&key='+ apiKey + '&callback=?',function(data1){


                 var time = data1.items[0].snippet.publishedAt;
                var date = new Date(time);
                var myDate = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear()+"&nbsp"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();

                $('table tbody').append("<tr><td><a target=\'_blank\' href=\'https://www.youtube.com/watch?v="+data1.items[0].id+"\'>"+data1.items[0].id+"</a></td><td>"+data1.items[0].snippet.title+"</td><td>"+myDate+"</td><td>"+data1.items[0].snippet.channelTitle+"</td><td>"+data1.items[0].statistics.viewCount+"</td></tr>");

            });

        }

        if( numOfResult < maxResults) {
          searchVideo(keyword);
        }

    });
 }
   </script>
    </body>
 </html>

任何人都可以帮我分类。提前致谢 。 Tablesorter插件未检测到表

1 个答案:

答案 0 :(得分:1)

在空表上加载页面后,您已经初始化了表格分类器。

相反,在从YT-API获取数据并将其设置为您的表格之后立即删除该插件并将其初始化

$('table tbody').append(....)
$('#keywords').tablesorter();

对于排序日期,请查看tablesorter演示/示例 - 开箱即用它只能用于标准日期格式; - )