2012-08-10 30 views
0

我正在使用localstorage作为项目,并试图使用tablesorter2 jquery插件对表进行排序。我不断收到以下错误:已从本地存储中添加的排序表行

遗漏的类型错误:无法读取属性“行”的不确定

我有一个提交按钮点击,从localStorage的,我的友好JavaScript控制台认为不添加这些行运行功能存在。

有没有办法调用从localstorage添加的行,而不是原始标记中的行?

下面的代码添加表行:

$(document).ready(function(){ 

    $.extend($.fn, { 
     storeitem: function() { 

      //add a new id for each array 
      $newTeam = $('#team'); 

      //set up unique ids for each array 
      var i = Number(localStorage.getItem('team-counter')) + 1; 

      // create condition to add or loop through new entry 
      if ($newTeam.val() !== "") { 
       // Take the value of the input field and save it to localStorage 
       localStorage.setItem("team-" + i, $newTeam.val()); 

       // Set the to-do max counter so on page refresh it keeps going up instead of reset 
       localStorage.setItem('team-counter', i); 
      } 

      //use serializeArray to simplify array creation 
      // var teaminfo = $("#newTeamForm").serializeArray(); 

      var teaminfo = {}; 
      var givemethat = $.each($('#newTeamForm').serializeArray(), function() { 
       teaminfo[this.name] = this.value; 
      }); 
      //alert(teaminfo.teamname); 
      //$('div.second').replaceWith('<h2>New heading</h2>'); 
      $("#standings tr:last-child").after('<tbody><tr><td>' + teaminfo.teamname + '</td> <td></td><td></td> <td></td></tr></tbody>'); 
      $("#standings").tablesorter(); 
      //$("#standings > tbody:last").after("<tr>" + + "</tr>"); 
      //store data in key value pair and stringify 
      localStorage.setItem("team-" + i, JSON.stringify(teaminfo)); 

      // var neat = localStorage.setItem('teamname') 

      var teamName = JSON.parse(localStorage.getItem("team-" + i)); 

     }//storeitem 

    });//extend 

});//doc ready 

要运行的排序功能,我跑:

$('#standings').tablesorter(); 

当我手动添加表行到我的标记功能的工作,但当使用上面的函数从localstorage中添加它们时。

请让我知道这是否合理。我是一名新开发人员,请原谅模糊不清。

+2

向我们展示您用来插入本地存储的代码,并向我们显示您正在使用的代码,从而产生错误。这些是难题的关键部分,并可能导致更多的人对解决您的问题感兴趣。 – Ohgodwhy 2012-08-10 03:38:46

+0

为什么这个问题在没有特定原因的情况下被修改?这是编程有关和确定,如果有点模糊。 – 2012-08-10 03:58:52

回答

0

最明显的问题,我看到的是,该代码添加新tbody为每它添加一行:

$("#standings tr:last-child").after('<tbody><tr><td>' + teaminfo.teamname + '</td> <td></td><td></td> <td></td></tr></tbody>'); 

这应该是这个样子:

$("#standings tr:last-child").after('<tr><td>' + teaminfo.teamname + '</td> <td></td><td></td> <td></td></tr>'); 

虽然,我不看不到你是如何循环并将新数据添加到空白表格单元格。

缺少的部分是,一旦添加了所有行,就需要更新tablesorter - 请参阅this demo

$("table").trigger("update");