这是我真正想要实现的目标。我可以添加多个文本框并自动填充每个文本框。但代码不起作用。
模型 - 我在哪里查询表
function __construct(){
// DATABASE CONNECTION
$this->db_mlm = $this->load->database('mlm',TRUE);
}
function auto_product(){
//QUERY THE TABLE
$auto = $this->db_mlm->query("SELECT productCode, productName, qty FROM product where qty !=0 and UPPER($type) LIKE '".strtoupper($name)."%'");
return $auto->result();
}
}
控制器 - 我不认为发布数据会将值传递给模型
class Compose_product extends CI_Controller {
function __construct(){
parent::__construct();
$this->is_logged_in();
}
public function index()
{
if(!empty($_POST['type'])){
$type = $_POST['type'];
$name = $_POST['name_startsWith'];
//AUTO COMPLETE MODEL
$this->load->model('Autocomplete_product');
$rows = $this->Autocomplete_product->auto_product();
$data = array();
foreach($rows as $row ){
$name = $row->productCode.'|'.$row->productName.'|'.$row->qty;
array_push($data, $name);
}
echo json_encode($data);
}
$this->load->view('includes/header');
$this->load->view('compose_product');
$this->load->view('includes/sidebar');
$this->load->view('includes/footer');
}
查看
<tbody>
<tr>
<td><input class="case" type="checkbox"/></td>
<td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_1" class="autocomplete_txt" autocomplete="off"></td>
<td><input type="text" data-type="productName" name="itemName[]" id="itemName_1" class="autocomplete_txt" autocomplete="off"></td>
<td><input type="number" name="quantity[]" id="quantity_1" class="changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
</tr>
</tbody>
AJAX - 请参阅此代码。
//autocomplete script
$(document).on('focus','.autocomplete_txt',function(){
type = $(this).data('type');
if(type =='productCode' )autoTypeNo=0;
if(type =='productName' )autoTypeNo=1;
$(this).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'http://localhost/hq.cni-system.com/compose_product',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: type
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[autoTypeNo],
value: code[autoTypeNo],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#itemNo_'+id[1]).val(names[0]);
$('#itemName_'+id[1]).val(names[1]);
$('#quantity_'+id[1]).val(1);
calculateTotal();
}
});
});