doute3621 2019-08-03 02:24
浏览 254
已采纳

datatables“表格中没有数据”

This isn't my first time using datatables, but I have an implementation that I can't seem to get to work.

PHP:

public function renderTable() {
    $config = $this->container->get('RestorePoint\Configuration');
    $displayAndRestoreFrom = $config->get('Settings.displayAndRestoreFrom');
    if ($displayAndRestoreFrom !== 'local') {
        $remoteStorage = $this->container->get('RestorePoint\RemoteFilesystemFactory');
        $service = $remoteStorage->service($displayAndRestoreFrom);
        $files = $service->listFilesByCreated($service->getFolder());
    } else {
        $files = $this->getFilesByCreated($config->get('dir.files'));
    }
    foreach ($files as &$file) {
        if ($displayAndRestoreFrom == 'local') {
            $file['id'] = $file['name'];
        }
        $file['adapter'] = $displayAndRestoreFrom;
        $file['created'] = date('Y-m-d H:i:s', $file['created']);
        $file['actions'] = '';//view('RestorePoint.actions')->with($file)->render();
    }
    //echo json_encode(array('data'=>$files), JSON_UNESCAPED_SLASHES);
    return response()->json($files);
}

HTML:

                                <div class="table-responsive">
                                    <table class="table card-table table-vcenter text-nowrap datatable" id="backups" role="grid">
                                        <thead>
                                            <tr>
                                                <td class="w01">Name</td>
                                                <td>Created</td>
                                                <td>Adapter</td>
                                                <td>Actions</td>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>

JS: (defined in head)

    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <script src="./assets/plugins/datatables/datatables.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#backups').DataTable({
                //"deferRender": true,
                ajax: {
                    url: "<?php echo url('restorepoint/renderTable'); ?>",
                    dataSrc: ''
                },
                columns: [
                    {data: 'name'},
                    {data: 'created'},
                    {data: 'adapter'},
                    {data: 'actions'}
                ]
            });
        });
    </script>

JSON:

{"5":{"name":"restorepoint_2019_08_02_05_47_49.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_08_02_05_47_49.zip","created":"2019-08-02 05:55:11","adapter":"dropbox","actions":""},"4":{"name":"restorepoint_2019_08_02_07_30_05.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_08_02_07_30_05.zip","created":"2019-08-02 03:36:59","adapter":"dropbox","actions":""},"3":{"name":"restorepoint_2019_08_02_07_32_57.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_08_02_07_32_57.zip","created":"2019-08-02 03:35:54","adapter":"dropbox","actions":""},"2":{"name":"restorepoint_2019_08_02_07_38_55.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_08_02_07_38_55.zip","created":"2019-08-02 03:34:46","adapter":"dropbox","actions":""},"1":{"name":"restorepoint_2019_08_02_08_13_49.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_08_02_08_13_49.zip","created":"2019-08-02 03:33:40","adapter":"dropbox","actions":""},"0":{"name":"restorepoint_2019_07_31_08_10_34.zip","id":"\/restore_point_backups_fagardesignscom\/restorepoint_2019_07_31_08_10_34.zip","created":"2019-07-31 03:54:27","adapter":"dropbox","actions":""}}

Datatables is showing "No data available in table" however, I don't understand why given I've followed this example: https://datatables.net/examples/ajax/custom_data_flat.html

  • 写回答

1条回答 默认 最新

  • douzhi7082 2019-08-03 02:30
    关注

    it was the indexes messing things up.

    needed to use array_values()

        return response()->json(array_values($files));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 怎样删除 右键菜单里的 剪切(T)和复制(C)
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题