addEventListener when using createTableViewRow in createTableView?

You must Login before you can answer or comment on any questions.

Hello,

I have created a table view and added rows using an http call. I am retrieving just 2 rows currently and this works fine. The next thing I want to happen is when a click is done on either row a new window opens with the case name passed through. I have managed to create a addEventListener to do this but I am doing something wrong as I can't get the correct details to be passed in the new window. No matter which row I click the last rows case name always gets passed.

Below is my code - can anyone tell me what I am doing wrong please?

function AppWindow(title) {
    var Main = Ti.UI.createWindow({
        title:title,
        backgroundColor:'white'
    });
 
    //Closed Case Button
    var ccButton = Titanium.UI.createButtonBar({
        labels:['Closed'],
        backgroundColor:'#336699'
    });
    Main.setLeftNavButton(ccButton);
 
    //New Case Button
    var ncButton = Titanium.UI.createButtonBar({
        labels:['New Case'],
        backgroundColor:'#336699'
    });
    Main.setRightNavButton(ncButton);
 
    ncButton.addEventListener('click', function() {
    //containingTab attribute must be set by parent tab group on
        //the window for this work
        Main.containingTab.open(Ti.UI.createWindow({
            title: L('newCaseWindow'),
            backgroundColor: 'white'
        }));
    });
 
    var uid = Ti.App.Properties.getString('uid');
    var url = 'http://localhost/api/case_query.php';
    var table = Ti.UI.createTableView();
    var tableData = [];
    var json, cases, cases1, i, row, nameLabel, numberLabel;
 
    var xhr = Ti.Network.createHTTPClient({
            onload: function() {
        // Ti.API.debug(this.responseText);
 
        json = JSON.parse(this.responseText);
        for (i = 0; i < json.cases.length; i++) {
        cases1 = json.cases[i];
        row = Ti.UI.createTableViewRow({
        height:'60dp'
        });
        nameLabel = Ti.UI.createLabel({
        text:cases1.name,
        font:{
        fontSize:'20dp',
        fontWeight:'bold'
        },
        height:'auto',
        left:'10dp',
        top:'5dp',
        color:'#000',
        touchEnabled:true
        });
        numberLabel = Ti.UI.createLabel({
        text:cases1.number + ' - ' + cases1.status,
        font:{
        fontSize:'16dp'
        },
        height:'auto',
        left:'10dp',
        bottom:'5dp',
        color:'#000',
        touchEnabled:true
        });
 
        row.add(nameLabel);
        row.add(numberLabel);
        tableData.push(row);
                }
 
        table.setData(tableData);
            },
            onerror: function(e) {
        Ti.API.debug("STATUS: " + this.status);
        Ti.API.debug("TEXT: " + this.responseText);
        Ti.API.debug("ERROR: " + e.error);
        alert('There was an error retrieving the remote data. Try again.');
            },
            timeout:5000
    });
 
            //Open case on click
            table.addEventListener('click', function()
            {
            var casename = cases1.name
 
            Main.containingTab.open(Ti.UI.createWindow({
                title:('Case Details'),
                url:('ui/case_detail.js'),
                backgroundColor: 'white',
                casename:casename
            }));
            });
 
 
    //Refresh the view on tab click
    Main.addEventListener('focus', function() 
    {   tableData = []; //clear old data
        xhr.open("POST", url);
        var params = {  
          uid:uid,
        };  
        xhr.send(params); 
    });
 
    var msg = Titanium.UI.createLabel({  
    //text:"You have successfully logged in. Upon logging in we sent back your email address and your name. You can pass all kinds of data simply by creating objects on your window.\n\nYour email is:\n" + casename + "\n\nyour name is:\n" + casenumber,  
    top:10,  
    left:10,  
    width:300,  
    height:'auto'  
});  
 
 
    Main.add(table)
//  Main.add(msg);  
    return Main;
};
 
module.exports = AppWindow;

1 Answer

Accepted Answer

table.addEventListener('click', function()
Instead:
table.addEventListener('click', function(e)
You will find that e is an object that contains a rack of properties, one of them being e.row

— answered 10 months ago by Stephen Feather
answer permalink
7 Comments
  • e.row contains information about the particular row that was clicked.

    — commented 10 months ago by Stephen Feather

  • I added the 'e' to the function. Where do I need to add the e.row? I tried adding e.row but got undefined errors so I must not have been doing them right.

    //Open case on click
                table.addEventListener('click', function(e)
                {
                var casename = cases1.name
     
                Main.containingTab.open(Ti.UI.createWindow({
                    title:('Case Details'),
                    url:('ui/case_detail.js'),
                    backgroundColor: 'white',
                    casename:casename
                }));
                });

    — commented 10 months ago by Mobile Engineer

  • When creating your row: row.casename = cases1.name

    Then in your click function you should be able to access it by e.row.casename

    — commented 10 months ago by Stephen Feather

  • Show 4 more comments

Your Answer

Think you can help? Login to answer this question!