0
我正嘗試從XML文件創建動態HTML表格。在字母表的每個字母下有幾個主要的「章節」。我想每個字母下將生成表格,類似這種格式:使用jQuery從XML文檔創建表格
Code Description
Q87.1 Some description
F44.4 Another description
...
這裏XML文件的樣子:
<index>
<letter>
<title>A</title>
<mainTerm>
<title>Some description</title>
<code>Q87.1</code>
</mainTerm>
<mainTerm>
<title>Another description<nemod>(-some) (detail)</nemod></title>
<code>F44.4</code>
</mainTerm>
<mainTerm>
<title>A more detailed term</title>
<seeAlso>similar terms</seeAlso>
<term level="1">
<title>acute</title>
<code>R10.0</code>
</term>
<term level="1">
<title>angina</title>
<code>K55.1</code>
</term>
</mainTerm>
</letter>
.....
</index>
而且這裏是jQuery代碼我通過創建表與ID =「內容」一個div:
$(document).find("letter").each(function(){
// Define letters variable
$letters = $(this).find('> title').text();
//Find descendants of each letter
$codes = ($(this).find('code').text());
$desc = ($(this)).find('mainTerm title').text();
//Build table
$("#content").append(
'<table border="1"><tr><th>Code</th><th>Description</th></tr><tr><td> '+$codes+' </td><td> '+$desc+' </td></tr></table>'
);
(end brackets for the above)
我得到的輸出是隻有一行對每個字母/章節,所有的碼錶,所有的描述集中一起在一個單元中。
我需要的是一個動態表,其中每行都有一個單獨的代碼及其相應的描述。我怎麼做?
非常感謝,@Bob塵!除了一個重要的細節之外,我明白了它的作用:XML結構偶爾會有更深的節點,我也想將它們分解成它們出現的單元格中的某種子表。例如,如果你看看上面結果中的最後一行,我們會得到「R10.0K55.1更詳細的termacuteangina」。我希望能夠將其顯示爲類似的表格格式,可能嵌套在顯示「更詳細的術語」的單元格中。不知道我是否確實如此清楚,但我充滿希望:-) – Cucurigu
我明白了。讓我試着處理更深層次的問題。 –
@Cucurigu,回覆更新 –