Save my modified/deleted table view

You must Login before you can answer or comment on any questions.

I'm totally new and I really need some help. I've googled and searched the Q&A here and I havent found anything that I can apply to my problem.

When i press the edit button and move around my rows I would like to save what I moved or deleted if I terminate the app. I was thinking of giving my rows a value from the loop and then try to save those values in the edited order within the done button. If that is possible? A hint would be fantastic. :)

Titanium.UI.setBackgroundColor('#000');
 
var tabGroup = Titanium.UI.createTabGroup();
 
var win = Titanium.UI.createWindow({
    title:"Main",
    backgroundColor:"#fff",
    tabBarHidden:true
});
 
var button = Ti.UI.createButton({
    title: "edit",
});
 
win.setRightNavButton(button);
    var tab = Titanium.UI.createTab({
        title:"Tab1",
        window:win
});
 
var data = [
    {title:"Rad1", className:"tableRow"},
    {title:"Rad2", className:"tableRow"},
    {title:"Rad3", className:"tableRow"},
    {title:"Rad4", className:"tableRow"},
    {title:"Rad5", className:"tableRow"},
    {title:"Rad6", className:"tableRow"}
]
 
var rowData = [];
for(var i = 0; i < data.length; i++){
    var row = Titanium.UI.createTableViewRow({
        title:data[i].title,
        className:data[i].className,
        hasChild:true
    });
    rowData.push(row);
 
}
 
var tableView = Titanium.UI.createTableView({
    data:rowData,
    editable:true,
    allowsSelectionDuringEditing:true,
    moving:false
});
 
tableView.addEventListener("click", function(e){
 
    var w = Titanium.UI.createWindow({
        title:e.rowData.title,
        backgroundColor:"#fff"
    });
 
    var label = Titanium.UI.createLabel({
        text:"this is a new window",
        height:"auto",
        width:"auto"
    })
 
    w.add(label);
 
    tab.open(w,{animated:true});
 
});
 
button.addEventListener('click', function(e) {
    if (!tableView.moving){
        tableView.moving = true;
        e.source.title = "done";
 
    } else { 
        tableView.moving = false;
        e.source.title = "edit";
    }
});
 
 
 
win.add(tableView);
 
tabGroup.addTab(tab);  
 
tabGroup.open();

— asked 9 months ago by Martin Andersson
2 Comments
  • Can you explain a more what you are trying to do. what do you mean by "I terminate the app"

    — commented 9 months ago by Arian Caraballo

  • Saving my last modifications. Terminate I mean when I quit the app. Not only home button quit, but a real quit.

    — commented 9 months ago by Martin Andersson

1 Answer

Accepted Answer

A simple solution to achieve that is storing your row data of your Tableview ("tableView.data[0].rows") permanently with Ti.App.Properties...

Here's the code:

Titanium.UI.setBackgroundColor('#000');
 
var tabGroup = Titanium.UI.createTabGroup();
 
var win = Titanium.UI.createWindow({
    title:"Main",
    backgroundColor:"#fff",
    tabBarHidden:true
});
 
var button = Ti.UI.createButton({
    title: "edit",
});
 
win.setRightNavButton(button);
    var tab = Titanium.UI.createTab({
        title:"Tab1",
        window:win
});
 
var data = [
    {title:"Rad1", className:"tableRow"},
    {title:"Rad2", className:"tableRow"},
    {title:"Rad3", className:"tableRow"},
    {title:"Rad4", className:"tableRow"},
    {title:"Rad5", className:"tableRow"},
    {title:"Rad6", className:"tableRow"}
];  
 
var rowData  = JSON.parse(Ti.App.Properties.getString("rowData"));
Ti.API.info("rowData: "+rowData);
if (!rowData) {
    rowData = [];
    for(var i = 0; i < data.length; i++){
        var row = Titanium.UI.createTableViewRow({
            title:data[i].title,
            className:data[i].className,
            hasChild:true
        });
        rowData.push(row);
    }
}
 
var tableView = Titanium.UI.createTableView({
    data:rowData,
    editable:true,
    allowsSelectionDuringEditing:true,
    moving:false
});
 
tableView.addEventListener("click", function(e){
 
    var w = Titanium.UI.createWindow({
        title:e.rowData.title,
        backgroundColor:"#fff"
    });
 
    var label = Titanium.UI.createLabel({
        text:"this is a new window",
        height:"auto",
        width:"auto"
    })
 
    w.add(label);
 
    tab.open(w,{animated:true});
 
});
 
button.addEventListener('click', function(e) {
    if (!tableView.moving){
        tableView.moving = true;
        e.source.title = "done";
 
    } else { 
        tableView.moving = false;
        Ti.App.Properties.setString("rowData", JSON.stringify(tableView.data[0].rows));
        e.source.title = "edit";
    }
});
 
 
 
win.add(tableView);
 
tabGroup.addTab(tab);  
 
tabGroup.open();

— answered 9 months ago by Moritz Knecht
answer permalink
3 Comments
  • Works perfect thank you, but is it a bad solution?

    — commented 9 months ago by Martin Andersson

  • For a small amount of data it is quite good. But if you're dealing with a much larger data set, i would prefer using a sqlite database and add an additional property to each entry with the position in the tableview.

    — commented 9 months ago by Moritz Knecht

  • I guess I need to create an addEventListener to the swipe/delete button as well and include the line: Ti.App.Properties.setString("rowData", JSON.stringify(tableView.data[0].rows)); ?

    — commented 9 months ago by Martin Andersson

Your Answer

Think you can help? Login to answer this question!