Titanium Community Questions & Answer Archive

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

how to pass parameters from tableview to another view?

Newb question…
I am trying to create a custom phone number picker.
I am able to pull the FULLNAMES out of the contactlist, and createTableView.
I am also able to pull the individual numbers out of the Phone array.

I am looking at the example: contacts_db.js, but I don't want to display the address. I have some other buttons and stuff I would want to add.

IDEALLY, I want to create a new .js file that will display the phone numbers with buttons.

MY QUESTION is…
how do I pass the ROWNUMBER of the TableView on click to another .js file as a parameter?
If I get the row number, than I have the contact number, and I can render the view as I want. Besides, I want to use this view on another page.

I was also thinking of setting the number in the click event:
Titanium.App.Properties.setInt('contactnumber',i); // use this to build view of contacts
can I get a hint please..

— asked September 30th 2010 by vincent youmans
  • tableview
0 Comments

5 Answers

  • This doesn't seem to work for me… How can I access the "moo" object in my second view which is generated (in the example) via "bar.js"?

    — answered November 11th 2010 by Czar
    permalink
    1 Comment
    • Although I am no expert at scope, I believe there is an issue with where you chose to place the import of the second file and that sets the scope for the contents of the imported file.

      Try this code snippet:

      app.js

      // Import the files
      Ti.App.import('win1.js');
      Ti.App.import('win2.js');
      
      // Create tabgroup and tabs
      var tabGroup = Titanium.UI.createTabGroup({});
      var tab1 = Titanium.UI.createTab({
          title: win1.title,
          window: win1
      });
      var tab2 = Titanium.UI.createTab({
          title: win2.title,
          window: win2
      });
      tabGroup.addTab(tab1);
      tabGroup.addTab(tab2);
      tabGroup.open({});
      

      win1.js

      var win 1 = Titanium.UI.createWindow({
          title: 'Window 1'
      });
      win1.foo = 'bar';
      

      win2.js

      var win 1 = Titanium.UI.createWindow({
          title: 'Window 2'
      });
      alert('Global variable win1.foo is ' + win1.foo);
      

      — commented November 11th 2010 by Adam Renklint
  • the easiest way, and i think the best architecture is to fire events passing the information you want and then listening for these events in the other js files.

    so in you table view click event

    Ti.App.fireEvent.fireEvent('user_selected', {
       // you can also just pass the whole object here...
        row_id : 10;
    });
    

    now anywhere in you application, listen for this event and you will get the data

    someDetailedView.addEventListener('user_selected', function(data) {
        Ti.API.info("user_selected " + data. row_id);
        // get the data from the data store
    });
    
    — answered November 11th 2010 by Aaron Saunders
    permalink
    0 Comments
  • Hey, if i understand you correctly then the answer lies in the API documentation at the bottom of the UI.Window page - Passing Data.

    Basically since data in one script cant access data or functions from another (because they run in seperate contexts) what you need to do is pass your variable/method to the script you want.

    Here's an example:

    var foo = "test"
    
    var win = Titanium.UI.createWindow({
        url:'bar.js'
    });
    
    win.moo = foo;
    

    Notice the last line - this is basically saying "add a variable 'moo' to the 'win' object and let it be the value of a variable 'foo' in the current script"

    Within your bar.js script you would now be able to make use of the variable 'moo' and it will have the value that 'test' had in the previous window.

    — answered September 30th 2010 by Alex Borisov
    permalink
    0 Comments
  • is that…
    win.moo = test;

    or

    win.moo = foo;

    that may have been a typo?

    but thanks for the explanation.

    — answered September 30th 2010 by vincent youmans
    permalink
    1 Comment
    • Yup it was a typo :) Thanks for spotting that - it's fixed now :P

      — commented October 1st 2010 by Alex Borisov
  • My code..

    customers.js :

        //create tableView event listener
                    tableView.addEventListener('click', function(e)
        {
    
    
        //set properties for passed info
        var newWin = Titanium.UI.createWindow({
            title:e.row.name,
            url:'customerDetail.js',
            passedName:e.row.name,
            passedID:e.row.id
                });
    
        newWin.open({animated:true, modal:true});
    });
    

    customerDetail.js:

    var custName = win.passedName;
    

    then custName will hold the value of whichever row was clicked in customers.js

    — answered November 11th 2010 by Ben Boblis
    permalink
    0 Comments
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.