【问题标题】:typeahead updater data mismatch预输入更新程序数据不匹配
【发布时间】:2014-04-13 16:51:11
【问题描述】:

我尝试使用 typeahead updater 来进一步处理数据。在这里,我使用我的代码。看看并帮助我。

我的 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。

【问题讨论】:

  • count 初始化在哪里?

标签: json typeahead.js bootstrap-typeahead


【解决方案1】:

您可以使用obj.name 映射到使用map 数组的obj 对象:

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"); 
   } 
  }); 

DEMO

【讨论】:

  • 未捕获类型错误:无法读取未定义的属性“pid”,出现此错误。
  • @Eric 哦,这是一个错字,map[item]['pid'] 不是 map['item']['pid'],更新了代码。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多