提前输入更新程序数据不匹配 [英] typeahead updater data mismatch
本文介绍了提前输入更新程序数据不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了typeahead更新程序来进一步处理数据.在这里,我将使用我的代码.看看并帮帮我.
I tried typeahead updater for further manipulation of data. Here I go with my code. Have a look and help me out.
我的HTML:
<input type="text" class="form-control" data-provide="typeahead" name="pname" id="pname" autocomplete="off">
<table id="pytable"> <tbody></tbody>
</table>
我的JSON:
[{"pname":"iPhone4","id":"14","pid":"PRO14"},{"pname":"iphone5s","id":"16","pid":"PRO16"}]
JS:
$('#pname').typeahead({
source: function (query, process) {
names = [];
map = {};
return $.get('/'+url_root_path+'/misc/purchase/source.php', { query: query }, function (data) {
//console.log(data);
var json_obj = $.parseJSON(data);
$.each(json_obj, function (i, obj) {
map = obj;
if($.inArray(obj.pname, names)==-1)
names.push(obj.pname);
// console.log(map);
});
return process(names);
});
}, // source
updater: function(item) {
//console.log(map);
//console.log(item);
//console.log(map.id);
var item_code = map.pid; var item_cost = 0;
var item_name = item;
var item_id = map.id;
var newTr = $('<tr id="row_'+ count +'"></tr>');
newTr.html('<td><input name="product'+ count +'" type="hidden" value="'+ item_code +'"><input class="span5 tran" style="text-align:left;" name="item'+ count +'" type="text" value="'+ item_name +' ('+ item_code +')"></td><td><input class="span2 tran" name="quantity'+ count +'" type="text" value="1" onClick="this.select();"></td><td><input class="span2 tran" style="text-align:right;" name="unit_cost'+ count +'" type="text" value="'+ item_cost +'"></td><td><i class="icon-trash tip del" id="'+ count +'" title="Remove this Item" style="cursor:pointer;" data-placement="right"></i></td>');
newTr.appendTo("#pytable");
} //updater
}); //typehead
我的问题是,当我单击更新程序时,它会为每个具有相似名称的产品附加相同的pid.
My problem is, when I click through the updater it append same pid for every product with similar name.
推荐答案
您可以使用map
数组,使用obj.name
映射到obj
对象:
You can use obj.name
to map to to the obj
object using map
array :
JS:
$('#pname').typeahead({
source: function (query, process) {
names = [];
map = {};
return $.get('/'+url_root_path+'/misc/purchase/source.php', { query: query }, function (data) {
var json_obj = $.parseJSON(data);
$.each(json_obj, function (i, obj) {
map[obj.pname] = obj; //<------ make this change
if($.inArray(obj.pname, names)==-1)
names.push(obj.pname);
});
return process(names);
});
},
updater: function(item) {
//change the below line
var item_code = map[item]['pid'] , item_cost = 0, item_name = item , item_id = map[item]['id'];
var newTr = $('<tr id="row_'+ count +'"></tr>');
newTr.html('<td><input name="product'+ count +'" type="hidden" value="'+ item_code +'"><input class="span5 tran" style="text-align:left;" name="item'+ count +'" type="text" value="'+ item_name +' ('+ item_code +')"></td><td><input class="span2 tran" name="quantity'+ count +'" type="text" value="1" onClick="this.select();"></td><td><input class="span2 tran" style="text-align:right;" name="unit_cost'+ count +'" type="text" value="'+ item_cost +'"></td><td><i class="icon-trash tip del" id="'+ count +'" title="Remove this Item" style="cursor:pointer;" data-placement="right"></i></td>');
newTr.appendTo("#pytable");
}
});
这篇关于提前输入更新程序数据不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文