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% 機能します。私が抱えている問題の 1 つは、一致するデータベースがない場合、オートコンプリート リストが空白になることです。モデルが値を返さないときに「データベースに一致がありません」を返す方法はありますか? 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);
}
}