修改JSON数据的显示

时间:2016-12-13 22:43:06

标签: javascript jquery

我正在尝试根据第一个返回对象中某个字段的值修改作为JSON返回的数据的显示。

以下是成功回调中返回的对象数组的示例:

JSON数据ViewPricing = N

[{
  "ItemCount": 2,
  "ViewPricing": "N"
}, {
  "ProductID": "4600033",
  "ProdMfg": "",
  "Desc": "Cambridge IR Driftwood",
  "Quantity": 1,
  "ExtPrice": " ",
  "UnitPrice": " "
}, {
  "ProductID": "4600068",
  "ProdMfg": "",
  "Desc": "Cambridge IR Charcoal Grey",
  "Quantity": 1,
  "ExtPrice": " ",
  "UnitPrice": " "
}, {
  "ItemTotExtPrice": ""
}]

JSON数据ViewPricing = Y

[{
  "ItemCount": 1,
  "ViewPricing": "Y"
}, {
  "ProductID": "60PRO50BL",
  "ProdMfg": "",
  "Desc": "Duraflo Weatherpro 50 Vent Black",
  "Quantity": 1,
  "ExtPrice": "$10.95",
  "UnitPrice": "$10.95"
}, {
  "ItemTotExtPrice": "$10.95"
}]

的jQuery

   function getPallet() {
    "use strict";
    var palletContent = $('#palletContent'),
        palletBtns = $('#palletBtns'),
        spinner = '<div class="loadingSpinner"><div class="spinner-wave"><div></div><div></div><div></div><div></div><div></div></div><span class="text-center"><b>Loading Pallet...</b></span></div>';
    palletBtns.addClass('hide');
    $.ajax({
        type: 'Get',
        url: Config.engineUrl + '?aShoppingCart',
        dataType: 'json',
        cache: false,
        beforeSend: function() {
            palletContent.empty().append(spinner);
        },
        success: function(data) {
            palletContent.empty();
            if (data[0].ItemCount === 0) {
                palletContent.html('<div class="alert alert-danger ma10"><center><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><b class="pl10">You have no items in your pallet.</b></center></div>');
            } else {
                var items = [],
                    subtotal = [];
                $.each(data, function(i, item) {
                    if (item.ViewPricing === "Y") {
                        console.log('View Pricing: True');
                        if (i > 0 && i < data.length - 1) {
                            items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td><td class="text-right">' + item.UnitPrice + '</td></tr>');
                        }
                    } else if (item.ViewPricing === "N") {
                        console.log('View Pricing: False');
                        if (i > 0 && i < data.length - 1) {
                            items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td></tr>');
                        }
                    }
                    if (i === data.length - 1) {
                        subtotal.push(item.ItemTotExtPrice);
                    }
                });
                palletContent.append('<table id="miniPallet" class="table mb0"><thead><tr><th class="text-left">Item </th><th class="text-center">Qty</th><th class="text-right">Price</th></tr></thead><tfoot><tr><td class="text-left"><strong>Subtotal</strong></td><td></td><td class="text-right"><strong>' + subtotal + '</strong.</td></tr></tfoot><tbody>' + items.join('') + '</tbody></table>');
                palletBtns.removeClass('hide');
            }
        },
        error: function() {}
    });
}

我正在尝试做什么

我想要做的是根据&#34;查看定价&#34;的价值有条件地改变商品数据。领域。

如果(item.ViewPricing ===&#34; Y&#34;),我希望推送的html /数据为:

 items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td><td class="text-right">' + item.UnitPrice + '</td></tr>');

如果(item.ViewPricing ===&#34; Y&#34;),我希望palletContent html / data为:

 palletContent.append('<table id="miniPallet" class="table mb0"><thead><tr><th class="text-left">Item </th><th class="text-center">Qty</th><th class="text-right">Price</th></tr></thead><tfoot><tr><td class="text-left"><strong>Subtotal</strong></td><td></td><td class="text-right"><strong>' + subtotal + '</strong.</td></tr></tfoot><tbody>' + items.join('') + '</tbody></table>');

如果(item.ViewPricing ===&#34; N&#34;),我希望推送数据为:

 items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td></tr>');

如果(item.ViewPricing ===&#34; N&#34;),我希望palletContent html / data为:

 palletContent.append('<table id="miniPallet" class="table mb0"><thead><tr><th class="text-left">Item </th><th class="text-center">Qty</th></tr></thead><tbody>' + items.join('') + '</tbody></table>');

已更新 - 工作代码

 function getPallet() {
        "use strict";
        var palletContent = $('#palletContent'),
            palletBtns = $('#palletBtns'),
            spinner = '<div class="loadingSpinner"><div class="spinner-wave"><div></div><div></div><div></div><div></div><div></div></div><span class="text-center"><b>Loading Pallet...</b></span></div>';
        palletBtns.addClass('hide');
        $.ajax({
            type: 'Get',
            url: Config.engineUrl + '?aShoppingCart',
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            cache: false,
            beforeSend: function() {
                palletContent.empty().append(spinner);
            },
            success: function(data) {
                palletContent.empty();
                if (data[0].ItemCount === 0) {
                    palletContent.html('<div class="alert alert-danger ma10"><center><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><b class="pl10">You have no items in your pallet.</b></center></div>');
                } else {
                var items = [],
                    subtotal = [];
                if (data[0].ViewPricing === "Y") {
                    $.each(data, function(i, item) {
                        if (i > 0 && i < data.length - 1) {
                            items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td><td class="text-right">' + item.UnitPrice + '</td></tr>');
                        }
                        if (i === data.length - 1) {
                            subtotal.push(item.ItemTotExtPrice);
                        }
                    });
                    palletContent.append('<table id="miniPallet" class="table mb0"><thead><tr><th class="text-left">Item </th><th class="text-center">Qty</th><th class="text-right">Price</th></tr></thead><tfoot><tr><td class="text-left"><strong>Subtotal</strong></td><td></td><td class="text-right"><strong>' + subtotal + '</strong.</td></tr></tfoot><tbody>' + items.join('') + '</tbody></table>');
                } else if (data[0].ViewPricing === "N") {
                    $.each(data, function(i, item) {
                        if (i > 0 && i < data.length - 1) {
                            items.push('<tr><td class="text-left"><span class="itemNum">' + item.ProductID + '</span></br><span class="desc">' + item.Desc + '</span></td><td class="text-center"><span class="qty">' + item.Quantity + '</td></tr>');
                        }
                        if (i === data.length - 1) {
                            subtotal.push(item.ItemTotExtPrice);
                        }
                    });
                    palletContent.append('<table id="miniPallet" class="table mb0"><thead><tr><th class="text-left">Item </th><th class="text-center">Qty</th></tr></thead><tbody>' + items.join('') + '</tbody></table>');
                }
                palletBtns.removeClass('hide');
}
            },
            error: function() {}
        });
    }

0 个答案:

没有答案