# Pretty simple code does not work and IDK why

**URL:** <https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 30, 2017, 7:31pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208 "2017-08-30T19:31:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [August 30, 2017, 7:31pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/1 "2017-08-30T19:31:56Z")

</div>

Hello,

I have a powertable on the form in the window called XYZ\_Images. This powertable does have a DB table as a source. In this table, records are being written (sometimes few per Minute, sometimes no record written for half an hour). The SELECT ORDERs BY timestamp descending (most recent row should be the first in the table). I’d like to refresh the powertable with client script every 10 seconds and in case new record has been added, I’d like to select programatically first row. I’ve done this:

```auto
def refreshList():
	try:
		rowcountBeforeRefresh = system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table').data.rowCount
		system.db.refresh(system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table'),'data')
		rowcountAfterRefresh = system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table').data.rowCount
		if rowcountBeforeRefresh < rowcountAfterRefresh:
			 system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table').selectedRow = 0	
	except:
		pass 
```

The table is being refreshed flawlessly, the first row is not selected though.

Thank you in advance for the hints.

---

<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:** [August 30, 2017, 8:49pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/2 "2017-08-30T20:49:25Z")

</div>

```auto
def refreshList():
	try:
		rowcountBeforeRefresh = system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table').data.rowCount
		system.db.refresh(system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table'),'data')
		rowcountAfterRefresh = system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table').data.rowCount
		if rowcountBeforeRefresh < rowcountAfterRefresh:
			def later(target=system.gui.getWindow('XYZ_Images').getRootContainer().getComponent('Power Table'))
				target.selectedRow = 0
			system.util.invokeLater(later)
	except:
		pass

```

It’s possible that the selectedRow modification is happening _before_ the table repopulates with data - try wrapping it in an `invokeLater` so that it happens after the rest of the processing on the event dispatch thread.

[https://docs.inductiveautomation.com/display/DOC79/system.util.invokeLater](https://docs.inductiveautomation.com/display/DOC79/system.util.invokeLater)

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [August 31, 2017, 8:13pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/3 "2017-08-31T20:13:18Z")

</div>

> [@paul-griffith](#):
>
> def refreshList():  
> try:  
> rowcountBeforeRefresh = system.gui.getWindow('XYZ\_Images').getRootContainer().getComponent('Power Table').data.rowCount  
> system.db.refresh(system.gui.getWindow('XYZ\_Images').getRootContainer().getComponent('Power Table'),'data')  
> rowcountAfterRefresh = system.gui.getWindow('XYZ\_Images').getRootContainer().getComponent('Power Table').data.rowCount  
> if rowcountBeforeRefresh \< rowcountAfterRefresh:  
> def later(target=system.gui.getWindow('XYZ\_Images').getRootContainer().getComponent('Power Table'))  
> target.selectedRow = 0  
> system.util.invokeLater(later)  
> except:  
> pass

Nope, it does not work anyway. The behavior is still the same new record is being loaded, but it is not selected programatically. However, after discussing this feature with the users, they came to concluson they don't need it, therefore I don't need the solution to this problem. However, Thank you Paul for the effort. Take care.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 1, 2017, 11:03am UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/4 "2017-09-01T11:03:48Z")

</div>

I’ll give a solution anyway. That way, if the customer decides they want it after all, you have it in your back pocket. 😉

- On the Power Table, add a custom property: tableLength

- Set tableLength to the following expression (this one assumes the table is in the root container):  
`len({Root Container.Power Table.data})`

- Use a propertyChange script on the PowerTable to monitor tableLength:

```auto
if event.propertyName == 'TableLength':
  if event.newValue > event.oldValue:
    event.source.selectedRow = 0  

```

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [September 1, 2017, 12:37pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/5 "2017-09-01T12:37:10Z")

</div>

Hello Jordan,

that’s a pretty cool approach. I’m an Ignition-newbie, I would not think of that ;-). Thank you, take care.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 1, 2017, 1:07pm UTC](https://forum.inductiveautomation.com/t/pretty-simple-code-does-not-work-and-idk-why/15208/6 "2017-09-01T13:07:55Z")

</div>

I recall playing with these events and noted that assignment to data would generate events on both data and selectedRow, like so: -1 =\> selectedRow, newdata =\> data, previously selected row =\> selectedRow. So to change the selected row on a data event, the assignment had to be deferred until after the internal reset of the selected row back to its previous value. I haven’t investigated the event order for the Table lately, but you might want to put some print statements in your propertyChange script to see what’s happening, and in what order.
