动态添加钛表视图中的值

Dynamically add the values in titanium tableview

我在第一页列出了类别,在第二页列出了子类别,在第三页列出了产品。现在我已将产品添加到购物车。现在将再次返回类别页面并将产品添加到购物车。如果我们需要查看购物车中的商品,我们可以声明一个全局数组 [] 并将数据添加到该数组 []。但是在这里,如果我添加了 3 个项目,则意味着最近添加的最后一个项目只显示在购物车列表项目页面中。但我想显示添加的 3 个项目。你能检查我的代码并找出我的问题吗?

Ti.App.data = [];
data=[
            {title:productlist_product,
            price:productlist_product_price,
            quantity:selectedqty},
         ];
var myObjectString = JSON.stringify(data);

编辑: Ti.API.info("DATA-length"+" "+data.length);

这里得到的值为 1。但是在包含 3 种产品的购物车页面中。我需要将这 3 个产品值添加到数据 []。你能给我解决方案吗?

编辑:

现在我已经使用下面的代码在数组[]中动态添加了值。

 data = [
{title:productlist_product,
price:productlist_product_price,
image:productlist_product_image,
quantity:selectedqty},
];

// append new value to the array
 data.push({title:productlist_product,
            price:productlist_product_price,
            image:productlist_product_image,
            quantity:selectedqty});

     // display all values
 for (var i = 0; i < data.length; i++) {
 console.log(data[i]);
   }
   var myObjectString = JSON.stringify(data);

  Ti.API.info("PRICE"+" "+myObjectString);

现在我添加了相同的产品但数量不同意味着该产品是分开列出的。但是如果我添加了不同数量的相同产品意味着要在列表中一次列出产品名称。但数量需要更新产品。

例如: 如果我在购物车中添加了数量为 2 的 "Android development " 产品。我将再次添加相同的产品 "Android development" 数量为 4 的产品意味着列表正在显示:

 PRICE [{"title":"Android development","quantity":2},
        {"title":"Android development","quantity":4}]

但我想看起来像下面这样:

 PRICE [{"title":"Android development","quantity":6}]

您必须将数据数组分配给 table 的数据 属性,以便将这些对象显示为 tableView 行。比方说:

Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow();
var tableData = [ {title: 'Apples'}, {title: 'Bananas'}, {title: 'Carrots'}, {title: 'Potatoes'} ];

var table = Ti.UI.createTableView({
data: tableData});
win.add(table);
win.open();

或者,您可能还想使用 table.setData 添加行 customTableView.setData(数据);

var customTableView = Titanium.UI.createTableView({ BackgroundColor:'White', style: Titanium.UI.iPhone.TableViewStyle.GROUPED, });
data.push({title:productlist_product, price: productlist_product_price,quantity: selectedqty});
customTableView.data = data;

试试这个,

var productData = [
                    {Product_Name: 'Apples',Product_Price :50,Product_qty :5}, 
                    {Product_Name: 'Banana',Product_Price :40,Product_qty :5},
                    {Product_Name: 'Carrots',Product_Price :90,Product_qty :5}
                ];
var tableData = [];
for(var i=0;i<data.length;i++){
    var tableRow = Ti.UI.createTableViewRow({
        title :"Name :"+data[i].Product_Name+"      Price : "+data[i].Product_Price,
    });
    tableData.push(tableRow);
}

var tableView = Ti.UI.createTableView({
    data: tableData
});
win.add(tableView);

现在我已经使用下面的代码在数组[]中动态添加了值。

data = [
{title:productlist_product,
price:productlist_product_price,
image:productlist_product_image,
quantity:selectedqty},
];

// append new value to the array
 data.push({title:productlist_product,
            price:productlist_product_price,
            image:productlist_product_image,
            quantity:selectedqty});

     // display all values
 for (var i = 0; i < data.length; i++) {
 console.log(data[i]);
   }
   var myObjectString = JSON.stringify(data);

  Ti.API.info("PRICE"+" "+myObjectString);