jQuery的自动完成与codeigniter [英] jquery autocomplete with codeigniter
问题描述
我正在使用带有Codeigniter框架的jQuery自动完成功能.
I am using a jQuery autocomplete with the codeigniter framework.
这目前可以100%使用.
This currently works 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是:
My View javascript is:
$("#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请求来做到这一点?
As mentioned this works 100%. The one issue I am having is that if there is no database match, the autocomplete list is simply blank. Is there a way to return 'no matches in database' when the model returns no values? should I do this with jquery or with the codeigniter mysql request?
一如既往的感谢,
控制器-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);
}
}
推荐答案
First of all according to the MVC pattern you should not echo anything in your Model. All this logic must be at the Controller from which the View will get the data.
将模型更改为此:
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>
元素,以在返回结果为空时显示没有匹配项"消息.
Then in your View you can create a <span id="noMatches"></span>
element somewhere below or next to the <input id="product">
to display your 'No matches' message when the returning result is empty.
$("#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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!