2012-01-18 110 views
2

我解析XML文件到表中,並希望使用jquery tablesorter。現在,我得到了一個不存在的tablesorter的錯誤,我的數據被填充到表中,但是頭文件不能被排序。有任何想法嗎?下面列出JS和HTML代碼。jQuery Tablesorter不排序列

HTML:

<html> 
<head> 
    <title>Read XML</title> 
    <script type="text/javascript" src="jquery-1.7.1.js"></script> 
    <script type="text/javascript" src="jquery.tablesorter.js"></script> 
    <script type="text/javascript" src="custom.js"></script> 
</head> 
<body> 
    <table id="table" border="1"> 
     <thead> 
      <tr> 
       <th>Item #</th> 
       <th>Shape</th> 
       <th>Weight</th> 
       <th>Color</th> 
       <th>Clarity</th> 
       <th>Price($)</th> 
      </tr> 
     </thead> 
     <tbody> 
     </tbody> 
    </table> 
</body> 
</html> 

JS:

$(document).ready(function() { 
$("#table").tablesorter(); 
$.ajax({ 
    type: "GET", 
    url: "tutorial.xml", 
    dataType: "xml", 
    success: parseXml 
}); 
$("#table").trigger("update"); 
}); 

function parseXml(xml) 
{ 
    $(xml).find("diamond").each(function() 
    { 
     $("#table tbody").after("<tr><td>" + $(this).find("id").text() + 
     "</td><td>" + $(this).find("shape").text() + "</td><td>" + $(this).find("weight").text() + 
     "</td><td>" + $(this).find("color").text() + "</td><td>" + $(this).find("clarity").text() + 
     "</td><td>" + $(this).find("price").text() + "</td></tr>"); 
    }); 
} 
+0

這兩個拼寫錯誤是否修復了您的代碼? – rekire 2012-01-18 21:54:35

+0

他們解決了我最後一個關於tablesorter不是函數的問題。 – Brandon 2012-01-18 21:55:43

回答

1

移動這一行:

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

到parseXML方法結束時,和改變.after.append

function parseXml(xml) 
{ 
    $(xml).find("diamond").each(function() 
    { 
     $("#table tbody").append("<tr><td>" + $(this).find("id").text() + 
     "</td><td>" + $(this).find("shape").text() + "</td><td>" + $(this).find("weight").text() + 
     "</td><td>" + $(this).find("color").text() + "</td><td>" + $(this).find("clarity").text() + 
     "</td><td>" + $(this).find("price").text() + "</td></tr>"); 
    }); 
    $("#table").trigger("update"); 
} 
+0

沒有爲我工作。 – Brandon 2012-01-18 21:59:04

+0

您是否嘗試添加編輯,將'.after'更改爲'.append'? – 2012-01-18 22:00:05

+0

將'.after'更改爲'.append'這個技巧......並且將觸發語句移動到了parseXml的末尾。 – Brandon 2012-01-18 22:01:38

2

parseXml的末尾需要$("#table").trigger("update");來告訴tablesorter表已更新。如果它在ajaxRequest返回之前運行,它沒有任何幫助。