当前位置: 首页 > 知识库问答 >
问题:

如果记录为null Datatables AJAX-dataSrc,则显示警告

谷梁振
2023-03-14
let dataSeta = {"error": true,"message": "Data Pengiriman tidak ditemukan","data": null};

let pengiriman = $('#pengiriman-data').DataTable({
        responsive: true,
        data: dataSeta,
        columnDefs: [{
            searchable: false,
            orderable: false,
            targets: 0
        }],
        columns: [{
            data: "id_mon",
            defaultContent: ''
        }, {
            data: "kodejalan",
            defaultContent: ''
        }, {
            data: "nopol",
            defaultContent: ''
        }, {
            data: "supir",
            defaultContent: ''
        }, {
            data: "pelanggan",
            defaultContent: ''
        }, {
            data: "start",
            defaultContent: ''
        }, {
            data: "end",
            defaultContent: ''
        }, {
            data: "status",
            defaultContent: ''
        }, {
            data: "tanggal",
            defaultContent: ''
        }, {
            data: "action",
            defaultContent: ''
        }],
        "lengthChange": true,
        "lengthMenu": [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],
        "paging":   true,
        "ordering": true,
        "info": true
      });
html lang-html prettyprint-override"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.24/js/dataTables.bootstrap4.min.js"></script>
<table loading="lazy" id="pengiriman-data" class="table table-bordered table-striped">
<thead>
  <tr>
    <th>ID</th>
    <th>Surat Jalan</th>
    <th>Mobil</th>
    <th>Supir</th>
    <th>Pelanggan</th>
    <th>Start</th>
    <th>End</th>
    <th>Status</th>
    <th>Tanggal</th>
    <th width="160">Aksi</th>
  </tr>
</thead>
</table>

我有代码,问题是如果记录null显示警告“DataTables警告:table id=pengiriman数据-true”DataTables

let pengiriman = $('#pengiriman-data').DataTable({
        responsive: true,
        ajax: {
            "url":readUrl,
            "dataSrc": 'data'
        },
        columnDefs: [{
            searchable: false,
            orderable: false,
            targets: 0
        }],
        columns: [{
            data: "id_mon",
            defaultContent: ''
        }, {
            data: "kodejalan",
            defaultContent: ''
        }, {
            data: "nopol",
            defaultContent: ''
        }, {
            data: "supir",
            defaultContent: ''
        }, {
            data: "pelanggan",
            defaultContent: ''
        }, {
            data: "start",
            defaultContent: ''
        }, {
            data: "end",
            defaultContent: ''
        }, {
            data: "status",
            defaultContent: ''
        }, {
            data: "tanggal",
            defaultContent: ''
        }, {
            data: "action",
            defaultContent: ''
        }],
        "lengthChange": true,
        "lengthMenu": [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],
        "paging":   true,
        "ordering": true,
        "info": true
      });

JSON记录数据:

{
    "error": false,
    "message": "Data Pengiriman",
    "data": [
        {
            "id_mon": "6",
            "kodejalan": "SJ00004\/03\/21",
            "nopol": "",
            "supir": "Dodong Karim",
            "pelanggan": "PT. Zoruka Sharp",
            "start": "",
            "end": "<button type=\"button\" class=\"btn btn-sm btn-success btn-flat\" onclick=\"return dataMap(6)\">Show<\/button>",
            "status": "<span class=\"badge badge-info\">Created<\/span>",
            "tanggal": "2021-04-01 20:12:14",
            "action": "bbb"
        },
        {
            "id_mon": "7",
            "kodejalan": "SJ00005\/03\/21",
            "nopol": "",
            "supir": "Dadang Kipas",
            "pelanggan": "PT. Zaruko Store",
            "start": "",
            "end": "<button type=\"button\" class=\"btn btn-sm btn-success btn-flat\" onclick=\"return dataMap(7)\">Show<\/button>",
            "status": "<span class=\"badge badge-info\">Created<\/span>",
            "tanggal": "2021-04-01 20:12:14",
            "action": "aaa"
        }
    ]
}

JSON记录空数据:

{
    "error": true,
    "message": "Data Pengiriman tidak ditemukan",
    "data": null
}

共有1个答案

师向文
2023-03-14

好的,这个问题终于解决了:)我将JSON结果错误更改为errData

{
    "errData": true,
    "message": "Data Pengiriman tidak ditemukan",
    "data": null
}

和JS代码,包括:

let pengiriman = $('#pengiriman-data').DataTable({
        responsive: true,
        ajax: {
            "url":readUrl,
            "dataSrc": function(data){
                if(data.data == null){
                    return [];
                } else {
                    return data.data;
                }
            }
        },
        columnDefs: [{
            searchable: false,
            orderable: false,
            targets: 0
        }],
        columns: [{
            defaultContent: '',
            data: 'id_mon'
        }, {
            defaultContent: '',
            data: 'kodejalan'
        }, {
            defaultContent: '',
            data: 'nopol'
        }, {
            defaultContent: '',
            data: 'supir'
        }, {
            defaultContent: '',
            data: 'pelanggan'
        }, {
            defaultContent: '',
            data: 'start'
        }, {
            defaultContent: '',
            data: 'end'
        }, {
            defaultContent: '',
            data: 'status'
        }, {
            defaultContent: '',
            data: 'tanggal'
        }, {
            defaultContent: '',
            data: 'action'
        }],
        "lengthChange": true,
        "lengthMenu": [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],
        "paging":   true,
        "ordering": true,
        "info": true
      });

阅读:我的问题在这个论坛上解决了

 类似资料:
  • 如果值为空,则需要在单元格中显示一个不间断的空格。这是我的模板: 我试过这个,但不管用: 它返回值的问题是: 如果许可证号带有值,则单元格为空,行颜色如下所示。 利用卢库马的建议,它表明了这一点: 更改筛选器中的if语句后,仍然不显示非值:

  • SQL Server 2008 SQL查询显示IP地址(如果存在) 输出 IP_ADDR1_TEXT是 列强制转换(IP_ADDR1<0则0xFFFFFFFFF+IP_ADDR1否则IP_ADDR1 END/256/256/256&0xFF为VARCHAR)+“.”+强制转换(IP_ADDR1<0则0xFFFFFFFF+IP_ADDR1否则IP_ADDR1结束/256/256和0xFF为VARCH

  • 我在ReactJS中工作,我通过API从服务器获取数据。我做了一些搜索过滤,我想显示消息,如果没有记录可用?我是ReactJS的初学者,没有太多关于ReactJS的知识。 代码:

  • 本文向大家介绍Xamarin.iOS 显示登录警报,包括了Xamarin.iOS 显示登录警报的使用技巧和注意事项,需要的朋友参考一下 示例 以下代码适用于iOS 8及更低版本的iOS,用于创建登录警报。            

  • 在开发和测试环境下,根记录器级别为DEBUG或info。spring-boot横幅在应用程序启动时显示: logging-devel.xml(显示横幅) logging-production.xml(未显示横幅)

  • 问题内容: 以下AngularJS应用程序正在使用ng-repeat和已应用的过滤器。某个应用的过滤器不会留下任何值。如何显示通知? js小提琴 的HTML AngularJS 问题答案: 我认为这是您想要的: 链接 这是另一个FIDDLE LINK,请检查此