I have a view with a table pulling data from a database.
keyID and projDescription are the columns returned from the database. When I load a view I want to automatically select a given row in the table based off of a session variable called keyID. What is the most logical way to achieve this?
This might help get you going. (I'm no Python expert!)

Here's the button code. (Note the text field and button are inside a flex row container.)
def runAction(self, event):
c = 0
self.parent.parent.getChild("Table").props.selection.selectedRow = None
for row in self.parent.parent.getChild("Table").props.data:
if row['city'] == self.getSibling("TextField").props.text:
self.parent.parent.getChild("Table").props.selection.selectedRow = c
return
c += 1
You'll need to put the code inside some event on your view instead of inside the button.
You can use enumerate() for stuff like this:
for i, row in enumerate(self.parent.parent.getChild("Table").props.data):
if row['city'] == self.getSibling("TextField").props.text:
self.parent.parent.getChild("Table").props.selection.selectedRow = i
return
Or with a python one-liner if you really want:
data = self.parent.parent.getChild("Table").props.data
city = self.getSibling("TextField").props.text
i = next(i for i, row in enumerate(data) if row['city'] == city)
self.parent.parent.getChild("Table").props.selection.selectedRow = i
Though that one liner will throw a StopIteration exception if it can't find it in the table...
2 Likes
There seems to be some bug? I have the selectedRow set to a valid entry but the table isn't actually highlighted. When I manually select a new row I can see the selectedRow correctly changing.
It seems to be related to the Transform script I have on the dataset that highlights all the rows with alternating shades of green for easier visibility.
I removed the Transform script and was able to get it to work correctly.
new_list = []
lightPattern = 0
# Iterate over each dictionary in the original list
for item in value:
# Create a new dictionary for the converted JSON
if lightPattern:
classPath = "tables/rows/green/light"
else:
classPath = "tables/rows/green/dark"
new_dict = {
"ID": {
"value": item["WorkstationTypeID"],
"editable": True,
"style": {
"classes": classPath
},
"align": "center",
"justify": "center"
},
"Type": {
"value": item["WorkstationTypeName"],
"editable": True,
"style": {
"classes": classPath
},
"align": "center",
"justify": "center"
}
}
# Append the converted dictionary to the new list
new_list.append(new_dict)
#Change every other row of color.
if lightPattern == 1:
lightPattern = 0
else:
lightPattern = 1
return new_list
Ok... I kept messing around and it looks like in my example row['city'] has some style properties including a value so I have to do the following:
for i, row in enumerate(self.parent.parent.getChild("Table").props.data):
if row['city'].value == self.getSibling("TextField").props.text:
self.parent.parent.getChild("Table").props.selection.selectedRow = i
return