EasyUI Forum
April 25, 2024, 12:50:05 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: XML data load  (Read 19629 times)
ClSoft
Jr. Member
**
Posts: 92


View Profile
« on: January 16, 2013, 12:25:06 PM »

Hi all
any example of loading XML data into grid and/or treegrid?
Thank you!
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: January 16, 2013, 05:51:03 PM »

Please use 'loadFilter' function to transfer xml data to standard json data.
Logged
ClSoft
Jr. Member
**
Posts: 92


View Profile
« Reply #2 on: January 17, 2013, 12:08:51 AM »

Thanks, but can not find any example.
I have no idea how it should work.
Can you help, please?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 on: January 17, 2013, 01:20:23 AM »

Try the code below:
Code:
$('#dg').datagrid({
  loadFilter: function(xml){
    // do your tansfering
    return {total:0,rows:[]};  // return the json data you want
  }
});
Logged
ClSoft
Jr. Member
**
Posts: 92


View Profile
« Reply #4 on: January 17, 2013, 04:53:37 AM »

Hi
unfortunately, I don't know what do you mean.
I suppose that is JavaScript function?
What means: "do your transfering"?
How XML file should looks like (what structure)?

As you can see, I can not start  Shocked

Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #5 on: January 17, 2013, 06:11:12 AM »

Suggest the xml data to be loaded is:

Code:
<root><people><name>name1</name><address>address1</address></people><people><name>name2</name><address>address2</address></people></root>

To load this xml data, the user must transfer this data to standard json data, so that the datagrid will be able to load it. Here is the example shows how to do this.

Code:
<table class="easyui-datagrid" title="Load XML Data" style="width:300px;height:200px"
data-options="
data: '<root><people><name>name1</name><address>address1</address></people><people><name>name2</name><address>address2</address></people></root>',
loadFilter: function(xml){
var rows = [];
$(xml).find('people').each(function(){
var p = $(this);
var row = {
name: p.find('name').text(),
address: p.find('address').text()
};
rows.push(row);
});
return {total:rows.length,rows:rows};
}
">
<thead>
<tr>
<th data-options="field:'name'">Name</th>
<th data-options="field:'address'">Address</th>
</tr>
</thead>
</table>
Logged
ClSoft
Jr. Member
**
Posts: 92


View Profile
« Reply #6 on: January 17, 2013, 01:07:26 PM »

Thank you so much, but your example "TreeGrid from Table " for my case works better as work with XML.
I have found how to hide some fileds etc. now I have only problem how to use different icons for different rows.
I saw "iconCls" but don't know how to implement it into this:

Code:
	<thead>
<tr>
  <th field="id" hidden="true"></th> 
  <th field="_parentId" hidden="true"></th> 
  <th field="name1" width="600%">Size</th> 
</tr>                         
</thead>                           
<tbody>                           
<tr>                           
<td>1</td>           
<td></td>           
<td>Data 1</td>           
</tr>                         
</tbody>                           
</table>


Thanks for any help.
Logged
ClSoft
Jr. Member
**
Posts: 92


View Profile
« Reply #7 on: January 19, 2013, 12:07:57 AM »

Is it possible that instead of this:

data-options="
   data: '<root><people><name>name1</name><address>address1</address></people><people><name>name2</name><address>address2</address></people></root>',
   loadFilter: function(xml){
...

something like this is used:
   data: 'some link to XML file',

with other words, is it possible to link to XML file instead of using it like above?
Thanks!
Logged
donlaur
Newbie
*
Posts: 2


View Profile Email
« Reply #8 on: December 03, 2013, 08:24:58 AM »

I also answered this on Stack Overflow.

I started with the CRUD datagrid tutorial version so my answer might be a bit different. I started from the CRUD example, converting from the mySQL solution to instead use XML.
http://www.jeasyui.com/tutorial/app/crud2.php

$(function(){
        $('#dg').edatagrid({
            url: 'get_data.php' /* pulls in the data from a xml file  */
        });
    });
My example actually had the read (url), create (saveURL), update (updateURL), and delete (destroyUrl) but for this answer I just put the url to read the data in.

For my get_data.php I converted my XML file into JSON. I also removed a root I had in that XML file.

if(!$xml = simplexml_load_file("mydata.xml", null, LIBXML_NOCDATA)) {
echo "unable to load file";
}
{
$xmlarray = array();  // create a new array
    foreach ($xml as $element) {  // loop through the xml file elements
        array_push($xmlarray, $element);  // push the elements on the xml array
    }
}

echo json_encode($xmlarray);  // encode that json without the root xml element

I am still sticking with simplexml, there are easier ways to do this when you get into removing and updating.
Logged
getk
Newbie
*
Posts: 23


View Profile
« Reply #9 on: March 13, 2015, 12:23:03 AM »

StackOverflow  URL: http://stackoverflow.com/questions/16313651/easyui-xml-data-load-into-datagrid/20354958
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!