计算选定父级在AJAX调用中返回的结果

时间:2019-02-18 15:06:52

标签: javascript jquery ajax

我的第一个弹出列表与AJAX调用中的customers不同,cust_id也不同。我正在尝试获取与所选客户相同的cust_id行数。

例如:如果我从非重复列表中选择“ Jeffs Music”,则我需要计数返回5,因为cust_id = "15"

有5行
$('.add-invoice').live('click', function() {
  $("#invoice_div").css("display", "block");

  $.ajax({
    url: 'invoice_fill.php',
    data: {
      action: "invoice"
    },
    dataType: 'json',
    success: function(data) {
      var result = [];
      $.each(data, function(i, e) {
        var matchingItems = $.grep(result, function(item) {
          return item.customer === e.customer && item.cust_id === e.cust_id;
          console.log(item.cust_id);
        });
        if (matchingItems.length === 0) {
          result.push(e);
        }
      });

      var xyz = (JSON.stringify(result));
      console.log(xyz);

      populateSelectBoxes($('#invoice_div #ddinvoice'), result);

      function populateSelectBoxes($select, result) {
        var invoices = [];

        $.each(result, function() {
          invoices.push('<li data-value="' + this.cust_id + '">' + this.customer + ' : ' + this.invoice + '</li>');
        });
        $select.append(invoices.join(''));
      }

      function populateTableRow(data, selectedProductAutonum) {
        var invoices;
        $.each(result, function() {
          if (this.cust_id == selectedProductAutonum) {
            invoices = this;

            var arr = this.cust_id;
            var occurrences = {};
            for (var i = 0, j = arr.length; i < j; i++) {
              occurrences[arr[i]] = (occurrences[arr[i]] || 0) + 1;
            }
            console.log(occurrences);

            return false;
            // changed autonun to cust_id to give unique record/product call (changed in line 248 as well)
          }
        });

        $(".item-row:last").after(
          '<tr class="item-row"><td class="item-name"><div class="delete-wpr"><textarea form ="testinsert" name="item_name[]">Item Name</textarea><a class="delete" href="javascript:;" title="Remove row">X</a><a class="add-product" href="javascript:;" title="Add Product">A</a></div></td><td class="description"><textarea form ="testinsert" name="item_desc[]">Description</textarea></td><td><textarea class="cost" form ="testinsert" name="item_cost[]">$0</textarea></td><td><textarea class="qty" form ="testinsert" name="item_qty[]">0</textarea></td><td><span class="price" form ="testinsert" name="item_price[]">$0</span></td></tr>');
        if ($(".delete").length > 0) $(".delete").show();
        bind();

        $('#address-title').val(invoices.customer);
        $('#address-one').val(invoices.address);
        //$('#address-two').val(invoices.sales + '\n' + invoices.owed);
        //$('#address-three').val(invoices.address3);
        ///////////////////////////////////////////
        $('#invoice_num').val(invoices.invoice);
        $('#paid').val(invoices.paid);
        $('#owed').val(invoices.sales);
        $('#auto_num').val(invoices.autonum);
        ///////////////////////////////////////////
        $('[name="item_name[]"]').val(invoices.product);
        $('[name="item_desc[]"]').val(invoices.description);
        $('[name="item_cost[]"]').val(invoices.cost);
        $('[name="item_qty[]"]').val(invoices.quantity);
        $('[name="item_price[]"]').val(invoices.price);
      }

      $('#invoice_div #ddinvoice li').click(function(e) {
        var selection = $(this).attr("data-value");
        $(this).parent().parent().parent().hide();
        populateTableRow(data, selection);
        $('ul').empty();
      });
    }
  });
  update_total();
});

查看this jsFiddle以获取完整代码。这应该很清楚,但是AJAX调用在其中不起作用?

0 个答案:

没有答案