I want to have next and previous buttons in my workshop so that I can let the user page through a list of objects. I tried to write a function to do this, but i don’t think it’s a good implementation. Also, it has to be implemented specificially for each object, i would really love to have it be a generic function.
Currently I have the function below, and i have a dropdown menu with a list of objects. I pass the function the current object and the list of objects, and for a positive or negative increment, it will return the new object. I then have a button, that when its clicked takes the output the function and overwrites the current selction of the dropdown with the function results. The problem is, that sometimes the result is undefined, and then it goes into a bad state. It’s also slow, and not very generic. Any Typescript wizards care to enlighten me?
// Provide next and previous buttons
@Function()
public listArrowAction(objects: ObjectSet<myObject>, item: myObject, n: Integer = 1): myObject{
let keys: string[] = []
var pk = item.primaryKey.string
let item_list = objects.all()
if (item===undefined){
return item_list[0]
}
item_list.forEach(x => {
pk = x.primaryKey_
keys.push(pk)
})
const index = keys.indexOf(item.primaryKey_)
n = n % keys.length
let new_index = index + n
new_index = new_index % keys.length
if (new_index < 0) {
new_index = keys.length + new_index
}
return item_list[new_index]
}