【问题标题】:How to make autocomplete on HMVC codeigniter?如何在 HMVC codeigniter 上进行自动完成?
【发布时间】:2016-10-09 14:17:07
【问题描述】:

我有 HMVC codeigniter 项目,我想在字段上创建自动完成,但无论我尝试它都行不通,我使用的是 CI 3,php 5.5 +

这是我的控制器(测试):

function getData() {

    $name = $this->uri->segment(3);
    $query = $this->ex_model->getThisData()->like('name', $name)->get();

    foreach ($query->result() as $q) {
        $res['query'] = $name;
        $res['suggestions'][] = array(
            'value'=>$q->name,
            'gender'=>$q->gender,
            'id'=>$q->id,
        );
    }
    echo json_encode($res);
}

因此,我的模型(ex_model):

function getThisData(){
    $this->db->select('name');
    $this->db->from('table1');
    $this->db->join('table2', 'table1.id= table2.id');
    $query = $this->db->get();
    return $query->row_array();
}

最后,我的观点 + JavaScript:

<input  type="search" id="data1" placeholder="Input Name..." class="form-control border-input">

var site = "<?php echo base_url();?>";
$(function(){

    $('#data1').autocomplete({
        serviceURL: site+'/test/getData',

        onSelect:function(suggestion){
            $('#id').val(''+suggestion.id);
            $('#gender').val(''+suggestion.gender);
        }
    });
});

那一刻我的网址:

http://localhost/myproject/admin/test

我不知道 HMVC ci 上的 URI 段是如何工作的,我尝试使用段 (4) 和 (5) 仍然不显示自动完成。

没有错误,但输入字段上的数据不会显示,我想知道我是否应该修复“$this->uri->segment(3)”或我的 javascripts 或其他。

【问题讨论】:

  • 你得到了什么不起作用?
  • @devpro,没有错误,但输入字段上的数据不会显示,我想知道我是否应该修复“$this->uri->segment(3)”或我的 javascripts否则
  • 查看答案,这将帮助你

标签: php codeigniter codeigniter-3


【解决方案1】:

根据您的网址:

http://localhost/myproject/admin/test/getData

您无法获取分段 (4) 和 (5) 中的值。

你需要这样使用:

$this->uri->segment(1); // admin
$this->uri->segment(2); // test
$this->uri->segment(3); // getData

您也可以按照用户手册:https://www.codeigniter.com/userguide3/libraries/uri.html

根据手册和你的网址:

您的细分应该是:

1- admin 
2- test 
3- getData

【讨论】:

  • 非常感谢,但是我应该如何处理我的 javascript?我应该修复“base_url”还是“serviceURL”?数据仍然不会显示顺便说一句
  • @teletubbies 使用段作为你使用这个 var site = "";
  • 仍然不会在输入字段上显示数据列表,我遵循一些没有使用 Wiredezignz HMVC 扩展的教程
【解决方案2】:

在 codeigniter 中,段在主域名称之后开始:

例如:http://localhost/myproject/admin/test/getData 这里 http://localhost/myproject 是主域,因此该段将从 admin 开始。 这里 admin 是第 1 个段 test 是第二个,getData 是第三个。

【讨论】:

  • 感谢您的回答,但主要问题是自动完成不会在输入字段中显示数据
  • 的输出是什么。我的意思是,管理员是否包含在 base_url() 中?
  • localhost/myproject,我使用wiredezignz HMVC扩展btw
  • 把这个serviceURL:site+'/test/getData'改成这个serviceURL:site+'/admin/test/getData'
【解决方案3】:

不检查 JS 代码,你的控制器和模型不适合:

  1. 您的模型已经返回数组(或 FALSE),因此不需要

    $query = $this->ex_model->getThisData()->like('name', $name)->get();
    

应该是:

$query = $this->ex_model->getThisData();
  1. 您正在尝试按参数过滤查询,因此您需要将其传递给模型:

    $name = $this->uri->segment(4) ? $this->uri->segment(4) : null;
    
    $query = $this->ex_model->getThisData($name);
    
  2. 现在您的模型的方法代码应如下所示:

    public function getThisData($name)
    {
        if ($name != null) {
            $this->db->select('name');//should this be '*'?
            $this->db->from('table1');
            $this->db->where('name', $name)
            $this->db->join('table2', 'table1.id= table2.id');
            $query = $this->db->get();
            return $query->result_array();//remember if you return array or object and you are probably looking for result, but not for single row here?
        }
        //case when $name value from controller is null
        return false;
    }
    

【讨论】:

  • 感谢您的回答,我试过了,但自动完成功能仍然无法正常工作,我只是对我的项目进行了一些更改,所以我的网址如:localhost/myproject/admin/test 我应该将 uri 段更改为 3 吗?
猜你喜欢
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 2012-06-04
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多