javascript - 如果其他像在数据表 jquery
问题描述
我如何在数据表中执行 If else like 方法?变量“data”返回变量的值,这是正确的,但如果它是空白的,它会返回单词“from1”,“from2”,这应该是变量“from1”的值。我是否采取了正确的方法,或者您对这个问题有什么建议作为解决方法?谢谢您的回答。这是我的代码:
var table = $('#records').DataTable({
type: 'post',
"ajax": "getHumanTrainings",
"bPaginate": true,
"bProcessing": true,
"pageLength": 10,
"columns": [{
mData: 'tdesc'
}, {
data: "fdDateFrom2",
defaultContent: 'from1'
}, {
data: "fdDateTo2",
defaultContent: 'from2'
}, {
data: "fcTrainor2",
defaultContent: 'train1'
}, {
mData: 'dur'
}]
});
解决方案
我会render
为您拥有的列数据使用该选项,它在希望默认显示某些内容方面更加灵活。
var table = $('#records').DataTable({
type: 'post',
"ajax": "getHumanTrainings",
"bPaginate": true,
"bProcessing": true,
"pageLength": 10,
"columns": [{
mData: 'tdesc'
}, {
data: "fdDateFrom2",
render: function(data, type, row) {
// Check if blank
if (data === "") {
return row[<index for from1>]; // Just use the index for from1
}
// If not blank display data normally
return data;
}
}, {
data: "fdDateTo2",
render: function(data, type, row) {
// Check if blank
if (data === "") {
return row[<index for from2>]; // Just use the index for from2
}
// If not blank display data normally
return data;
}
}, {
data: "fcTrainor2",
render: function(data, type, row) {
// Check if blank
if (data === "") {
return row[<index for train1>]; // Just use the index for train1
}
// If not blank display data normally
return data;
}
}, {
mData: 'dur'
}]
});
由于我不熟悉您的数据,因此我已留下评论为您提供指导,我建议您row
先打印出来,render
以便您知道要使用哪个索引。
推荐阅读
- sql - 如何 GROUP BY date,id 通过检查 ORACLE 中的总交易量
- c# - 如何获取.net核心项目中项目文件夹中存在的文件的相对路径
- ionic4 - 离子范围如果为真,按下旋钮时会显示一个具有整数值的引脚。我可以在不按下旋钮的情况下默认显示吗?
- python - 将数组保存在 csv 文件的单元格中
- javascript - React useState 在运行时运行 useEffect
- java - Java 在使用 Scanner 将字符串数组作为用户输入时打印“null”并突然结束
- regex - c# Regex.Replace 为 (.*) 但这个 (.*) 有最大长度
- python - 我将视频 mp4 文件转换为音频 mp3 格式,现在我想使用 os.remove 删除原始 Mp4 视频文件但给出错误
- vue.js - 在 Vuetify 中使用 srcset
- node.js - Nodejs-为什么在安装nodemon时出现这个错误