【问题标题】:Can't get CI autocomplete from DB to work无法从 DB 获得 CI 自动完成功能
【发布时间】:2016-07-24 13:55:04
【问题描述】:

我最近接触了 CI,还在学习。

我的控制器:

public function test() {
    $keyword=$this->input->post('search[1]');
    $data=$this->hbc_model->search_autocomplete($keyword);        
    //echo json_encode($data);

    $this->load->view('headfoot/test-header-main');
    $this->load->view('test');
    $this->load->view('headfoot/test-footer-main');
}

型号:

function search_autocomplete($search_term){
    $this->db->select('v_city_name'); 
    $this->db->like('v_city_name', $search_term);
    $response = $this->db->get('vbc_city')->result_array();
    // var_dump($response);
    return $response;
}

并查看:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $( "#main-search" ).autocomplete({
    source: 'hbc_Model/search_autocomplete'
    });
    });
</script>
</head>
<body>
<input type="text" id="main-search" name="search[1]" size="20" />
</body>

当我从模型中取消注释 vardump 甚至从控制器回显 json 响应时,它会在整个页面上显示来自数据库的城市名称,但在用作自动完成时不起作用。

任何帮助将不胜感激。

【问题讨论】:

  • .autocomplete() 函数是 jQueryUI 的一部分,所以我将这些标签添加到您的问题中。您的问题可能是 JavaScript,因此您必须在浏览器控制台中查找错误。

标签: jquery jquery-ui mysqli jquery-ui-autocomplete codeigniter-3


【解决方案1】:

自动完成需要proper formatted 源数据: 我会这样处理:

 $response = $this->db->get('vbc_city')->result_array();
 $outputString="";
foreach ($response as $city)
$outputString.="'".$city['v_city_name']."',";

print "[".substr(SoutputString,0,-1)."]";
die; // or else the page html will be printed too!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2015-03-24
    • 2011-11-24
    • 2019-05-12
    • 1970-01-01
    • 2012-09-22
    • 2011-09-30
    相关资源
    最近更新 更多