2012-01-18 14 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

2つのタイプミスがあなたのコードを固定していますか? – rekire

+0

彼らは関数ではないtablesorterの最後の質問から問題を解決しました。 – Brandon

答えて

1

移動このライン:parseXMLメソッドの最後に

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

、および変更.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

+0

編集内容を追加しましたが、 '.after'を' .append'に変更しましたか? '.append'へ –

+0

変更' .after'は...トリックをやったので、parseXmlの最後にトリガー文を移動しました。 – Brandon

2

あなたは、テーブルが更新されたことを伝えるためにtablesorter parseXmlの終わりに$("#table").trigger("update");を必要としています。これは、ajaxRequestが返される前に実行されている場合は役に立ちません。

関連する問題