Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

event Listener in tableView

Hi guys.
I have a few lines in a tableView that a want with a eventlistener (with a message).. Theres is alredy an eventListener added.. but how do i link it with each "title" ?? Heres the Code:

if (Ti.Platform.name == 'android') 
{
    Titanium.UI.currentWindow.backgroundColor = '#4e5c4d';
}
else
{
    Titanium.UI.currentWindow.backgroundColor = '#aebcad';
}

// create table view data object

var data = [
    {title:'Turtyp', header:'Viktigt att tänka på'},
    {title:'Manuell lossning'},
    {title:'Pant returer'},
    {title:'Kund signatur'},


    ];


if (Titanium.Platform.name == 'iPhone OS')


// create table view
var tableViewOptions = {
        data:data,
        style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
        backgroundColor:'transparent',
        rowBackgroundColor:'white'
    };


var tableview = Titanium.UI.createTableView(tableViewOptions);

// create table view event listener
tableview.addEventListener('click', function(e)

{
    if (e.rowData.test)
    {
        var win = Titanium.UI.createWindow({
            url:e.rowData.test,
            title:e.rowData.title
        });
        Titanium.UI.currentTab.open(win,{animated:true});
    }
});


// add table view to the window
Titanium.UI.currentWindow.add(tableview);
— asked August 11th 2011 by Nico Kjeldsen
  • eventlistener
  • tableview
0 Comments

4 Answers

  • evt.index is the row number that was clicked.<br>
    evt.section is the TableViewSection that contains the row that was clicked.<br>
    evt.row is the TableViewRow that was clicked.<br>
    evt.rowData is the row properties "object" for the row that was clicked.<br>
    <br>
    Your code looks fine, except that you are referencing e.rowData.test and I don't see that any of your rows have a "test" property. What is it that isn't working?

    — answered August 11th 2011 by Shawn Lipscomb
    permalink
    0 Comments
  • Like Shawn said, you don't have e.rowData.test in your table data, so that if condition never passes. I think that the Kitchen Sink examples may not be clear for you here. To get it to work, try adding the window urls to the table date like this:

    var data = [
        {title:'Turtyp', header:'Viktigt att tänka på', test:'turtyp.js'},
        {title:'Manuell lossning', test:'manuell.js'},
        {title:'Pant returer', test:'pant.js'},
        {title:'Kund signatur', test:'pant.js'},
    
        ];
    

    Of course you need those files to exist and contain your window data. For more info on tableview rows, check out the TableViewRow object and consider using it as you build your app.

    — answered August 11th 2011 by Adam Paxton
    permalink
    0 Comments
  • Ah… about the "test" . the code is copied from another of my windows.. i Dont want it to link typ another .js file.. i just want to add an Alert-message to the Title-text ('turtyp', 'manuell lossning' etc). Are you guys following or am i explaining it like an idiot :P PS. What is the 'rowData.test'? I have it in my other codes (from kitchensink)..

    — answered August 11th 2011 by Nico Kjeldsen
    permalink
    0 Comments
  • At the top of your code, add var window = Ti.UI.currentWindow;

    Then, at the bottom, change Titanium.UI.currentWindow.add(tableview); to window.add(tableview);.

    Last, make your tableview eventlistener look like this:

    tableview.addEventListener('click', function(e)
    {
            window.title = e.rowData.title;
    });
    

    Also, when working in the Q&A, please comment on answers using the 'add comment' link at the bottom of the answer, and only use the 'Your Answer' box when adding an answer to the question. It helps keep the q&a readable for future visitors to the question.

    — answered August 11th 2011 by Adam Paxton
    permalink
    8 Comments
    • Thanks man :) Will do!
      But where do i put the comments that will pop up with the eventlistener ?
      Something like:
      title1 = bla bla ?
      title2 = bla again ?

      — commented August 11th 2011 by Nico Kjeldsen
    • Your code indicated it would be coming from the table data, so it would be put in the data variable. In my example, the window title is being changed to match the 'title' that is in the selected tablerow view. So, when Turtyp is clicked in the tableview, then the window title will be changed to say 'Turtyp'.

      — commented August 11th 2011 by Adam Paxton
    • ahhh i see now :) Thats nice to.. but i meant an Alert-box-message :):):)

      — commented August 11th 2011 by Nico Kjeldsen
    • alert(&#39;This is an alert message. You clicked on row title: &#39;+e.rowData.title);

      — commented August 11th 2011 by Adam Paxton
    • Actually i meant to put different text on each column .. Are u following me? Something like:

      var data =[];
      data [0] = The message
      data [1] = Also a message but something else..
      

      — commented August 11th 2011 by Nico Kjeldsen
    • Not really following you. Maybe this:

      var tabledata = [{title:&#39;Turtyp&#39;, alertmessage: &#39;This is an alert&#39;}];

      then the alert message in the eventListener:

      alert(e.rowData.alertmessage);

      — commented August 11th 2011 by Adam Paxton
    • that seems pretty correct… but i cant get to work now. Im all tangled up in "my" own code now.. :/ BS if you're (myself) not so good at javascript.. :/

      — commented August 12th 2011 by Nico Kjeldsen
    • Post your code so people can help you.

      — commented August 12th 2011 by Adam Paxton
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.