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 = ["REF","CAT","TASK","Priority","Logged On","Resolved On","Resolved By","Major Incident"]
var arrayTe = [], arrayTe2 = []

function OnStart() {
    app.EnableBackKey(false) // save on exit
	lay = app.CreateLayout("Linear","Left")
    butAdd = app.AddButton(lay,"Add")
    butAdd.SetOnTouch(Add)
	app.AddLayout( lay )

    dlgAdd = app.CreateDialog("Add","Old")
    layAdd = app.CreateLayout("Linear")
    dlgAdd.AddLayout(layAdd)
    fields.forEach(function (name,index,array) {
        horiz = app.AddLayout(layAdd,"Linear","Horizontal")
        txt = app.AddText(horiz,name,0.1,-1,"Right")
        te  = app.AddTextEdit(horiz,"",0.1,-1,"singleline")
        arrayTe.push(te)
    }) // forEach
    butSave = app.AddButton(layAdd,"Save")
    butSave.SetOnTouch(Save)

    dlgShow = app.CreateDialog("Detail","Old")
    layShow = app.CreateLayout("Linear")
    dlgShow.AddLayout(layShow)
    fields.forEach(function (name,index,array) {
        horiz = app.AddLayout(layShow,"Linear","Horizontal")
        txt = app.AddText(horiz,name,0.1,-1,"Right")
        te  = app.AddTextEdit(horiz,"",0.1,-1,"singleline")
        arrayTe2.push(te)
    }) // forEach
    butUpdate = app.AddButton(layShow,"Update")
    butUpdate.SetOnTouch(Update)
    butDelete = app.AddButton(layShow,"Delete")
    butDelete.SetOnTouch(Delete)
    
//    app.ClearData() // for testing
    tasks = app.LoadText("tasks","")
    lstTasks = app.AddList(lay,tasks,1,-1,"html")
    lstTasks.SetOnTouch(Show)
} // fn
function Add () {
    arrayTe.forEach(function (name,index,array) {
        name.SetText("")
    }) // forEach
    dlgAdd.Show()
} // fn
function Save () {
    body = ""
    arrayTe.forEach(function (name,index,array) {
        if (index == 0) {
            title = fields[index] + " - " + name.GetText()
        } else {
            if (body != "") body+="<br>"
            body+= fields[index] + " - " + name.GetText()
        } // if
    }) // foreach
    lstTasks.AddItem(title,body)
    dlgAdd.Dismiss()
} // fn
function Show(title,body,icon,index) {
    globalindex = index
    str = title.replace(fields[0]+" - ","")
    arrayTe2[0].SetText(str)
    temp = body.split("<br>")
    temp.forEach(function (name,index,array) {
        nm = name.replace(fields[index+1]+" - ","")
        arrayTe2[index+1].SetText(nm)
    }) // forEach
    dlgShow.Show()   
} // fn
function Update () {
    body = ""
    arrayTe2.forEach(function (name,index,array) {
        if (index == 0) {
            title = fields[index] + " - " + name.GetText()
        } else {
            if (body != "") body+="<br>"
            body+= fields[index] + " - " + name.GetText()
        } // if
    }) // forEach
    lstTasks.SetItemByIndex(globalindex,title,body)
    dlgShow.Dismiss()
} // fn
function Delete () {
    lstTasks.RemoveItemByIndex(globalindex)
    dlgShow.Dismiss()
} // fn
function OnBack() {
    tasks = ""
    for (let i=0; i<lstTasks.GetLength() ; i++) {
        if (tasks != "") tasks+=","
        tbi = lstTasks.GetItemByIndex(i)
        tasks+= tbi.title + ":" + tbi.body + ":null"
    } // for
    app.SaveText("tasks",tasks)
    app.Exit()
} // fn