自动完成 jquery codeigniter 错误

autocomplete jquery codeigniter error

我在 Jquery 中使用 Codeigniter。 jquery 自动完成有问题。当我输入关键字时,它显示空的结果列表。

控制器:

function suggestions()
{
    $term = $this->input->get('term', TRUE);
    if (strlen($term) < 1) {
        die();
    }
    $rows = $this->products_model->getProductNames($term);
    if ($rows) {
        foreach ($rows as $row) {
            $pr[] = array('id' => $row->id, 'name' => $row->name, 'brand' => $row->brand, 'stock' => $row->stock, 'price' => $row->price, 'hsn' => $row->hsn, 'gst' => $row->gst, 'size' => $row->size);
        }
        $this->sim->send_json($pr);
    }
    echo FALSE;

}

型号

public function getProductNames($term, $limit = 5)
{
    $this->db->like('brand', $term, 'both');
    $this->db->limit($limit);
    $q = $this->db->get('products');
    if ($q->num_rows() > 0) {
        foreach (($q->result()) as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return FALSE;
}

查看:

<td>
                    <div class="input-group">
                        <?= form_input('product[]', $_POST['product'][$r-1], 'id="product-'.$r.'" class="form-control input-sm suggestions" maxlength="80" style="min-width:270px;"'); ?>
                        <span class="input-group-addon"><i class="fa fa-file-text-o pointer details"></i></span>
                    </div>
                    <div class="details-con details-con-0<?= $r; ?>"<?= $_POST['details'][$r-1] ? '' : ' style="display:none;"'; ?>>
                        <?= form_textarea('details[]', $_POST['details'][$r-1], 'class="form-control details" id="details-'.$r.'" maxlength="255" style="margin-top:5px;padding:5px 10px;height:60px;"');?>
                    </div>
                </td>

脚本:

 $(".suggestions").autocomplete({
    source: Site.base_url+'products/suggestions',
    select: function (event, ui) {

        var row = $(this).closest('tr');
        var sel_item = ui.item;
        row.find('.price').val(sel_item.price);
        rate_origin = sel_item.price;
        var data = $('#order_tax').val();
        var price = parseInt(row.find('.price').val());
        if(data == 'incl'){
            var gst = parseFloat(price/1.28 * 0.28);
        }else{
            var gst = parseFloat(price * 0.28);
        }
        //console.log('THIS IS MAIN PRICE TEST : '+formatDecimal(gst));
        row.find('.hsn').val(sel_item.hsn);
        row.find('.cgst').val(gst/2);
        var igst = $('#gst').val();
        if(igst == 'sgst'){
            row.find('.igst').val('NIL');
            row.find('.sgst').val(gst/2);
        }else {
            row.find('.sgst').val('NIL');
            row.find('.igst').val(gst/2);
        }

        rate = row.find('.price').val();
        var cgst = row.find('.cgst').val();
        var sgst = row.find('.sgst').val();
        var igst = row.find('.igst').val();
        var change_price = 0;

        if(data == 'incl'){
            if($('#gst').val() === 'sgst'){
                change_price = rate / 1.28;
            }else {
                change_price = rate / 1.28;
            }
        }else{

            if($('#gst').val() === 'sgst'){
                change_price = rate;
            }else {
                change_price = rate;
            }
        }

        row.find('.price').val(change_price);

        if(row.find('.sqft').val() == 0){
            row.find('.sqft').val(1).change();
        }
        if (sel_item.brand != '' && sel_item.brand != null) {
            row.find('.details-con').css('display', 'block');
            row.find('.details').val(sel_item.brand+' - Stock : '+sel_item.stock);
        }
        calculateTotal();
    },
    minLength: 1,
    autoFocus: false,
    delay: 250,
    response: function (event, ui) {
        if (ui.content.length == 1 && ui.content[0].id != 0) {
            ui.item = ui.content[0];
            console.log(ui.item.name);
            $(this).val(ui.item.name);
            $(this).removeClass('ui-autocomplete-loading');
        }
    },
});

它接收数据但不显示它anymore.I知道问题出在哪里吗???

特此附上我的错误截图 等待最好的answers.Thank你。

Before

控制器:

function suggestions()
{
    $term = $this->input->get('term', TRUE);
    if (strlen($term) < 1) {
        die();
    }
    $rows = $this->products_model->getProductNames($term);
    if ($rows) {
        foreach ($rows as $row) {
            $pr[] = array('id' => $row->id, 'name' => $row->name, 'brand' => $row->brand, 'stock' => $row->stock, 'price' => $row->price, 'hsn' => $row->hsn, 'gst' => $row->gst, 'size' => $row->size);
        }
        $this->sim->send_json($pr);
    }
    echo FALSE;

}

After

function suggestions()
{
    $term = $this->input->get('term', TRUE);
    if (strlen($term) < 1) {
        die();
    }
    $rows = $this->products_model->getProductNames($term);
    if ($rows) {
        foreach ($rows as $row) {
            $pr[] = array('id' => $row->id, 'label' => $row->name, 'brand' => $row->brand, 'stock' => $row->stock, 'price' => $row->price, 'hsn' => $row->hsn, 'gst' => $row->gst, 'size' => $row->size);
        }
        $this->sim->send_json($pr);
    }
    echo FALSE;

}

当我们尝试从 PHP 或任何服务器端编程语言获取 JSON 数据时,我们应该使用名为 label 的键。它应该包含一个值,该值在触发自动完成功能时显示。所以它对我来说就像是空洞的建议。编码愉快。