# Delete selected item and associated sql data from tree component when DEL key is pressed

**URL:** <https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [June 3, 2024, 9:54am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275 "2024-06-03T09:54:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmadariaga](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmadariaga/32/63201_2.png) [@mmadariaga](https://forum.inductiveautomation.com/u/mmadariaga)\
**Post date:** [June 3, 2024, 9:54am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/1 "2024-06-03T09:54:59Z")

</div>

Hello, I've got a tree component connected to a table. When I click an item on the tree, it queries data from sql with the selected id and the displayed table changes. I would like to delete the sql data that I select on the tree when I press DEL key.

I've read a post where it's encouraged to use Session Events. I've tried that approach but I can't manage to do it.

```python
# Tree onItemClicked
def runAction(self, event):
	self.parent.parent.getChild("SelectedFileTable").custom.uploaded_id = self.props.selectionData[0].value

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/b/dbb10f4a617645f47f29e3d5218e7bd748a7db18.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:** [June 3, 2024, 10:57am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/2 "2024-06-03T10:57:10Z")

</div>

In [Perspective - Tree | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/components/perspective-components/perspective-display-palette/perspective-tree) the following information is given for **selecitonData**.

| name | Description | Property type |
| --- | --- | --- |
| selectionData | Array of objects containing the data and index path for all currently selected nodes. **itemPath** : Index path. Value is numeric. **value** : The value of the 'data' property for the selected node. | Value is string. array |

 ![Tree selection data](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/f/6fb9690c2f3a65e4186e77862580221f995c8634.png)

We can see that you have,  
`items.1.items.0.data : 6`

If you select that item on the tree then you should have two pieces of information in `selectionData`.  
`selectionData.0.itemPath : 1/0`  
`selectionData.0.value : 6`

You are going to use SQL to delete the rows from the database so the `data` property should contain a unique key for the query. For example, using a named query with four parameters it might look like this.

```python
DELETE FROM ReportTable
WHERE idfile = :idfile
  AND idcust = :idcust
  AND idline = :idline
  AND dateprod = :dateprod

```

That means that the `data` field will look like this:

```python
{
  "idfile": 6,
  "idcust": 991,
  "idline": 1,
  "dateprod": '2024-01-29'
}

```

Play with that. If you get into difficulty then please show how you are constructing the tree.

---

<div class="post-metadata">

**Author:** ![mmadariaga](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmadariaga/32/63201_2.png) [@mmadariaga](https://forum.inductiveautomation.com/u/mmadariaga)\
**Post date:** [June 3, 2024, 11:05am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/3 "2024-06-03T11:05:08Z")

</div>

Thanks for the reply. I see that this would work, but how could I associate the delete query to a key press (DEL, for example)?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 3, 2024, 2:35pm UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/4 "2024-06-03T14:35:44Z")

</div>

Add a key event listener in session events that broadcasts a message (system.perspective.sendMessage) and then listen for that event on your table.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 3, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/5 "2024-06-03T14:50:27Z")

</div>

Maybe check if the component is in focus first

---

<div class="post-metadata">

**Author:** ![mmadariaga](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmadariaga/32/63201_2.png) [@mmadariaga](https://forum.inductiveautomation.com/u/mmadariaga)\
**Post date:** [June 4, 2024, 10:49am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/6 "2024-06-04T10:49:34Z")

</div>

Thanks!! It worked.

---

<div class="post-metadata">

**Author:** ![mmadariaga](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmadariaga/32/63201_2.png) [@mmadariaga](https://forum.inductiveautomation.com/u/mmadariaga)\
**Post date:** [June 4, 2024, 10:52am UTC](https://forum.inductiveautomation.com/t/delete-selected-item-and-associated-sql-data-from-tree-component-when-del-key-is-pressed/91275/7 "2024-06-04T10:52:38Z")

</div>

Thanks for the advice (Sorry for the edits, I didn't see it was a response to PGriffith)
