Select the first row of a table as soon as it is done loading

How would you go about this? I have implemented something that works much of the time, but is hacky and reliant on use of a Timer to continue checking whether the selection data exists. It seems like the only way this could work reliably would be to invoke the selection "on load" which is an event handler that Perspective doesn't have, and which I created a request for because of a different but similar scenario (the need to focus an input once a view is done loading).

Use a change event attached to the tables props.data. In the script, check if the length of the new data (handed to you in the event) is greater than zero. If so, set the selected row to zero.

1 Like

I've tried that and it doesn't work:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if len(currentValue.value) > 0:
		self.props.selection.selectedRow = 0

(It actually did work the very first time I opened the popup containing the view, but hasn't worked in any subsequent openings.)

This does work:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	from threading import Timer
	
	def onLoad(self):
		self.props.selection.selectedRow = 0
	
	if len(currentValue.value) > 0:
		Timer(0.2, onLoad, [self]).start()

...but not on slower machines, or if I throttle my CPU.