1
votes

I have created a tableview row.here the tableview each row having the one lebel and 2 image view.if am clicking the image, the selected row of label value need to change something.

But from my code if am clicking the image, the label value is changed of last item. What's wrong in my code.

for( var i=0; i<data.length; i++){ 
    var row = Ti.UI.createTableViewRow({
    layout : 'horizontal',
    width: "100%",
    height: Ti.UI.SIZE,
});
row.add(Ti.UI.createImageView({
    image: data[i].image,
    top: 5,
    width: '50',
    height: Ti.UI.SIZE,
}));
row.add(Ti.UI.createLabel({
    text: data[i].name,
    top: 5,
    width: 180,
    color: '#040404',
    height: Ti.UI.SIZE,
}));

var deleteitem = Ti.UI.createImageView({
    image: "images/back.png",
    top: 5,
    i:i,
    squantity : data[i].itemCount,
    spprice :data[i].itemPrice,
    width: Ti.UI.SIZE,
    height: Ti.UI.SIZE,
    myrow: row,
});
row.add(deleteitem);
var adddeleteitemview = Ti.UI.createView({
    width: Ti.UI.SIZE,
    height: Ti.UI.SIZE,
    layout : 'horizontal',
    left:10,
    borderColor:"gray",
    borderRadius:"10"
});
var removeitem = Ti.UI.createImageView({
    image: "images/minus.jpg",
    top: 5,
    width: Ti.UI.SIZE,
    squantity : data[i].itemCount,
    spprice :data[i].itemPrice,
    height: Ti.UI.SIZE,
});
adddeleteitemview.add(removeitem);
var itemcounttext = Ti.UI.createLabel({
    top: 5,
    text:data[i].itemCount,
    width: Ti.UI.SIZE,
    height: Ti.UI.SIZE,
});
adddeleteitemview.add(itemcounttext);
removeitem.addEventListener('click', function(e) {
    squantity = e.source.squantity;
    spprice = e.source.spprice;
    totalqty = Number(totalqty) - Number(1);
    $.ViewCartItemslist_header_cart.text = totalqty;
    totalamount = Number(totalamount) - Number((spprice));
    squantity = Number(squantity) - 1;
    itemcounttext.text=squantity;
    $.ViewCartItemslist_total_value.text = totalamount;
});

var additem = Ti.UI.createImageView({
    image: "images/plus.jpg",
    top: 5,
    width: Ti.UI.SIZE,
    squantity : data[i].itemCount,
    spprice :data[i].itemPrice,
    height: Ti.UI.SIZE,
});
    adddeleteitemview.add(additem);
    additem.addEventListener('click', function(e) {
        squantity = e.source.squantity;
        spprice = e.source.spprice;
        totalqty = Number(totalqty) + Number(1);
        $.ViewCartItemslist_header_cart.text = totalqty;
        totalamount = Number(totalamount) + Number((spprice));
        squantity = Number(squantity) + 1;
        itemcounttext.text=squantity;
        $.ViewCartItemslist_total_value.text = totalamount;
    });
    row.add(adddeleteitemview); 

    dataArray.push(row);
    row.addEventListener('click', function() {});
    deleteitem.addEventListener('click', function(e) {
        squantity = e.source.squantity;
        spprice = e.source.spprice;
        $.ViewCartItemstableView.deleteRow(e.source.myrow);
        totalqty = Number(totalqty) - Number(squantity);
        $.ViewCartItemslist_header_cart.text = totalqty;
        totalamount = Number(totalamount) - Number((spprice * squantity));
        $.ViewCartItemslist_total_value.text = totalamount;
    });   
};
$.ViewCartItemstableView.setData(dataArray);
}

EDIT:

From Tony answer am getting the little bit improvement.,

removeitem.addEventListener("click",function(e){
var item=e.source;
item.getParent(); 
Ti.API.info("parent"+item.getParent());
 });

here am getting the results like :

parent[object TiUIView]

how can i modify the text for

 var itemcounttext = Ti.UI.createLabel({
        top: 5,
        text:data[i].itemCount,
        width: Ti.UI.SIZE,
        height: Ti.UI.SIZE,
    });

How can i get this label id value and need to modify the results in the imageview onclick event listener((i.e.,)removeitem.addEventListener("click",function(e){)

If any one knows give me a idea to implementation

EDIT:

From Tony commment.,

It's working well in the IOS.But it's not work with the android implementation.

alert(item.children[1].getText());

Here am getting the alert with correct updated value.But it's not showing in the label. Can you give me a idea to resolve this problem in android.

1
Please check this question : stackoverflow.com/questions/29554108/… You will get idea how to add event to each fieldVRK

1 Answers

1
votes

you can add the click event on the table view and then access the child of table view row in this way

    s_f_table.addEventListener("click", function(e) {
    var l = e.row.children[0];

    if (l.getColor() == '#919191') {
        l.applyProperties({
            backgroundColor : o_color,
            color : "white"
        });
    } else {
        l.applyProperties({
            backgroundColor : "white",
            color : "#919191"
        });
    }

});

here in my code the first child in table view row is a label so you can access the children of you table view row if you know the order of them

another way if you want to add the event on the image view you can add some thing like this

anImageView.addEventListener("click",function(e){
    var item=e.source;
    item.getParent();//so now you are in the table row 
    // so you can access any child and modify it    
     });

EDIT: as your last update you are now on the parent view parent[object TiUIView] which have two child the second one of them is the label which you want to modify it so now after you got the view you can call view.children[1].settText("any thing you want")