如何在字符串中包含隐藏的 HTML 元素 - 数据表 - jquery

How do I include hidden HTML elements in a string - datatables - jquery

本文关键字:元素 HTML 数据表 jquery 隐藏 字符串 包含      更新时间:2023-09-26

我有一个脚本,它将获取元素(表)的html(),并将其写入文本文件。

我从中获取的表格是使用数据表 jQuery 插件绘制的,用于分页和排序。

JS将只包含字符串中的displayed元素。是否可以也包含所有hidden元素,以便我的所有表行都包含在此字符串中,而不仅仅是active页面上的行?

function saveRangeReview(){
    var htmltosave = $("#rangereviewtable").html();
    var filename = "testreview";
    var postdata = { rangereviewdata: htmltosave,
                     filename: filename};
    $.ajax({
          url: "rangereview.save.php",
          type: "post",
          dataType: 'html',
          data: postdata,
          success: function (response) {
              console.log(response); //only includes 10 currently displayed rows
              },
          error: function(jqXHR, textStatus, errorThrown) {
              console.log(textStatus, errorThrown);
            }
      });
}

您必须通过dataTables API来执行此操作,幸运的是,有一些方法可以检索表的所有方面 - 以及底层DOM节点。

这是一个示例,它将整个表重现为纯 HTML 标记

$('#generateHTML').on('click', function() {
  var domNodes = '';
  table.rows().every(function(rowIdx, tableLoop, rowLoop) {
    domNodes += this.node().outerHTML
  });
  //<thead> section
  html = table.table().header().outerHTML
  //add <tbody> with all DOM nodes
  html += '<tbody>'+domNodes+'</tbody>'
  //<tfoot> section
  html += table.table().footer().outerHTML
  //wrap into <table>
  html = '<table>'+html+'</table>'
  console.log(html)
})

演示 -> http://jsfiddle.net/krqL519o/