# How to access selection of a row of a table in a icon script

**URL:** <https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [May 16, 2024, 7:18am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475 "2024-05-16T07:18:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 7:18am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/1 "2024-05-16T07:18:00Z")

</div>

```python
def runAction(self, event):
	if (self.parent.parent.parent.parent.parent.getChild("Table").onSelectionchange(event.selectedRow)==1):
		params = {'id':self.parent.parent.parent.parent.parent.getChild("Table").props.selection.data[0].ID}
		system.db.runNamedQuery("Delete_Rows", params)
		system.db.runNamedQuery("Edit_data")
		self.parent.parent.parent.parent.parent.getChild("Table").refreshBinding("props.data")
	else:
		system.perspective.openPopup("AlertId",view,params={'display':"Select the row to be Deleted"} )
	

```

i want to know whether the row has been selected or not  
additionally is the popup id correct if not how to get it

---

<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 16, 2024, 8:20am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/2 "2024-05-16T08:20:58Z")

</div>

**Relative paths vs custom properties**  
As I'm sure you have found, `self.parent.parent.parent.parent.parent.getChild` syntax is awkward to set up, difficult to read and very hard to maintain when components are moved in a view. Try this:

- On the view, create a custom property to hold a copy of the table data you are examining.
- Create a Property Binding on that table property, point it at the custom property and set the binding to bidirectional.
- Now you can replace the awkward syntax with `self.view.custom.myCustomProp` or whatever you called it.

**Row selected or not**

> i want to know whether the row has been selected or not.

Create a `view.custom.selectedRow` property. Bind table `props.selection.selectedRow` to the custom property with a bidirectional property binding.  
Now you can use  
`if self.view.custom.selectedRow:`  
A non-null value indicates that a row is selected.

**Popup**

> additionally is the popup id correct if not how to get it

You don't need the popup. Disable or hide the delete button if no row has been selected. Don't waste the user's time by presenting options that won't work.

---

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 8:39am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/3 "2024-05-16T08:39:12Z")

</div>

Thanks a lot i will try it

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 16, 2024, 9:03am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/4 "2024-05-16T09:03:25Z")

</div>

I send you an onActionPerformed Script I did with the help of Transistor in case it helps:

```python
def runAction(self, event):
		
	# Reference the table component directly
	table = self.getSibling("tblWorkRequest")
	
	# Retrieve the selected rows
	selected_rows = table.props.selection.data
	
	# Check if any row is selected
	if not selected_rows:
	    system.perspective.messageBox("Please select a row to process", title="Notice")
	else:
	    # Process the first selected row (if only one row is expected)
	    selected_row = selected_rows[0]
	
	    # Extract specific columns' data using the column names
	    sede = selected_row.get("SEDE") # Replace with the actual column name
	    solicitud = selected_row.get("SOLICITUD") # Replace with the actual 
              column name
	
	
	    popup_params = {
	        "sede": sede,
	        "solicitud": solicitud
	    }
	
	    # Open the popup view, passing the parameters
	    system.perspective.openPopup(
	        id="aprobar-popup", # A unique identifier for this popup instance
	        view="Page/Aprobar",
	        params=popup_params,
	        title="Aprobar Solicitud",
	        modal=False,
	        width=400,
	        height=300

	)

```

---

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 9:09am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/5 "2024-05-16T09:09:40Z")

</div>

> [@Transistor](#):
>
> Create a `view.custom.selectedRow` property. Bind table `props.selection.selectedRow` to the custom property with a bidirectional property binding.

i can't comprehend it correctly  
let me explain it if i am wrong correct me , i want to create a custom property in view or page and write the view.custom.selectedRow in it and bind it to table proprety data or something like that please explain with a example

---

<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 16, 2024, 9:11am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/6 "2024-05-16T09:11:13Z")

</div>

> [@Alejandro\_Alaco](#):
>
> I did with the help of Transistor

I think I would have told you the same thing. Disable the button if no row is selected. Don't have unnecessary popups to annoy the user.

---

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 9:13am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/7 "2024-05-16T09:13:20Z")

</div>

but the highers want that function so i have to do it

---

<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 16, 2024, 9:16am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/8 "2024-05-16T09:16:40Z")

</div>

Sorry, it is just wrong and bad GUI design.

What is their specification?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 16, 2024, 9:22am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/9 "2024-05-16T09:22:52Z")

</div>

Yes, you right, I just fixed it up, so the user get a message in case they didn´t select any row to remind them to do so.

```python

	selected_rows = table.props.selection.data

	if not selected_rows:
		
	         system.perspective.openPopup(
            id="noneSelected-popup", # A unique identifier for this popup instance
            view="Page/NoneSelected",
            title="No hay fila seleccionada",
            modal=True,
            width=300,
            height=200
        )

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/d/5d7bb71beff16ce6522beb560d0f37b7cf74a674.png)

---

<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 16, 2024, 9:30am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/10 "2024-05-16T09:30:53Z")

</div>

> [@Alejandro\_Alaco](#):
>
> so the user get a message in case they didn´t select any row to remind them to do so.

But there is no need!  
**Disable the button until the user selects a row!**

![Is row selected](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/8/789a6ee6f80ccbbc3110559e04050403c1bec618.png)

_Expression binding on button's enabled property._

No script code.  
No popup.  
No time wasted by the designer (once) or the user (every day).

---

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 9:35am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/11 "2024-05-16T09:35:51Z")

</div>

but if i bind table to that property what about query update

---

<div class="post-metadata">

**Author:** ![Selvapandiaraj\_A](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Selvapandiaraj\_A](https://forum.inductiveautomation.com/u/Selvapandiaraj_A)\
**Post date:** [May 16, 2024, 9:52am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/12 "2024-05-16T09:52:08Z")

</div>

if i change it to a relative path i can't access the selection function

```python
Caused by: org.python.core.PyException: AttributeError: 'com.inductiveautomation.ignition.gateway.datasourc' object has no attribute 'selection'

```

it shows error like this help me transistor

---

<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 16, 2024, 9:59am UTC](https://forum.inductiveautomation.com/t/how-to-access-selection-of-a-row-of-a-table-in-a-icon-script/90475/13 "2024-05-16T09:59:21Z")

</div>

Your script becomes something like this:

```python
def runAction(self, event):
	# If button was enabled then a row has been selected.
	params = {'id': self.view.custom.selectedRow[0].ID}
	system.db.runNamedQuery("Delete_Rows", params)

	system.db.runNamedQuery("Edit_data")

	# Refresh the table binding to show the updated records.
	system.perspective.sendMessage("tableRefresh", payload = {}, scope = "view")

```

Then on the table you right-click, Configure Scripts, ...

 ![Message hander refresh table data](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/a/8aa5a75ef64923361e6e18f9d1e1d9945e54c39f.png)

> **[system.perspective.sendMessage | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-sendMessage)**
>
> Send a message to a message handler within the same session.

> **[Component Message Handlers | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/scripting-in-perspective/component-message-handlers)**
>
> In Perspective, Component Message Handlers are the preferred way to pass parameters between components or Views. Doing this involves the system.perspective.sendMessage function. There are typically two steps involved:
