简体   繁体   English

使用扩展和折叠功能在jQuery数据表中显示XML数据

[英]Displaying XML data in jQuery datatable with Expand and Collapse functionality

I am using jQuery in my project. 我在我的项目中使用jQuery。 I am very new to jQuery and learning the things now. 我是jQuery的新手,现在开始学习。 My requirement is to display XML data in datatables and with expand and collapse functionality. 我的要求是在数据表中显示XML数据,并具有扩展和折叠功能。

My XML will be something like below: 我的XML将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <case>
      <case-column-one>case-one</case-column-one>
      <case-column-two>case-two</case-column-two>
      <issue>
         <issue-column-one>issue-one</issue-column-one>
         <issue-column-two>issue-two</issue-column-two>
      </issue>
   </case>
   <case>
      <case-column-three>case-three</case-column-three>
      <case-column-four>case-four</case-column-four>
      <issue>
         <issue-column-three>issue-three</issue-column-three>
         <issue-column-four>issue-four</issue-column-four>
      </issue>
   </case>
</root>

Each <case> (parent) contains few <issue> tags (children). 每个<case> (父)包含几个<issue>标签(子)。 Upon expanding the <case> , the children <issue> should be opened. 扩展<case> ,应打开子<issue>

I have to render the above XML data in the jQuery datatable and add expand and collapse functionality to it. 我必须在jQuery数据表中呈现以上XML数据,并为其添加展开和折叠功能。 I have gone through the following link and got few things about handling the JSON response. 我浏览了以下链接,并获得了一些有关处理JSON响应的信息。

jQuery datatable expand and collapse functionality jQuery数据表展开和折叠功能

By using the information given in the above link, I am able to render the parent tags <case> , but I am not able render the child tags <issue> under respective parent tags ie <case> . 通过使用上面链接中给出的信息,我可以呈现父标签<case> ,但是不能在相应的父标签即<case>下呈现子标签<issue> <case>

Can anyone help me on this? 谁可以帮我这个事?

The code that I tried is given below: 我尝试的代码如下:

$(".dataTables_scrollBody").find(".details-control").on("click", function() {
  var tr = $(this).closest('tr');
  var row = table.row(tr);
  if (row.child.isShown()) {
    // This row is already open - close it
    row.child.hide();
    tr.removeClass('shown');
  } else {
    // Open this row

    row.child('<h4>hello</h4>').show();
    tr.addClass('shown');
  }
});

I just tried to render a hard coded value hello in place children. 我只是试图呈现一个硬编码值hello到位的儿童。 But in reality I have render <issue> under <case> when it is expanded. 但实际上,展开后,我在<case>下渲染了<issue>

Can anyone help me out? 谁能帮我吗?

I can give you a head start . 我可以给你一个好的开始。 I have taken your sample XML and parse it in JSON Array 我已经获取了示例XML并将其解析为JSON Array

HTML: HTML:

<div id="demo_jui">
    <table id="events" class="display">
            <thead>
                    <tr>
                        <th>Column 1</th>
                        <th>Column 2</th>
                        <th>Issuer</th>

                    </tr>
            </thead>
            <tbody>
            </tbody>
    </table>
</div>

JS: JS:

var thisTable = $("#events").dataTable();

//Simulated data response
var data = {
    xml: "<root><case><case-column-one>case-one</case-column-one><case-column-two>case-two</case-column-two><issue><issue-column-one>issue-one</issue-column-one><issue-column-two>issue-two</issue-column-two></issue></case><case><case-column-three>case-three</case-column-three><case-column-four>case-four</case-column-four><issue><issue-column-three>issue-three</issue-column-three><issue-column-four>issue-four</issue-column-four></issue></case></root>"
}

$.ajax({
    url:"/echo/xml/",
    data:data,
    type:"POST",
    success: function(response){
        var $events = $(response).find("case");

        $events.each(function(index, event){
            var $event = $(event),
                addData = [];

            $event.children().each(function(i, child){
                addData.push($(child).text());
            });

            thisTable.fnAddData(addData);
        });
    }
});

Working Code Here 这里的工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM