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.
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.
-
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.
-
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. -
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().
-
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. -
Hey, you're a Hero there Mark.
That looks like it'll do the business.
Many thanks,
Chris.