简体   繁体   English

使用Ajax将HTML加载到Datatable子行中

[英]Load html into Datatable child row using ajax

I am trying to load the html returned from a controller into a div when the user clicks on the row of a table. 当用户单击表的行时,我试图将从控制器返回的html加载到div中。

Here is my code: 这是我的代码:

   // Add event listener for opening and closing details
   jQuery('#exRowTable tbody').on('click', 'td.details-control', function () {
     var tr = $(this).closest('tr');
     var row = exRowTable.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(format()).show();
                    tr.addClass('shown');
                    loadChildRow(row);
                }
            });


 function format() {
            return '<div id="detailsDiv"></div>';
        };

        function loadChildRow(row) {
            $.ajax({
                type: 'GET',
                url: '@Url.Action("ChildRow","Users",new{ id= 1})',
                data: null,
                success: function (data) {
                   $('.detailsDiv').innerHTML = data;                        
                }
            });
        };

However this is not actually inserting the html properly. 但是,这实际上并未正确插入html。

I think it could have something to do with the div being added dynamically to the dom. 我认为这可能与将div动态添加到dom有关。

What am I doing wrong? 我究竟做错了什么?

Also, I would some how like to pass the row var into the `loadChildRow()1 function and then be able to select only from divs within that row. 另外,我想知道如何将row var传递到`loadChildRow()1函数,然后仅从该行内的div中进行选择。 any idea how i can do this? 任何想法我该怎么做?

As I can see in your code, you are adding a div with an id="detailsDiv" in the format function. 正如我在您的代码中看到的那样,您将在format函数中添加一个id为“ detailsDiv”的div。 However, when adding the html after ajax call, you are using ".detailsDiv", which is trying to find a class (not id) with name "detailsDiv" (that probably does not exist in your page). 但是,在ajax调用后添加html时,您使用的是“ .detailsDiv”,它试图查找名称为“ detailsDiv”的类(不是id)(您的页面中可能不存在)。 This may be one of the reason for the data not being displayed. 这可能是数据不显示的原因之一。

Also, there should be only one element in the page with a particular id. 另外,页面中应该只有一个具有特定ID的元素。 In this case, if someone tries to expand two rows in the table, it will create two html elements (div) with id "detailsDiv". 在这种情况下,如果有人尝试扩展表中的两行,它将创建两个HTML元素(div),其ID为“ detailsDiv”。 This may lead to unpredictable results. 这可能会导致不可预测的结果。

I have a similar code working on my dev setup. 我在开发人员设置中也有类似的代码。

function format() {
  return '<div id="detailsDiv_' + rowId +'"></div>';
};

function loadChildRow(row) {
  $.ajax({
    type: 'GET',
    url: '@Url.Action("ChildRow","Users",new{ id= 1})',
    data: null,
    success: function (data) {
      $('#detailsDiv_' + rowId).html(data);                       
    }
  });
};

In the above code "rowId", may be randomly generated or just a counter to identify particular row. 在上面的代码“ rowId”中,可以随机生成,也可以只是一个计数器来标识特定的行。

For your first question: 对于第一个问题:

            success: function (data) {                 
                        $('.detailsDiv').html(data);                       
            }

For your second question: can you give us an example of what you want to do? 关于第二个问题:您能举例说明您想做什么吗?

What I discovered if you insert html page, it doesn't apply your current page styles. 我发现如果您插入html页面,它不会应用您当前的页面样式。 So add your formatting to the page you are trying to insert because it is treated as a separate page. 因此,将格式添加到您要插入的页面中,因为它被视为单独的页面。

Have you tried this? 你有尝试过吗?

$.ajax({
  type: 'GET',
  async: false,
  url: '@Url.Action("ChildRow","Users",new{ id= 1})',
  data: null,
  success: function (data) {
    $('.detailsDiv').innerHTML = data;                        
  }
});

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

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