# In a perspective table how would I select a row with a particular value in it upon loading the view?

**URL:** <https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435>\
**Category:** Ignition\
**Tags:** perspective, ignition81\
**Created:** [May 8, 2023, 7:00pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435 "2023-05-08T19:00:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 8, 2023, 7:00pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/1 "2023-05-08T19:00:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 8, 2023, 7:52pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/2 "2023-05-08T19:52:47Z")

</div>

This might help get you going. (I'm no Python expert!)

![Table highlight](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/0/e08916a0d60a5c7d7afe1aa72574813c0ab3ebdc.gif)

Here's the button code. (Note the text field and button are inside a flex row container.)

```auto
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.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [May 8, 2023, 8:24pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/3 "2023-05-08T20:24:37Z")

</div>

You can use enumerate() for stuff like this:

```auto
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:

```auto
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...

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 8, 2023, 8:59pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/4 "2023-05-08T20:59:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 8, 2023, 9:09pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/5 "2023-05-08T21:09:22Z")

</div>

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.

```auto
	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
```

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 8, 2023, 9:16pm UTC](https://forum.inductiveautomation.com/t/in-a-perspective-table-how-would-i-select-a-row-with-a-particular-value-in-it-upon-loading-the-view/73435/6 "2023-05-08T21:16:21Z")

</div>

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:

```auto
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
```
