Codeigniter 4 使用搜索时分页未正确过滤

Codeigniter 4 Pagination not properly filter when use searching

提问人:asnka go 提问时间:11/16/2023 更新时间:11/16/2023 访问量:26

问:

我正在尝试将我的代码从 codeigniter3 转换为 codeigniter4 。所有函数在 ci3 中都能正常工作,但在 im 转换为 ci4 后某些函数不起作用。

当我在搜索框中进行搜索时,如果数据不超过 10(默认每页 10),则分页会正确显示。但是,当结果超过 10 时,结果会出现在一个页面中,并且分页也会出现没有正确的页面。据说页面只会根据结果总数显示。

控制器 (Homes.php)

public function posts()
{
    $homemodel = new \App\Models\Homemodel(); // Assuming Homemodel is in the Models folder

    $columns = ['id', 'title', 'body', 'created_at', 'id'];

    $limit = $this->request->getPost('length');
    $start = $this->request->getPost('start');
    $order = $columns[$this->request->getPost('order')[0]['column']];
    $dir = $this->request->getPost('order')[0]['dir'];

    $totalData = $homemodel->allPostsCount();  // Adjusted method name

    $totalFiltered = $totalData;

    if (empty($this->request->getPost('search')['value'])) {
        $posts = $homemodel->allPosts($limit, $start, $order, $dir);  // Adjusted method name
    } else {
        $search = $this->request->getPost('search')['value'];

        $posts =  $homemodel->postsSearch($limit, $start, $search, $order, $dir);  // Adjusted method name

        $totalFiltered = $homemodel->postsSearchCount($search);  // Adjusted method name
    }

    $data = [];
    if (!empty($posts)) {
        foreach ($posts as $post) {
            $nestedData['id'] = $post['id'];
            $nestedData['title'] = $post['title'];
            $nestedData['body'] = substr(strip_tags($post['body']), 0, 50) . "...";
            $nestedData['created_at'] = date('j M Y h:i a', strtotime($post['created_at']));

            $data[] = $nestedData;
        }
    }

    $json_data = [
        "draw"            => intval($this->request->getPost('draw')),
        "recordsTotal"    => intval($totalData),
        "recordsFiltered" => intval($totalFiltered),
        "data"            => $data,
    ];

    return $this->response->setJSON($json_data);
}

我的模型 (Homemodel.php)

class Homemodel extends Model {
    protected $table = 'posts';

    public function allPostsCount()
    {
        return $this->countAll();
    }

    public function allPosts($limit, $start, $col, $dir)
    {
        return $this->select('*')
            ->limit($limit, $start)
            ->orderBy($col, $dir)
            ->paginate($limit);
    }

    public function postsSearch($limit, $start, $search, $col, $dir)
    {
        return $this->select('*')
            ->like('id', $search)
            ->orLike('title', $search)
            ->limit($limit, $start)
            ->orderBy($col, $dir)
            ->findAll();
    }

    public function postsSearchCount($search)
    {
        return $this->like('id', $search)
            ->orLike('title', $search)
            ->countAllResults();
    }
 }

我的视图 (homes.php)

<div class="row">
    <div class="col-md-12">
        <table class="table table-bordered" id="posts">
            <thead>
                <th>Id</th>
                <th>Title</th>
                <th>Body</th>
                <th>Created At</th>
            </thead>                
        </table>
    </div>
</div>

<script>
    $(document).ready(function () {
        $('#posts').DataTable({
            "processing": true,
            "serverSide": true,
            "stateSave": true,
            "ajax": {
                "url": "<?php echo site_url('homes/posts') ?>", // Use site_url() for proper URL generation
                "dataType": "json",
                "type": "POST",
                "data": {
                    '<?php echo csrf_token() ?>': '<?php echo csrf_hash() ?>'
                }
            },
            "columns": [
                { "data": "id" },
                { "data": "title" },
                { "data": "body" },
                { "data": "created_at" },
            ]
        });
    });
</script>

结果 当我尝试只有 1 个结果的搜索数据时,显示正确的分页

enter image description here

结果 当我尝试搜索数据时有超过 10 个结果,结果被转储到一个页面中,分页不正确

enter image description here

继续。。。

enter image description here

我的目标是当进行搜索并且结果超过 10 个(取决于设置的限制)时,结果将显示正确的页面。

我正在寻求帮助。谢谢

codeigniter 分页 codeigniter-4

评论


答: 暂无答案