【问题标题】:How to add the textfield values to the array dynamically?如何动态地将文本字段值添加到数组中?
【发布时间】:2014-03-22 22:46:14
【问题描述】:

我有两个字段,例如产品和数量。在字段中输入值后,必须将其存储在相应的多维数组中。这可能会发生多次,因为每当输入新值时,它必须与现有值相加并在表格视图中表示。

【问题讨论】:

  • 你到现在都做了什么。更新我们的详细信息?
  • 您开发的平台是什么(iOS / Android)?需要@ShaikMahaboobBasha 说的详细信息

标签: javascript android iphone titanium appcelerator


【解决方案1】:

输入 Apple 或 Orange,然后输入一个数字。单击添加,它会更新表格视图。如果您没有在数量中输入数字,我不确定会发生什么,因为我没有测试它。

要添加新产品(例如香蕉),请输入不存在的产品。它会将其添加到数组中,然后汇总之后添加的任何其他产品。

此示例始终使用产品名称来区分大小写。

由于这是 Titanium 代码并且只使用基本对象,它应该在 Android 和 IOS 中都可以工作,但我只测试了 IOS。

var win = Ti.UI.createWindow({
    layout: 'vertical',
    backgroundColor: "white"
});

var products = [{
        productName: "Apple",
        quantity: 5 
    },
    {
        productName: "Orange",
        quantity: 10
    }];

var view = Ti.UI.createView({
    top: 0,
    layout: "horizontal",
    height: Ti.UI.SIZE
});

var productView = Ti.UI.createView({
    width: "50%",
    height: Ti.UI.SIZE,
    layout: "vertical"
});
view.add(productView);

var productName = Ti.UI.createLabel({
    text: "Product Name:"
});
productView.add(productName);

var product = Ti.UI.createTextField({
    width: "90%",
    borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
productView.add(product);

var quantityView = Ti.UI.createView({
    width: "50%",
    height: Ti.UI.SIZE,
    layout: "vertical"
});
view.add(quantityView);

var productQuantity = Ti.UI.createLabel({
    text: "Quantity: "
});
quantityView.add(productQuantity);

var quantity = Ti.UI.createTextField({
    width: "90%",
    borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});

quantityView.add(quantity);
win.add(view);

var button = Ti.UI.createButton({
    title: "Add",
    height: Ti.UI.SIZE,
    width: Ti.UI.SIZE
});
win.add(button);

function createRow(params){
    var row = Ti.UI.createTableViewRow({
        height: "50dp",
        layout: "horizontal"
    });
    var product = Ti.UI.createLabel({
        text: params.productName
    });
    row.add(product);
    var quantity = Ti.UI.createLabel({
        left: "20dp",
        text: params.quantity
    });
    row.add(quantity);
    return row;
}

button.addEventListener('click', function(e){
    var index = -1;
    for(var i = 0; i < products.length; i++){
        if(products[i].productName === product.value){
            index = i;
        }
    }
    if(index > -1){
        products[index].quantity = parseFloat(products[index].quantity) + parseFloat(quantity.value);
        rows[index] = createRow(products[index]);

    } else {
        var newProduct = {
            productName: product.value,
            quantity: quantity.value
        };
        products.push(newProduct);
        rows.push(createRow(newProduct));
    }
    table.setData(rows);
});

var rows = [];

var table = Ti.UI.createTableView({
    top: "10dp"
});
win.add(table);

for(var i = 0; i < products.length; i++){
    rows.push(createRow(products[i]));
}

table.setData(rows);
win.open();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多