This shows you the differences between two versions of the page.
— |
sample_code:list_of_entries [2022/07/14 16:04] (current) al4he6 created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====List of entries==== | ||
+ | This code allows the user to display a list of entries.\\ | ||
+ | The user can add entries to the list,by touching an entry they can update or delete it.\\ | ||
+ | The list is saved when the user touches Android Back (bottom left).\\ | ||
+ | The list of fields can be easily customized.\\ | ||
+ | Should be easy to format (colour, size), and add validation.\\ | ||
+ | < | ||
+ | const fields = [" | ||
+ | var arrayTe = [], arrayTe2 = [] | ||
+ | function OnStart() { | ||
+ | app.EnableBackKey(false) // save on exit | ||
+ | lay = app.CreateLayout(" | ||
+ | butAdd = app.AddButton(lay," | ||
+ | butAdd.SetOnTouch(Add) | ||
+ | app.AddLayout( lay ) | ||
+ | |||
+ | dlgAdd = app.CreateDialog(" | ||
+ | layAdd = app.CreateLayout(" | ||
+ | dlgAdd.AddLayout(layAdd) | ||
+ | fields.forEach(function (name, | ||
+ | horiz = app.AddLayout(layAdd," | ||
+ | txt = app.AddText(horiz, | ||
+ | te = app.AddTextEdit(horiz,"", | ||
+ | arrayTe.push(te) | ||
+ | }) // forEach | ||
+ | butSave = app.AddButton(layAdd," | ||
+ | butSave.SetOnTouch(Save) | ||
+ | |||
+ | dlgShow = app.CreateDialog(" | ||
+ | layShow = app.CreateLayout(" | ||
+ | dlgShow.AddLayout(layShow) | ||
+ | fields.forEach(function (name, | ||
+ | horiz = app.AddLayout(layShow," | ||
+ | txt = app.AddText(horiz, | ||
+ | te = app.AddTextEdit(horiz,"", | ||
+ | arrayTe2.push(te) | ||
+ | }) // forEach | ||
+ | butUpdate = app.AddButton(layShow," | ||
+ | butUpdate.SetOnTouch(Update) | ||
+ | butDelete = app.AddButton(layShow," | ||
+ | butDelete.SetOnTouch(Delete) | ||
+ | | ||
+ | // app.ClearData() // for testing | ||
+ | tasks = app.LoadText(" | ||
+ | lstTasks = app.AddList(lay, | ||
+ | lstTasks.SetOnTouch(Show) | ||
+ | } // fn | ||
+ | function Add () { | ||
+ | arrayTe.forEach(function (name, | ||
+ | name.SetText("" | ||
+ | }) // forEach | ||
+ | dlgAdd.Show() | ||
+ | } // fn | ||
+ | function Save () { | ||
+ | body = "" | ||
+ | arrayTe.forEach(function (name, | ||
+ | if (index == 0) { | ||
+ | title = fields[index] + " - " + name.GetText() | ||
+ | } else { | ||
+ | if (body != "" | ||
+ | body+= fields[index] + " - " + name.GetText() | ||
+ | } // if | ||
+ | }) // foreach | ||
+ | lstTasks.AddItem(title, | ||
+ | dlgAdd.Dismiss() | ||
+ | } // fn | ||
+ | function Show(title, | ||
+ | globalindex = index | ||
+ | str = title.replace(fields[0]+" | ||
+ | arrayTe2[0].SetText(str) | ||
+ | temp = body.split("< | ||
+ | temp.forEach(function (name, | ||
+ | nm = name.replace(fields[index+1]+" | ||
+ | arrayTe2[index+1].SetText(nm) | ||
+ | }) // forEach | ||
+ | dlgShow.Show() | ||
+ | } // fn | ||
+ | function Update () { | ||
+ | body = "" | ||
+ | arrayTe2.forEach(function (name, | ||
+ | if (index == 0) { | ||
+ | title = fields[index] + " - " + name.GetText() | ||
+ | } else { | ||
+ | if (body != "" | ||
+ | body+= fields[index] + " - " + name.GetText() | ||
+ | } // if | ||
+ | }) // forEach | ||
+ | lstTasks.SetItemByIndex(globalindex, | ||
+ | dlgShow.Dismiss() | ||
+ | } // fn | ||
+ | function Delete () { | ||
+ | lstTasks.RemoveItemByIndex(globalindex) | ||
+ | dlgShow.Dismiss() | ||
+ | } // fn | ||
+ | function OnBack() { | ||
+ | tasks = "" | ||
+ | for (let i=0; i< | ||
+ | if (tasks != "" | ||
+ | tbi = lstTasks.GetItemByIndex(i) | ||
+ | tasks+= tbi.title + ":" | ||
+ | } // for | ||
+ | app.SaveText(" | ||
+ | app.Exit() | ||
+ | } // fn | ||
+ | |||
+ | </ |