对某些 Datatable 列进行排序时出错,但当我注释“ServerSide”代码时很正常 (CodeIgniter 4)

Error when sorting some Datatable columns but It's normal when I commented the "ServerSide" Code (CodeIgniter 4)

提问人:Code Man 提问时间:7/27/2020 更新时间:7/27/2020 访问量:596

问:

我试图对一些列进行排序,但出现此错误:Ajax error. For more information about this error, please see http://datatables.net/tn/7

这是我的控制器:

public function show_all_issues()
    {
        $project_model = new Project_Model();
        $arr_project = $project_model->get_selected_project()->getResult();
        $project_id = '';
        foreach ($arr_project as $data) {
            $project_id = $data->id;
        }
        $model = new Issue_Model();
        $lists = $model->get_all_issues($project_id);
        $data = [];
        $no = $_POST["start"];
        foreach ($lists as $list) {
            $color = '';
            $no++;
            $row = [];
            $row[] = '';
            $row[] = $no;
            $row[] = $list->issue_key;
            $row[] = $list->issue_title;
            $row[] = $list->issue_reporter;
            $row[] = $list->issue_assignee;

            if ($list->issue_label == 'Open') {
                $color = 'secondary';
            } else if ($list->issue_label == "Waiting for Support") {
                $color = 'info';
            } else if ($list->issue_label == "Work In Progress") {
                $color = 'warning';
            } else {
                $color = 'success';
            }

            $row[] = "<span class='badge badge-$color'>$list->issue_label</span>";
            $row[] = $list->issue_created;
            $data[] = $row;
        }

        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $model->count_all(),
            "recordsFiltered" => $model->count_filtered_all(),
            'data' => $data
        );
        return $this->respond($output, 200);
    }

这是我的模型:

protected $table = 'issues';
    protected $column_order = array('issue_key', 'issue_reporter', 'issue_assignee', 'issue_label', 'issue_created');
    protected $column_search = array('issue_key', 'issue_title', 'issue_reporter', 'issue_assignee', 'issue_label');
    protected $order = array('issue_label' => 'desc');
    protected $db;
    protected $dt;

    function __construct()
    {
        parent::__construct();
        $this->db = db_connect();
        $this->dt = $this->db->table($this->table);
    }

    private function _get_datatables_query()
    {
        $i = 0;
        foreach ($this->column_search as $item) {
            if ($_POST['search']['value']) {
                if ($i === 0) {
                    $this->dt->groupStart();
                    $this->dt->like($item, $_POST['search']['value']);
                } else {
                    $this->dt->orLike($item, $_POST['search']['value']);
                }
                if (count($this->column_search) - 1 == $i)
                    $this->dt->groupEnd();
            }
            $i++;
        }

        if ($_POST['order']) {
            $this->dt->orderBy($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else if (isset($this->order)) {
            $order = $this->order;
            $this->dt->orderBy(key($order), $order[key($order)]);
        }
    }

function get_all_issues($project_id)
    {
        $this->_get_datatables_query();
        if ($_POST['length'] != -1){
            $this->dt->limit($_POST['length'], $_POST['start']);
        }
        $query = $this->dt->orderBy('issue_created', 'desc')->getWhere(['project_id' => $project_id]);
        return $query->getResult();
    }

    function count_filtered_all()
    {
        $this->_get_datatables_query();
        return $this->dt->countAllResults();
    }

public function count_all()
    {
        $tbl_storage = $this->db->table($this->table);
        return $tbl_storage->countAllResults();
    }

这是我的表 + JQuery (AJAX) :

<table class="table table-bordered table-hover" id="table" style="width:100%;">
                <thead>
                    <tr>
                        <th class="text-center"><input type="checkbox" /></th>
                        <th>No.</th>
                        <th>Key</th>
                        <th>Summary</th>
                        <th>Reporter</th>
                        <th>Assignee</th>
                        <th>Status</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

         <script type="text/javascript">
            $(document).ready(function() {
                var table = $('#table').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        "url": "<?php echo base_url('public/Issue_Controller/'); ?>/show_<?= $issue_type; ?>",
                        "type": "POST"
                    },

                    //optional
                    "lengthMenu": [
                        [5, 10, 25],
                        [5, 10, 25]
                    ],
                    "columnDefs": [{
                            "targets": 0,
                            "orderable": false,
                            "checkboxes": {
                                "selectRow": true
                            },
                            "className": 'select-checkbox'
                        },
                        {
                            "targets": 3,
                            "orderable": false
                        }
                    ],

                    "select": {
                        "style": 'multi',
                        "selector": 'td:first-child'
                    },
                    "order": [],
                });

                table.on("click", "th.select-checkbox", function() {
                    if ($("th.select-checkbox").hasClass("selected")) {
                        table.rows().deselect();
                        $("th.select-checkbox").removeClass("selected");
                    } else {
                        table.rows().select();
                        $("th.select-checkbox").addClass("selected");
                    }
                }).on("select deselect", function() {
                    ("Some selection or deselection going on")
                    if (table.rows({
                            selected: true
                        }).count() !== table.rows().count()) {
                        $("th.select-checkbox").removeClass("selected");
                    } else {
                        $("th.select-checkbox").addClass("selected");
                    }
                });
            });

这是表格的样子:表格视图

错误如下所示:错误视图

当我尝试对 Assignee's Columns、Status's Column 和 Created's Column 进行排序时会发生这种情况,但如果我尝试对其余列进行排序,它不会显示错误。

还有一个..如果我将 false 作为 ServerSide 的值或将其作为注释,它不会显示错误。

太奇怪了:]

我不知道我的代码出了什么问题。有人可以帮助我的小脑袋吗?拜托... :')

jQuery ajax codeigniter datatable 服务器端

评论

0赞 Vickel 7/27/2020
您是否点击了链接: datatables.net/tn/7
0赞 Code Man 7/27/2020
已经。它显示 500 内部错误
0赞 Vickel 7/27/2020
500 内部错误意味着您的服务器端脚本出了问题,很可能是您的数据库查询。使用 echo $this->db->last_query(); 查看您的查询的外观并在例如 phpmyAdmin 中对其进行测试。您可能还会在浏览器的“网络”选项卡或日志文件中看到该错误
0赞 Code Man 7/27/2020
我已经尝试了PhpMyAdmin中的“最后一个查询”,它没有显示任何错误

答: 暂无答案