开发者

What or which plugin in jquery shall i use to populate a html table with a xml file content?

开发者 https://www.devze.com 2023-03-13 17:19 出处:网络
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 v

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

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' />
    </filesyst开发者_Python百科em>
</system>
<product>
    <showtime>Tue Apr 26 14:05:23 2011
    </showtime>
</product>
</csmclient>

Updating with working solution

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&gt;System&gt;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");

Works perfectly Thanks @Tomas and @doctrey


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'));
});

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
    }
});

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.dataTables is good as long as you have control over the contents of the xml file. Their formatting requirements are rather strict.


You probably want to use XSLT - it will let you take the XML response and directly apply an XSL transform, which can then be inserted into the DOM.

0

精彩评论

暂无评论...
验证码 换一张
取 消