jquery - 导出 jQuery DataTable 时自定义数据
问题描述
我在 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> | |
+-----------+----------+---------------------------------------------------------------+---------------------------------------------------------------+--+
有谁知道我怎么能做到这一点?
解决方案
如果我们可以假设 HTML 被渲染。即<span class="{{($r->submitted == 1)?'checkbox-checked':''}}">
呈现为<span class="checkbox-checked">
or <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
}
}
}
推荐阅读
- python - 你如何用YES替换输入中的'Y'?
- javascript - 动态查询 mongoDb 中的嵌套字段
- java - MongoDB - 未找到 MongoClientURI ...(使用 maven)
- azure - Device Twin IoT Hub - 从 Python 中的 JSON 获取标签
- php - 如何将特定记录链接到 PHP 表单以更新该记录?
- kubernetes - 在 Kind 上使用 Ingress 和 LoadBalancer 类型的服务
- python - 用于输入自动语音识别模型的不同格式(比特率)的音频数据
- quaternions - 使用带有 gl-matrix.js 的四元数进行简单旋转
- java - Thymeleaf:检索隐藏输入到控制器的值
- python - 如何用while循环解决这个问题