Titanium Community Questions & Answer Archive

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

Save reordered tableview data in db?

Hi there,

I have a tableview which the user can reorder the items and that's great. However, when the user closes the app, the reordering is lost. Btw, the data is stored in a sqlite database.

Does anyone have an example of how to update the database with the re-ordered rows?

Hugely grateful for help on this,

cheers,
Chris.

— asked April 13th 2010 by Chris Reed
  • iphone
  • order
  • sort
  • tableview
1 Comment
  • will you please help me how to reorder the table view rows in titanium studio, for android app?

    — commented January 17th 2014 by VENU LEKKALA

6 Answers

  • Accepted Answer

    The documentation is wrong regarding the TableView object(s), so I had to guess at this and figure it out, but this will work (assuming your table has a single section):

    var tableSection = tableview.data[0];
    Titanium.API.info(tableSection);
    Titanium.API.info("tableview.data[0].rowCount=" + tableview.data[0].rowCount);
    for (var j=0; j < tableSection.rowCount; j++)
    {
        var row = tableSection.rows[j];
        Titanium.API.info(row);
    }
    

    That gets you at the rows in the table at least.

    — answered April 13th 2010 by Mark Burggraf
    permalink
    2 Comments
    • I am able to get the number of rows this way. How do I access the row index and any possible data stored in the row?

      — commented October 1st 2011 by Leonardo Amigoni
    • The index would just be the value of j. As for data stored in the row, see how he's declaring the row variable there inside of the for loop? In there you just do row.whatever to access any of that row's properties.

      — commented October 8th 2011 by Chris Hoopes
  • This is very tricky, and it depends on how you've structured the data in your table. (There's an entire book on the subject of storing hierarchical data in SQL called "SQL for Smarties by Joe Celko" that's very highly recommended.)

    Anyhow, you'll want to trap the "move" event of your TableView, figure out some sort of identifier for your dragged row and the target, then use those pieces of data to update your table.

    — answered April 13th 2010 by Mark Burggraf
    permalink
    0 Comments
  • Thanks Mark,

    Since I have a button to start/end the move mode, I was going to wait until the user had finished all their re-jigging and take the reordered data at the end. In that case I'd have just have the rows in their post-move order. If they shutdown 'mid-move' I think that's lose-able.

    I thought the best way would be to drop the table and reload by looping through the rows in the new sequence (on-screen) and insert into the db that way. Thankfully there's only one table involved.

    But maybe that won't work if I can't step through the rows in the order they appear on the display(?).

    cheers,
    Chris.

    — answered April 13th 2010 by Chris Reed
    permalink
    0 Comments
  • You can probably loop through the re-ordered rows as you've suggested. Give it a try and just spit out the new values with Ti.API.info().

    — answered April 13th 2010 by Mark Burggraf
    permalink
    0 Comments
  • Thanks Mark,
    Where I hit a blocker is picking-up each row object from the data array and then getting the data from that row to populate the database with. I just can't fathom the right syntax.

    (I'd usually been working from a 'rowclick' where it feeds me the specific eventObject to work from).

    cheers,
    Chris.

    — answered April 13th 2010 by Chris Reed
    permalink
    0 Comments
  • Hey, you're a Hero there Mark.

    That looks like it'll do the business.

    Many thanks,
    Chris.

    — answered April 13th 2010 by Chris Reed
    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.