提前输入更新程序数据不匹配

编程入门 行业动态 更新时间:2024-10-11 05:21:12
本文介绍了提前输入更新程序数据不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了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"); } });

演示

更多推荐

提前输入更新程序数据不匹配

本文发布于:2023-08-03 06:28:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   程序   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!