在钛tableview中动态添加值

时间:2015-04-15 12:43:43

标签: javascript iphone arraylist titanium tableview

我列出了第1页的类别,第2页的子类别和第3页的产品。现在我已将产品添加到购物车中。现在将再次返回类别页面,并将产品添加到购物车。如果我们需要查看购物车中的商品,我们可以声明一个全局数组[]并将数据添加到该数组[]。但是,如果我添加了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);

现在我添加了不同数量的相同产品意味着产品单独列出。但如果我添加了不同数量的相同产品,则意味着要在列表中单次列出产品名称。但数量需要在产品上更新。

对于Eg: 如果我在购物车中添加了2个数量的“Android开发”产品。我将再次使用“Android开发”产品添加相同的产品,其中4个数量意味着该列表正在显示:

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

但我希望看起来如下:

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

3 个答案:

答案 0 :(得分:0)

您必须将数据数组分配给表的data属性,以便将这些对象显示为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;

答案 1 :(得分:0)

试试这个,

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

答案 2 :(得分:0)

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

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);
相关问题