Titanium Community Questions & Answer Archive

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

Trying to refresh dynamic table view with complex table rows

Hello all,
I'm having a hard time grasping setData and how to use it. I can't figure out how to re-query and reload my dynamic list. The code below is how I initially load a list of projects. I've tried moving this into a function and calling it when a 'refresh' button is clicked but no luck. How do re-query and then refresh? Thx!

var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
  var data = JSON.parse(this.responseText);
  var dl = (data.length-1);
  for(i=0; i<=dl;i++)
  {
    p = addRow(data,i); // returns the **arr array 
  }
  var projTable = Titanium.UI.createTableView({ data: **arr, blah, blah });
  proj.add(projTable);
};
xhr.open('GET','http://foo.com/statuscheck/getProjectData.php');
xhr.send();

// refresh button
var b = Titanium.UI.createButton({title:'Refresh List'});
proj.rightNavButton = b;
rightnav = true;

b.addEventListener('click', function(e)
{
  projTable.setData(data);
});
— asked May 7th 2010 by Kelly Redd
  • iphone
  • setdata
0 Comments

7 Answers

  • um believe it or not there is actually a table_view_refresh.js in kitchen sink. how i've never noticed this file i'll never know. but anyway, after getting the reusable function rockin i just needed to create the tableview and add the tableview to the window outside the function.

    bam. success. thanks tamas and markus.

    — answered May 8th 2010 by Kelly Redd
    permalink
    0 Comments
  • is that data

    projTable.setData(data);
    

    declared somewhere else?, because is the data inside the xhr.open function, is not globally accessible.

    — answered May 7th 2010 by Dan Tamas
    permalink
    0 Comments
  • Hey Tamas, no data is not declared somewhere else. That seems to be my problem. I don't know how to make it accessible…data is populated by the xhr call response so it seems like I should be making another xhr call again at some point to get updated content…which is why I tried putting that into a function to make it reusable.

    Maybe setData is not the right solution here?

    — answered May 7th 2010 by Kelly Redd
    permalink
    0 Comments
  • there's an rss-example in KitchenSink with a reload-button. maybe you can use this as a good example how reloading works (even data is loaded from a rss-feed).
    if you want to move your requests into a function maybe this link (http://bradvernon.com/tag/titanium-mobile/) would be helpful.

    — answered May 7th 2010 by Markus Gerlach
    permalink
    0 Comments
  • Thank Markus, exactly what i needed…filled in the gaps of my previous attempt at making a reusable function.

    And not to have every one do my homework ;) but now that i've my code reusable its grabbing the new content and appending it to the bottom of the existing list…not exactly refreshing or reloading…just adding to.

    Any ideas?

    — answered May 7th 2010 by Kelly Redd
    permalink
    0 Comments
  • anyway to mark my own answer as the best answer??

    — answered May 8th 2010 by Kelly Redd
    permalink
    0 Comments
  • also, a simple way to refresh an array is to simply do this: if your array is called data, just do data = []; and that empties the array.. then refill with new data and you are good.

    — answered May 8th 2010 by Clint Tredway
    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.