[英]Format JQuery Result into HTML Table
Hi I'm Parsing an XML File within my web page through XML like so: 嗨,我正在通过XML解析网页中的XML文件,如下所示:
function loadCards(lang)
{
$.ajax({
type: "GET",
url: 'data.xml',
dataType: "xml",
success: function (xml) { parseDataXml(xml, lang); }
});
}
function parseDataXml(xml, lang)
{
var $xml = $(xml),
name = $xml.find('name[lang="' + lang + '"]').text(),
start = $xml.find('start[lang="' + lang + '"]').text();
end = $xml.find('end[lang="' + lang + '"]').text();
}
My XML File looks like so: 我的XML文件如下所示:
<student id = 1>
<bob>
<name>English</name>
<start>9.00am</start>
<end>11.00am</end>
</bob>
</student>
I have a HTML table, formatted like a Time Table which has 5 Table Headers, and then 5 Table Rows for the Students. 我有一个HTML表格,格式如时间表,其中有5个表格标题,然后有5个表格行供学生使用。 Basically my question is how can I format the results from my JQuery Parsing into the table in order to give a structured view as being new to JQuery/XML I'm finding it hard to implement the parsing results into my table.
基本上,我的问题是如何将来自JQuery解析的结果格式化到表中,以便提供JQuery / XML的新结构化视图,我发现很难将解析结果实现到表中。
Since I believe your XML doesn't make sense, I rewrote it and show how to use that to build a table: 由于我认为您的XML没有意义,因此我重写了它,并展示了如何使用它来构建表:
<students>
<student id="1" name="Bob">
<class name="English" days="MWF" start="9:00am" end="11:00am"/>
<class name="History" days="MWF" start="1:00pm" end="3:00pm"/>
<class name="Math" days="TT" start="9:00am" end="12:00pm"/>
</student>
<student id="2" name="Sally">
<class name="Algebra" days="MWF" start="9:00am" end="11:00am"/>
<class name="Political Science" days="MWF" start="1:00pm" end="3:00pm"/>
</student>
<student id="3" name="Jim">
<class name="Spanish" days="MWF" start="9:00am" end="11:00am"/>
<class name="Calculus" days="MWF" start="1:00pm" end="3:00pm"/>
<class name="History" days="TT" start="9:00am" end="1:00pm"/>
</student>
<student id="4" name="Erin">
<class name="AP English" days="F" start="9:00am" end="3:00pm"/>
</student>
</students>
Note the first var
is only to acquire an XML source (see the fiddle for details): 请注意,第一个
var
仅用于获取XML源(有关详细信息,请参见小提琴):
var $xml = $($('#xml').text()),
$table = $('<table>'),
$rows = $('<tbody>');
$xml.find('student class').each(function ea(){
var $class = $(this),
$student = $class.closest('student');
$rows.append(
'<tr>' +
' <td>' + $student.attr('id') + '</td>' +
' <td>' + $student.attr('name') + '</td>' +
' <td>' + $class.attr('name') + '</td>' +
' <td>' + $class.attr('days') + '</td>' +
' <td>' + $class.attr('start') + '</td>' +
' <td>' + $class.attr('end') + '</td>' +
'</tr>'
);
});
$table
.append(
'<thead>' +
' <tr>' +
' <th>ID</th>' +
' <th>Name</th>' +
' <th>Class</th>' +
' <th>Days</th>' +
' <th>Start</th>' +
' <th>End</th>' +
' </tr>' +
'</thead>'
)
.append($rows)
.appendTo('body');
Gives us: 给我们:
ID Name Class Days Start End
----------------------------------------------------
1 Bob English MWF 9:00am 11:00am
1 Bob History MWF 1:00pm 3:00pm
1 Bob Math TT 9:00am 12:00pm
2 Sally Algebra MWF 9:00am 11:00am
2 Sally Political Science MWF 1:00pm 3:00pm
3 Jim Spanish MWF 9:00am 11:00am
3 Jim Calculus MWF 1:00pm 3:00pm
3 Jim History TT 9:00am 1:00pm
4 Erin AP English F 9:00am 3:00pm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.