为什么Jquery-ui自动完成在codeigniter中不起作用?

问题描述 投票:-3回答:1

我正在构建一个博客应用程序。我有一个搜索框,会将类别建议为用户类型。所以我使用jquery-ui自动完成。但不确定为什么它不起作用。我是新手,花了一整天。请帮忙。这是我的代码。

模型:

public function getCategoriesJson ($keyword) {

    $this->db->select('cat_name');
    $this->db->from('categories');
    $this->db->like('cat_name', $keyword);

    $data = $this->db->get()->result_array();

    $output = array();

    if ($data) {
        foreach ($data as $d) {
            array_push($output, $d['cat_name']);
        }
    }

    echo json_encode($output);
}

视图:

控制器:

public function getCatJson () {

    $this->Category_model->getCategoriesJson($this->input->get('query'));
}

脚本:

$('#search').autocomplete({

    source: '<?php echo base_url(); ?>categories/getCatJson?query=' + $('#search').val(),

    minLength: 1
});
javascript php codeigniter jquery-ui-autocomplete web-development-server
1个回答
0
投票

最后,我有一个解决方案。我改变了我的模型功能代码和我的脚本,如下所示,它的工作原理。

模型:

public function getCategoriesJson($keyword)
{
        $this->db->select('cat_name');
        $this->db->from('categories');
        $this->db->like('cat_name', $keyword);

        $data = $this->db->get()->result_array();

        $output = array();

        if($data)
        {
            foreach($data as $d)
            {
                array_push($output, ['label' => $d['cat_name']]);
            }
        }

        echo json_encode($output);
}

脚本:

$("#search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<?php echo base_url(); ?>categories/getCatJson',
                type:'GET',
                dataType: "json",
                data: {
                    query: request.term
                },
                success: function (data) {
                    response(data);
                },
                error: function (message) {
                    response([{'label': 'Not found!'}]);
                }
            });
        },
        minLength: 2
    });
© www.soinside.com 2019 - 2024. All rights reserved.