jQuery技术:使用codeigniter进行jquery自动完成

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

这目前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); } } 

    首先根据MVC模式,您不应该在模型中回显任何内容。 所有这些逻辑必须位于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)); } } 

    然后在您的视图中,您可以在下方或旁边的某处创建元素,以在返回结果为空时显示“无匹配”消息。

      $("#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]); }); } }); 

      以上就是jQuery教程分享使用codeigniter进行jquery自动完成相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/548240.html

      (0)
      上一篇 2021年1月13日
      下一篇 2021年1月13日

      精彩推荐