1

I am using bootstrap table in my web page and want to get complete textual data from all table cells, when pagination is on. I have tried the following method and it returns all the data:

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

Now when i traverse data object to get value for every cell it works fine but, for those cells which have some nested html , for example:

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

Now, in this case, i want to get value for second cell as google but it returns me whole html as

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

Any idea, how i can get only textual value.

I can't do any server side operation, I have to achieve this using javascript/jquery. I have also tried using jquery:

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

it returns data correctly but only which is visible on active page and i want all the data.

TechMaster
  • 251
  • 1
  • 2
  • 13

2 Answers2

3

Based on your file on JSFiddle I have modified the JS part as follows, this will get you the text on every td(i.e. text or text content) and not the values of their attributes. Basically this traverses through the DOM searching for tags embedded in ones - except for those on the table header - then obtains the text value.

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);
});

You can see it in action here

UPDATE: This will get you all data regardless of pagination, also it will strip tags and nested tags.

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);
});

You can see it here

  • 1
    Hey, Thanks but this code will not work in case pagination is turned on, So any solution traversing DOM is not an option because I need to get complete data irrespective of pagination on or off. You can see here, select 5 records per page and then click, records are missing because Dom object are not there for invisible pages. http://jsfiddle.net/e3nk137y/7447/ – TechMaster Jun 29 '16 at 09:37
  • @TechMaster I do have the same need. Have you found a solution? – Maxbester Feb 10 '21 at 16:14
2

Since the actual data is coming in as a string, I don't think bootstrap-table can't differentiate it from the other data. The simple solution I can think of is to use substring() to extract the data from the cells that contain custom html.

http://jsfiddle.net/vwg5Lefz/

The alternative is to go through the generated table <td> and use text() to get the text data from the cells.

http://jsfiddle.net/n0djy60v/

Kevin Quach
  • 221
  • 2
  • 7
  • Second solution is not an option because it doesn't return complete data in case pagination is turned on. First solution is okay but not very elegant because it can break if i have nested tags inside cell, However, i think this is only feasible choice i have, i think i can generalise portion of code to find textual content inside cell by removing all the tags. Thanks ! – TechMaster Jun 28 '16 at 12:53
  • No problem, I realize it's not exactly the solution but if I spot a more elegant solution similar to what you're looking for in a future update for the plugin, I'll be sure to update my answer. :) – Kevin Quach Jun 28 '16 at 22:57
  • I'm looking for something equivalent to the second solution that would return all rows (even those that the pagination hides). Is it possible? – Maxbester Feb 10 '21 at 16:12