首页 > 解决方案 > 以json形式发送数据并在datatable中显示

问题描述

谁能帮助我如何在数据表中显示我的数据?我正在尝试服务器端数据表,但我无法得到它。无论如何,我可以以 JSON 格式返回我的数据,但我总是收到此错误:DataTables 警告:表 id=show-employee-table - 无法重新初始化 DataTable。

这些是我尝试过的:

ajax.js

var url = '../../class/functions.php';

function show_employee() {
    var data = { action : 'Show Employee Table' };
    $("#show-employee-table").dataTable({
        ajax: {
            type: 'POST', url: url, data: data
        }
    })
}

函数.php

<?php 
    include 'config.php';

    switch($_POST['action']) {
        case 'Show Employee Table':
            $data->show_employee_table();
        break;
    }

?>

类.php

<?php
    include 'controller.php';

    class db extends Controller {
        //Get all employees
        public function get_employee() {
            $query = $this->db->query("SELECT * FROM tbl_accounts WHERE employee_role_id = 2");
            return $query;
        }

        public function show_employee_table() {
            foreach($this->get_employee() as $row): ?>
                <?php $data = $row; ?>
                <?php $employee_status = ($row['employee_status'] == "Active") ? '<span class="badge badge-success">Active</span>' : '<span class="badge badge-danger">Not Active</span>';?>
                    
            <?php endforeach;
    
            echo json_encode(['data' => $data]);
        }
    }
?>

控制器.php

<?php
    class Controller {
        public $host                = 'localhost';
        public $database_username   = 'root';
        public $database_password   = '';
        public $database_name       = 'try_db';
        public $db;
        
        function __construct() {
            $this->connection();
        }

        private function connection() {
            $this->db = new mysqli($this->host, $this->database_username, $this->database_password, $this->database_name);
            if($this->db->connect_error) {
                echo 'Could not connect to the database!';
            }
        }
    }
?>

配置文件

<?php
    session_start();
    ob_start();
    include 'class.php';
    //include 'init.php';
    $data = new db();
?>

标签: phpajaxdatatablesdatatables-1.10

解决方案


错误

错误:DataTables 警告:table id=show-employee-table - 无法重新初始化 DataTable。

是一个很常见的。事实上,他们有一个专门的页面来说明这个错误的原因和诊断。

请参阅:https ://datatables.net/manual/tech-notes/3

简而言之 - 您只能初始化 DataTable 一次,随后的调用(带有选项)会导致错误。

不过,您有选择,请参阅文档。

至于答案试试这个。

控制器.php

<?php

class Controller {
    public $host                = 'localhost';
    public $database_username   = 'root';
    public $database_password   = '';
    public $database_name       = 'try_db';
    public $db;
    
    function __construct() {
        $this->connection();
    }

    private function connection() {
        $this->db = new mysqli(
            $this->host, 
            $this->database_username, 
            $this->database_password, 
            $this->database_name
        );

        if($this->db->connect_error) {
            die 'Could not connect to the database!';
        }
    }
}

配置文件

<?php

session_start();
ob_start();
require_once 'class.php';
$data = new db();

类.php

<?php

require_once 'controller.php';

class db extends Controller {

    /** Get all employees */
    public function get_employee() {
        if (!($query = $this->db->query("SELECT * FROM tbl_accounts WHERE employee_role_id = 2"))) {
            die "Could not execute query"
        };

        return $query->fetch_all(MYSQLI_ASSOC);
    }

    public function show_employee_table() {
        echo json_encode(['data' => $this->get_employee()]);
    }
}

函数.php

<?php 
    require_once 'config.php';

    switch($_POST['action']) {
        case 'Show Employee Table':
            $data->show_employee_table();
        break;
    }

ajax.js

var url = '../../class/functions.php';
var dtTable = null;

$(function() {
    dtTable = $("#show-employee-table").dataTable({
        ajax: {
            type: 'POST', 
            url: url, 
            data: {
                action : 'Show Employee Table'
            }
        }
    });
})

function refreshDT() {
    dtTable.ajax.reload();
}

推荐阅读