2013-10-24 57 views
0

目前我有一个脚本,可以在键入后(500毫秒)从数据库获取信息。第一次当我给出参数时,它完全正常工作,但第二次如果我给出另一个参数,它不会做正确的事情。当我点击表格的标题时,它会添加15行,但是我首先删除它们,我的查询限制为5行。对生成的行进行排序tablesorter

如何解决这个问题?

<html> 
    <head> 

     <script> 

      jQuery(document).ajaxStop(function() { 
       jQuery("#myTable").tablesorter(); 
      }); 

      var delay = (function() { 
       var timer = 0; 
       return function(callback, ms) { 
        clearTimeout(timer); 
        timer = setTimeout(callback, ms); 
       }; 
      })(); 

      $(document).on("keypress", "#searchValue", function() { 


       delay(function() { 


        $("#myTable tbody").empty(); 
        var name = $("#searchValue").val(); 
        $.post("<?php echo site_url('project/searchProject'); ?>", 
          { 
           name: name 

          }, 
        function(data, status) { 
         var items = ""; 
         data = $.parseJSON(data); 
         if (data.toString() !== "") { 
          $.each(data, function(index, item) { 
           items += "<tr>" + "<td>" + item.Code + "</td>"; 
           items += "<td>" + item.Description + "</td>"; 
           items += "<td>" + item.ProjectLeader + "</td>"; 
           items += "<td>" + item.AccountManager + "</td>"; 
           items += "</tr>"; 

          }); 
         } 
         $("#myTable tbody").append(items); 


        }); 

       }, 500); 

      }); 

     </script> 
    </head> 


    <body> 
     <input type='text' name='searchValue' id='searchValue'/> 

     <br> 
     <br> 


     <table id='myTable' class='tablesorter'> 
      <thead> 
       <tr> 
        <th>Code</th> 
        <th>Description</th> 
        <th>Project leader</th> 
        <th>Account manager</th> 
       </tr> 
      </thead> 
      <tbody> 

      </tbody> 
     </table> 


    </body> 
</html> 
+0

因此,您在每次请求后将结果追加到tbody。你删除重复的条目或以某种方式合并它们? –

+0

在发出请求之前,我使用.empty()从表中删除了所有行。 – David

回答

0

刚刚发现了tabelsorter的更新。

追加后项目做到这一点:

$("#myTable").trigger("update"); 

不要有错误了。

相关问题