首页 > 解决方案 > 如果其他像在数据表 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'
  }]
});

标签: javascriptjqueryjsonajaxdatatable

解决方案


我会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以便您知道要使用哪个索引。


推荐阅读