导出jQuery DataTable时自定义数据

时间:2018-09-19 12:28:04

标签: jquery datatables export-to-excel

我在Laravel中使用jQuery DataTables,并且想使用插件的导出功能。

现在我的问题是我的表中有一些HTML,因此我渲染了一个复选框,而不是实际的文本。

示例

<td>
    <span class="{{($r->submitted == 1)?'checkbox-checked':''}}">
        <i class="material-icons md-18">check_box</i>
    </span>
</td>

当我在Excel中导出该表时,我得到了td'check_box'的值,因此excel看起来像这样

+-----------+----------+-----------+-----------+--+
| Firstname | Lastname | Option 1  | Option 2  |  |
+-----------+----------+-----------+-----------+--+
| Christos  | Savva    | check_box | check_box |  |
+-----------+----------+-----------+-----------+--+
| Second    | Person   | check_box | check_box |  |
+-----------+----------+-----------+-----------+--+
| Third     | Person   | check_box | check_box |  |
+-----------+----------+-----------+-----------+--+

显然,在excel文件中这没有任何意义,而在屏幕上就可以了,因为我渲染了图标。

根据文档,我尝试使用Format output data - export options

var buttonCommon = {
    exportOptions: {
        format: {
            body: function ( data, row, column, node ) {
                //Do stuff to replace check_box with word Yes
                //or no
                return data
            }
        }
    }
};

问题来了。当我从函数返回数据时,它会返回td内的整个html块。

所以excel看起来像这样

+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+
| Firstname | Lastname | Option 1                                                      | Option 2                                                      |  |
+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+
| Christos  | Savva    | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> |  |
|           |          |                                                               |                                                               |  |
|           |          | <i class="material-icons md-18">yes</i>                       | <i class="material-icons md-18">yes</i>                       |  |
|           |          |                                                               |                                                               |  |
|           |          | </span>                                                       | </span>                                                       |  |
+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+
| Second    | Person   | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> |  |
|           |          | <i class="material-icons md-18">yes</i>                       |                                                               |  |
|           |          | </span>                                                       | <i class="material-icons md-18">yes</i>                       |  |
|           |          |                                                               |                                                               |  |
|           |          |                                                               | </span>                                                       |  |
+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+
| Third     | Person   | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> | <span class="{{($r->submitted == 1)?'checkbox-checked':''}}"> |  |
|           |          |                                                               |                                                               |  |
|           |          | <i class="material-icons md-18">yes</i>                       | <i class="material-icons md-18">yes</i>                       |  |
|           |          |                                                               |                                                               |  |
|           |          | </span>                                                       | </span>                                                       |  |
+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+

有人知道我能做到吗?

1 个答案:

答案 0 :(得分:0)

如果我们可以假定呈现HTML。即<span class="{{($r->submitted == 1)?'checkbox-checked':''}}">呈现为<span class="checkbox-checked"><span class="">

exportOptions: {
  format: {
    body: function ( data, row, column, node ) {
      if (![2,3].indexOf(column)) {
        return $('span', data).hasClass('checkbox-checked')
          ? 'yes'
          : 'no'
      }
      return data
    }
  }    
}