ajax xml display with master-detail relationship

ajax xml display with master-detail relationship

smeca50smeca50 Posts: 15Questions: 3Answers: 0

Through this jQ bootstrap 4 datatable grid I want for tabular display the following xml file:

<?xml version="1.0" encoding="UTF-8"?>
  <fname>Smith</fname>  <!-- first name-->
  <lname>Milton</lname> <!-- last name --> 
  <addrss>5th summer st, mntb</addrss>
  <addrss>19th Penfield ave, brtcl</addrss>
  <addrss>34th Mansfield st, sgtp</addrss>
  <addrss>12th Peel st, pnslv</addrss>
  <addrss>27th north st, cnda</addrss>

but with master-detail functionality as it is shown over the dt's link:
Only that up to that fiddle there is .json data ! (and I need for .xml display)
The corresponding (almost functional) .js (through the master table I only want to display the following table headers:
fname, lname, age, city while the rest would be hidden; they'll only be displayed as related child table lines) is as follows:

 var prstbl = $("#prsns").dataTable(columns: [{"class":'details-control', "orderable":false, "data":null, "defaultContent":''},
              {dtaTbl:"e-mail", "visible":false},
              {dtaTbl:"phnmbr", "visible":false},
              {dtaTbl:"addrss", "visible":false}
     oTable = $('#prsne').DataTable()

 function format(d)
  return '<table cellpadding="3" cellspacing="0" border="0" style="padding-left:40px">'
    + '<tr>' + '<td>e-mail addrss</td>'
    + '<td>' + d.email + '</td>' + '</tr>'
    + '<tr>' + '<td>phn nmber</td>'
    + '<td>' + d.phnmbr + '</td>' + '</tr>'
    + '<tr>' + '<td>address</td>'
    + '<td>' + d.addrss + '</td>' + '</tr>'
    + '</table>'

 $.ajax({type:"GET", url:"persns.xml", dataType:"xml", success:
   var prsns = $(xml).find("prsn")
   prsns.each(function(idx, prs)
             var prs = $(prs), dtaTbl = [] 

 $('#prsns tbody').on('click', 'td.details-control', function()
             var tr = $(this).closest('tr'),
                 row = oTable.row(tr) 
             if(row.child.isShown()) // if the row is already expanded, collapse it
             else // if collapsed row, expandit it 

The relevant .html part is like this:

 <h5>Master-detail persons display</h5>
 <table id="prsns" border="1" class="table display" width="60%">
 <thead><tr><th></th><th>frst nme</th><th>name</th><th>age</th><th>city</th> <th>e-mail addrss</th></tr></thead> 

And there is also a small .css file like this:

background:url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
tr.shown td.details-control
{ background:url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;

I repeat, all the displaying I need to be done regarding this .xml file and not .json or only .html !
I know how to do it with .json table.
So you guys please help me with this issue.
Thank you all in advance

This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 8,931Questions: 25Answers: 2,040
    Answer ✓

    Looks like the $.ajax({type:"GET", url:"persns.xml".... request is parsing the XML into an array called dtaTbl. Looks to me like this is an array of arrays not objects. The Data Sources doc explains how to handle the different structures.

    You have {dtaTbl:"fname"}, which is not a Datatable option. If your data from the above ajax call where added as objects then you would use columns.data, like this {data:"fname"},. Your prs.children().each(function(j,chdnd) loop should be changed to create an array of objects to match the rest of your config.


  • smeca50smeca50 Posts: 15Questions: 3Answers: 0

    Ok I got it. I'll double check the doc and eventually i'll try correct my code accordingly ..
    Thank you for this tip.

Sign In or Register to comment.