开启分页后如何从(html)bootstraptable获取完整数据

How to get complete data from (html) bootstrap table when pagination is turned on

我在我的网页中使用 bootstrap table 并希望在分页打开时从所有 table 单元格中获取完整的文本数据。我尝试了以下方法,它 returns 所有数据:

var data = $('#' + tableID).bootstrapTable('getData')

现在,当我遍历 data 对象以获取每个单元格的值时,它工作正常,但是对于那些嵌套了 html 的单元格,例如:

   <td class="danger">cell 4</td>
   <td>
   <a href="https://www.google.com">google</a>
   </td>

现在,在这种情况下,我想获得第二个单元格的值 google 但它 returns 我整个 html

 <a href="https://www.google.com">google</a>

任何想法,我如何才能只获得文本值。

我无法进行任何服务器端操作,我必须使用 javascript/jquery 来实现。我也尝试过使用 jquery:

function getColData(tableID,colIndex) {
    var colArray = $('#' + tableID + ' td:nth-child'+'('+colIndex+')').map(function(){
           return $(this).text();
       }).get();
       return colArray
    }

它 returns 数据正确,但只有在活动页面上可见,我想要所有数据。

由于实际数据是以字符串形式传入的,我认为 bootstrap-table 无法将其与其他数据区分开来。我能想到的简单解决方案是使用 substring() 从包含自定义 html.

的单元格中提取数据

http://jsfiddle.net/vwg5Lefz/

另一种方法是遍历生成的 table <td> 并使用 text() 从单元格中获取文本数据。

http://jsfiddle.net/n0djy60v/

根据您在 JSFiddle 上的文件,我修改了 JS 部分如下,这将为您提供每个 td 上的文本(即文本或文本内容),而不是它们的属性值。基本上这会遍历 DOM 搜索嵌入在其中的标签 - 除了 table header 上的标签 - 然后获取文本值。

var table = $('#table'), button = $('#button');
button.click(function() {
  var data = [];
  table.find('tr:not(:first)').each(function(i, row) {
    var cols = [];
    $(this).find('td').each(function(i, col) {
      cols.push($(this).text());
    });
    data.push(cols);
  });
  alert(data);
});

你可以看到它的实际效果here

更新: 无论分页如何,这都会为您提供所有数据,它还会去除标签和嵌套标签。

var table = $('#table'), button = $('#button');

button.click(function() {
  var messedData = table.bootstrapTable('getData');
  var data = [];
  $.each(messedData, function(i, row) {
    var rowData = {
      'name': row['0'],
      'star': row['1'],
      'forks': row['2'],
      'desc': row['3'],
    }

    for (prop in rowData) {
      var tmp = document.createElement("div");
      tmp.innerHTML = rowData[prop];
      rowData[prop] = tmp.textContent || tmp.innerText || "";
    }

    data.push(rowData);
  });
  console.log(data);
});

可以看到here