【问题标题】:autocomplete in codeigniter not displaying anythingcodeigniter中的自动完成不显示任何内容
【发布时间】:2018-01-18 11:18:46
【问题描述】:

嗨,我正在 codeigniter 中创建一个自动完成搜索字段,我尝试使用以下代码在 codeignter 中进行自动完成。但它对我不起作用。在自动完成文本字段中不显示任何内容。任何人都可以找到问题

数据显示为未定义的成功函数

这是我的看法:

View
 <script >
     $(document).ready(function() {
 $("#trackerid").change(function(){
        var var_locoid= $("#trackerid option:selected").val();
        alert(var_locoid);
        $( "#deliverylocation" ).autocomplete({

            source: function(request, response) {
                var auto_data= $("#deliverylocation").val();
                      alert(auto_data);

                //alert(var_locoid);
               $.ajax({
                 url:"http://localhost/testcontroller/test/lookup",
                 type:"POST",
                 datatype:'json',
                 data:{'var_locoid' :var_locoid,'auto_data':auto_data},
                 success: function(data){
                    response(data);
                    alert(data);
                }
            });
        },
        minLength: 1
        });
    });});
     </script>

                                <input type="text" name="deliverylocation" id="deliverylocation" placeholder="Enter your area " >

控制器

    function lookup()
{
   $citys = $this->input->post('var_locoid'); 
      $auto_text = $this->input->post('auto_data');  

                    //var_dump($citys);
         //$data['response'] = 'false'; 
  $place = $this->Demo_model->load_places($citys,array('keyword' => $auto_text));   
    $json_array = array();
    foreach ($place as $row){
    array_push($json_array, $row->locationtext);
    }
           //echo json_encode($place);
   echo json_encode($json_array);
}



Model

function load_places($citys,$auto_text)
    {
        //$this->db->select('locationtext');
        $this->db->select('locationtext');
        $this->db->like('locationtext', $auto_text);
        $this->db->from('tbl_location');
        $array = array('cityautocode' => $citys);
        $this->db->where($array);
        //$this->db->->where('cityautcode', $place);
         $query= $this->db->get();
        //echo $this->db->last_query();

          return $query->result();

    }

【问题讨论】:

    标签: javascript jquery codeigniter autocomplete


    【解决方案1】:

    我正在使用来自 https://www.devbridge.com/sourcery/components/jquery-autocomplete/ 的自动完成模式

    这是我的控制器:

    $arrayData = $this->CRUD_model->getSelect("id as data, name as value, purchase_price","articles")->result_array();
        echo json_encode($arrayData);
    

    还有我的看法:

    $(document).ready(function(){
        // $("body").addClass("sidebar-collapse");
        var dataArticles = $.parseJSON(getArticles());
        $("input[name='articles_name']").devbridgeAutocomplete({
            lookup: dataArticles,
            onSelect: function(suggestion) {
                $("input[name='articles_id']").val(suggestion.data);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Barang tidak ditemukan',
            noCache: true,
            minChars: 2,
        })
    })
    
    function getArticles(){
        result = null;
        $.ajax({url: '<?= base_url();?>panel/purchasing/request/getArticles',type: 'get',dataType: 'html',async: false,success: function(data) {result = data;} });
        return result;
    }
    

    对于这个例子,我使用查找,所以当我打开页面时会加载所有自动完成值,注意 id 必须命名为“data”,label 必须命名为“value”。

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 2017-10-18
      • 1970-01-01
      • 2016-09-20
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多