【问题标题】:jquery autocomplete with codeigniter使用codeigniter的jquery自动完成
【发布时间】:2013-04-03 17:57:15
【问题描述】:

我正在使用带有 codeigniter 框架的 jQuery 自动完成功能。

这目前 100% 有效。

我的模型是:

  function get_sku_code($q){
    $this->db->select('ProductCode');
    $this->db->like('ProductCode', $q);
    $query = $this->db->get('ProductList');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['ProductCode'])); //build an array
      }
      $this->output->set_content_type('application/json')->set_output(json_encode($row_set));

    }
  }

我的视图 javascript 是:

 $("#product").autocomplete( 
 { 
 source: "get_sku_codes", 
 messages: 
 { 
 noResults: '', 
 results: function() {} 
 }, 
 select: function( event, ui ) 
 { 
  var selectedObj = ui.item; 
 $.post('get_sku_prices', {data:selectedObj.value},function(result) { 
 $("#product").parent().parent().find('input[id^="price"]').val(result[0]);
 $("#product").parent().parent().find('input[id^="adjustedprice"]').val(result[0]);
 }); 
 } 
 });

如上所述,这 100% 有效。我遇到的一个问题是,如果没有数据库匹配,自动完成列表只是空白。当模型没有返回值时,有没有办法返回“数据库中没有匹配项”?我应该使用 jquery 还是使用 codeigniter mysql 请求来执行此操作?

一如既往的感谢,

控制器 - get_sku_codes

 function get_sku_codes(){
    $this->load->model('Sales_model');
    if (isset($_GET['term'])){
      $q = strtolower($_GET['term']);
      $this->Sales_model->get_sku_code($q);
    }
  }

【问题讨论】:

    标签: jquery codeigniter autocomplete


    【解决方案1】:

    首先,根据MVC 模式,您不应在模型中回显任何内容。所有这些逻辑都必须在 View 从中获取数据的 Controller 中。

    将您的模型更改为:

      function get_sku_code($q){
        $this->db->select('ProductCode');
        $this->db->like('ProductCode', $q);
        $query = $this->db->get('ProductList');
        $row_set = array();
        if($query->num_rows > 0){
          foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row['ProductCode'])); //build an array
          }
         return $row_set;
      }
    

    还有你的控制器:

      function get_sku_codes(){
        $this->load->model('Sales_model');
        if (isset($_GET['term'])){
          $q = strtolower($_GET['term']);
          $output = $this->Sales_model->get_sku_code($q);
          $this->output->set_content_type('application/json')->set_output(json_encode($output));
        }
      }
    

    然后在您的视图中,您可以在<input id="product"> 下方或旁边的某个位置创建一个<span id="noMatches"></span> 元素,以在返回结果为空时显示“无匹配”消息。

        $("#product").autocomplete(
     { 
        source: "get_sku_codes", 
        response: function(event, ui) {
            if (ui.content.length === 0) {
                $("#noMatches").html("No matches");
            } else {
                $("#noMatches").empty();
            }
        },
        select: function( event, ui ) 
        { 
            var selectedObj = ui.item; 
             $.post('get_sku_prices', {data:selectedObj.value},function(result) { 
                 $("#product").parent().parent().find('input[id^="price"]').val(result[0]);
                 $("#product").parent().parent().find('input[id^="adjustedprice"]').val(result[0]);
             }); 
        } 
     });
    

    【讨论】:

    • 谢谢@Sport Billy,你是对的,但是当我填充时,什么都没有出现,只是“什么都没有”。关于为什么的任何想法?谢谢。
    • 嗨 Sports Billy,感谢您的努力,感谢。我按照您的建议进行了尝试,但仍然是空白 :-( 开发人员工具 XHR 根本没有显示任何 HTML 响应,因此不确定它是否被正确传递。还有其他建议吗?再次感谢。
    • 嗨,比利,在我的模型中,问题已经出现了。谢谢。
    • 那你有一个错字。在您的模型中,您有 get_sku_code 而不是 JS 中的 get_sku_codes。此外,您还需要一个变量 $q 来从 db 中获取结果,但是在使用 jQuery .autocomplete() 时您不会发送任何数据。您说它正在工作并且正在查看结果(如果有的话),但我无法弄清楚它是如何做到的。
    • 对不起,比利,你是对的。 get_sku_code 是我的模型,我在问题中添加了 get_sku_codes(控制器)。再次感谢。
    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多