我应该使用jquery中的哪个插件或哪个插件来用xml文件内容填充html表? [英] What or which plugin in jquery shall i use to populate a html table with a xml file content?
问题描述
我需要显示来自服务器的xml文件中的数据(到文件类似files/client.xml
的路径)到html表或datagrid中,我应该使用哪个插件或更确切地说应使用什么插件,以便它具有可变的分页,过滤器和表CSS定制.任何建议都会有所帮助,对我来说,一个小例子应该是一个加分点:)谢谢
I have a requirement to display data from a xml file from server (path to file something like files/client.xml
) into a html table or datagrid, which plugin or rather what should i use so that it has variable pagination, filter and table css customization. Any suggestions would help, a little example should be a plus point for me :) Thanks
注意:我的xml结构是固定的
Note: My xml structure is fixed
<?xml-stylesheet type="text/xsl" href="csmclientiir.xsl"?>
<csmclient product="abc" date="4/26/11 2:05 PM">
<system>
<osname>Linux
</osname>
<hostname>AbhishekNix
</hostname>
<release>2.6.18-128.el5
</release>
<filesystem>
<file mount='/home/hp1' home='(innfs2:/vol/home/shome/home/hp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/par21' home='(innfs2:/vol/home/shome/home/par21)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/h231' home='(innfs2:/vol/home/shome/home/h231)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/avallin1' home='(innfs2:/vol/home/shome/home/avallin1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/park' home='(innfs2:/vol/home/shome/home/park)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/sp1' home='(innfs2:/vol/home/shome/home/sp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/ganga1' home='(innfs2:/vol/home/shome/home/ganga1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
<file mount='/home/nbp1' home='(innfs2:/vol/home/shome/home/nbp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
</filesystem>
</system>
<product>
<showtime>Tue Apr 26 14:05:23 2011
</showtime>
</product>
</csmclient>
更新有效的解决方案
因为它不带属性..像这里我想得到mount
,free
等
这是我在jqGrid中针对上述xml所做的事情.
Since it does not take attribute.. like here i'd like to get mount
,free
etc
Here is what i did in jqGrid for the above xml.
var i=0;
var filesystem=[];
$(xml).find('file').each(function(){
var row={};
row.id=i++;
row.total=$(this).attr('total');
row.free=$(this).attr('free');
row.used=$(this).attr('used');
row.percentage=$(this).attr('percentage');
filesystem.push(row);
});
$('#detailTable').empty();
$('<div width="100%">')
.attr('id','diskUsageSpan')
.html('<div class="titleBlue">Configuration>System>Disk Usage</div>'+
'<table id="list1" width="100%"></table>'+
'<div id="gridpager"></div>'+
'</div>')
.appendTo('#detailTable');
jQuery("#list1").jqGrid({
datatype: "clientSide",
height: 250,
colNames:['id','Total Space','Free Space', 'Used Space', 'Used Percentage'],
colModel:[
{name:'id',index:'id', width:90, align:"right"},
{name:'total',index:'total', width:90, align:"right"},
{name:'free',index:'free', width:90, align:"right"},
{name:'used',index:'used', width:90, align:"right"},
{name:'percentage',index:'percentage', width:120, align:"right"}
],
pagination:true,
pager : '#gridpager',
rowNum:10,
scrollOffset:0,
height: 'auto',
autowidth:true,
viewrecords: true,
gridview: true,
edit:false,
add:false,
del:false
});
for(var i=0;i<filesystem.length;i++)
jQuery("#list1").jqGrid('addRowData',i+1,filesystem[i]);
jQuery("#list1").setGridParam({rowNum:10}).trigger("reloadGrid");
完美工作,谢谢@Tomas和@doctrey
Works perfectly Thanks @Tomas and @doctrey
推荐答案
基本上,您可以使用jQuery选择器来读取XML DOM,就像读取HTML DOM一样.因此,在您的XML示例中,如果您想对每个<file>
元素进行特定的操作-例如,将其mount
属性的内容添加到无序列表中,则可以执行以下操作:
Basically, you can read the XML DOM just as you read the HTML DOM, using jQuery selectors. So in your XML example, if you want to do something specific with each <file>
element - say, add the contents of it's mount
attribute to an unordered list, you could do something like this:
$(xml).('file').children().each(function() {
var fileElem = this; // save the instance for closure
$('ul#theList').append($('<li>').text(fileElem.attr('mount'));
});
您可以使用jQuery的内置AJAX API通过AJAX获取XML内容:
You can get the XML contents with AJAX, using jQuery's built-in AJAX API:
$.ajax({
type: "GET",
url: "your.xml",
dataType: "xml",
success: function(xml) {
// Insert the previous code snippet here
}
});
我从本教程中获得了所有内容,因此可能对您有所帮助你也是.注意:这是Google上第一个针对"jquery xml"的歌曲...
I got all of this from this tutorial, so it might be helpful for you too. Note: This was the very first hit on Google for "jquery xml"...
这篇关于我应该使用jquery中的哪个插件或哪个插件来用xml文件内容填充html表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!