使用数据表 v1.10
目前我的表格在第二列中以这种格式显示日期 17-04-2019 14:34
.这是数据库返回的格式。一切都按预期进行,过滤和搜索。
HTML 表格单元格当前看起来像这样;
<td class="sorting_1">17-04-2019 14:34</td>
我想在每个单元格中选择分钟,并在它们周围添加一个自定义的 css 类,就像这样
<td class="sorting_1">17-04-2019 <span class="text-muted">14:34</span></td>
我的数据表代码如下;
$(document).ready(function() {
$('#example').DataTable({
"columns": [
{
data: 'date',
"render": function(data, type, row) {
return data;
// console.log(data) displays a string
// in this format 17-04-2019 14:34
},
}
],
});
});
我试过使用 JavaScript String split() 方法,但无法正常工作。
感谢任何帮助。
最佳答案
试试这个;
$(document).ready(function() {
$('#example').DataTable({
"columns": [
{
data: 'date',
"render": function(data, type, row) {
var array = row['datePaid'].split(" ");
var date = array[0];
var mins = array[1];
data = date + ' ' + '<span class="text-muted">' + mins + '</span>';
return data;
// console.log(data) displays a string
// in this format 17-04-2019 14:34
},
}
],
});
});
我确信有更有效的方法可以做到这一点,但它应该有效。
关于javascript - 数据表日期时间提取分钟并添加 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55729212/