2015-04-15 99 views
1

我列出了第一頁上的類別,第二頁上的子類別和第三頁上的產品。現在我已將產品添加到購物車。現在將返回到類別頁面,並將產品添加到購物車。如果我們需要查看購物車中的項目,我們可以聲明一個全局數組[]並將數據添加到該數組[]中。但是,如果我添加了3個項目,則表示最近添加的最終項目僅顯示在購物車清單項目頁面中。但我想顯示添加的3個項目。你能檢查我的代碼,並找出我的問題。動態添加鈦表格中的值

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

編輯: Ti.API.info( 「DATA-長度」 +」「+ 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開發」產品。再以4數量意味着該列表現在顯示的是我會添加以「Android開發」的產品相同的產品:

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

,但我想看起來像下面:

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

請解釋更多的應用流量,你有三個觀點,第一個是類別,並且當用戶選擇一個類別,應用程序導航到包含子類別,並與產品相同的第二個觀點,我我對嗎? – Zabady

+0

@Zabady是的,你是正確的。 –

回答

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

嗨,你可以請檢查我編輯的問題,並給我一個解決方案。這正是我所遵循,需要... –

+0

你介意共享一個可運行的測試用例,以幫助排除故障。 –

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

我總共添加了3個產品...但是僅顯示在tableview.i中的最後一個產品有3個視圖。一個用於類別,當用戶選擇一個類別時,該應用導航到包含子類別和產品相同的第二個視圖。首先我添加了一個產品,然後我將返回到類別頁面,然後再添加2個產品意味着最後一個產品只顯示在最近由用戶添加的列表中,但我需要顯示所有添加的產品。你能幫我麼。 –

+0

我認爲你沒有正確地更新你的全局數組,每當你添加或刪除一個項目時,都會更新你的Ti.App.data數組。 –

+0

我如何更新全局數組?如果我點擊了按鈕,產品細節將被添加到數據[]中。我再次回到分類頁面,如果它選擇了分類,它將進入子分類頁面,如果它被選中,子分類將進入產品頁面。在這裏,我們將選擇產品並點擊按鈕意味着在產品上添加了正確的產品詳細信息數據[]。但是我已經聲明這個數據[]是全局唯一的...那麼爲什麼所有的值都沒有添加到數據[]上,最後選擇的產品值只能添加到數據[]。您能否通過代碼解釋它以更新數據[] –