Power Table drag and drop

I cannot get this to function. I am using the code in the manual but it literally does nothing.

[code]
destDataset = event.source.data.getData()
pyRowData = system.dataset.toPyDataSet(rowData)

Loop thru all the rows that have been selected and dragged to the

destination table.

for row in pyRowData:
newRow = []
for column in row:
newRow.append(column)
destDataset = system.dataset.addRow(event.source.data, dropIndexLocation, newRow)

Adds the rows to the destination table.

event.source.data.setData(destDataset)

Optional. Deletes the dragged rows from the source table.

#sourceDataset = system.dataset.deleteRows(sourceTable.getData(), rows)
#sourceTable.setData(sourceDataset)[/code]

Thanks!!

Did you indent all your code?

The editor wants to put your first line at the left margin, but because you’re doing the body of the function, it needs to start out indented. That’s tripped me up more than once.

I don’t think this is it. I tried a single indent of all of the code and still have the same result. I can initiate a messagebox inside the event but if I try writing the internal variables of the function to custom variables on the root container it doesn’t write anything. I also get no errors either. Very wierd. Ignition version 7.7.0

I don’t know why but it works now with this code:

[code] # manipulate underlying data here

destDataset = self.getData()
pyRowData = system.dataset.toPyDataSet(rowData)        
#system.gui.messageBox("TYES1")	
	 
	
# Loop thru all the rows that have been selected and dragged to the 
# destination table.        
for row in pyRowData:
	#system.gui.messageBox("TYES2")
	newRow = []
	for column in row:
		#system.gui.messageBox("TYES3")
		newRow.append(column)
	destDataset = system.dataset.addRow(destDataset,dropIndexLocation , 
		newRow)
	
		
# Adds the rows to the destination table.
self.setData(destDataset)
#event.source.parent.dataSet.setData(destDataset)

 

# Optional. Deletes the dragged rows from the source table.        
sourceDataset = system.dataset.deleteRows(sourceTable.getData(), rows)
sourceTable.setData(sourceDataset)

[/code]

Hmmm… I can’t replicate this. Here’s what I did:
[ul][li]Set up two power tables on a window.[/li]
[li]Both have row dragging enabled. [/li]
[li]Both are using the test data. [/li]
[li]In onRowsDropped, both have exactly the script from the manual[/li][/ul]
Dragging and dropping works like a charm.

If you try exactly these steps and still have a problem, you may want to have Tech Support do a GoToMeeting with you. I’m sure it’s something easy that’s been overlooked.

(note that the online manual isn’t displaying the initial indentation very well. I’ll see what I can do to fix that for 7.7.2. To make sure there’s no questions, I’ve uploaded a screenshot of the script.)


Ha! posted at the same time.

Glad it’s working for you now. :slight_smile:

I did have another question though. If I am dragging and dropping to the same table, what method would you recommend for deleting the original row? I don’t think the code at the bottom will work in this situation because if you have inserted the row above the original location then the row index will have gone up by 1 (or by as many rows as you’ve dropped).

You got me curious, so I experimented. As you suspected, the indices get messed up. If I get time later on, I’ll try to whip up a script that handles this correctly. (Don’t hold your breath, though. I literally have 70 things on my to do list right now.) :confused:

I think I got it. Requires 2 custom properties on the table component

[code] self.droppedRow = dropIndexLocation
self.originalRow = rows[0]
if system.gui.confirm(“Move KQC?”):
if self.droppedRow > self.originalRow:
# manipulate underlying data here
destDataset = self.getData()
pyRowData = system.dataset.toPyDataSet(rowData)
# Loop thru all the rows that have been selected and dragged to the
# destination table.
for row in pyRowData:
newRow = []
for column in row:
newRow.append(column)
destDataset = system.dataset.addRow(destDataset,dropIndexLocation, newRow)
#if dropIndexLocation <= rows(0,0):
#rows = rows + 1

		# Adds the rows to the destination table.
		#system.gui.messageBox(rows)
		self.setData(destDataset)
		# Optional. Deletes the dragged rows from the source table.        
		sourceDataset = system.dataset.deleteRows(sourceTable.getData(), rows)
		sourceTable.setData(sourceDataset)
	if self.droppedRow <= self.originalRow:
		# manipulate underlying data here
		destDataset = self.getData()
		pyRowData = system.dataset.toPyDataSet(rowData)        
		# Loop thru all the rows that have been selected and dragged to the 
		# destination table.        
		for row in pyRowData:
			newRow = [] 
			for column in row:
				newRow.append(column)
			destDataset = system.dataset.addRow(destDataset,dropIndexLocation, newRow)
		#if dropIndexLocation <= rows(0,0):
			#rows = rows + 1
			
		# Adds the rows to the destination table.
		#system.gui.messageBox(rows)
		self.setData(destDataset)
		# Optional. Deletes the dragged rows from the source table. 
		deleteRow = self.originalRow + 1
		rows = [deleteRow]       
		sourceDataset = system.dataset.deleteRows(sourceTable.getData(), rows)
		sourceTable.setData(sourceDataset)[/code]
2 Likes

:thumb_left:

Just a note for any future users who cruise through this thread – a quick glance says this is probably relying on multiple interval selection being turned off. You’ll have a much more fun (not!) calculation if it’s turned on.

1 Like

Yes, sorry I forgot to mention that this only applies to a single selection. I did not calculate a range on the rows selected as it did not apply to my project. Thanks for your help!!

Thanks! Saved me an hour…

2 Likes

Here is what I cam up with. This allows a single row to be moved between tables, and reordered in it’s own table. I have tested it with a few different sets of data, but let me know if you find something that doesn’t work. Put this code on the onRowsDropped function of all tables that you want to enable dragging on.

This does not work on tables that allow multiple rows to be selected.

Hopefully the comments help explain what I’m doing.

# If dragging to another table, rather than itseelf.
if self != sourceTable:
	#Get the current dataset of the destination table. All the rows
	destDataset = self.getData()
	
	#Convert the row that was dragged from the source table, to a py dataset
	pyRowData = system.dataset.toPyDataSet(rowData)        

	#Loop through all rows that were selected (should just be 1)
	#And then loop through each column, adding it to the 'newRow' array
	#Then, add the new row to the destination dataset.
	for row in pyRowData:
		newRow = []
		for column in row:
			newRow.append(column)
		destDataset = system.dataset.addRow(destDataset, dropIndexLocation, newRow)
			
	# Add the entire dataset to the table object
	self.setData(destDataset)
	
	# Deletes the dragged rows from the source table.
	# rows is actually an array, but we are only selecting one row      
	sourceDataset = system.dataset.deleteRows(sourceTable.getData(), rows)
	sourceTable.setData(sourceDataset)

#Dragging to itself		
else:

	#Convert the dragged row to a pyDataSet
	pyRowData = system.dataset.toPyDataSet(rowData) 

	#Get the number of rows originally in the dataset, used to calculate index later
	sourceDataset_OrignalCount = self.getData().getRowCount()

	#Delete the row from the dataset
	sourceDataset = system.dataset.deleteRows(self.getData(), rows)

	#Loop through the dragged row data, and add it to the dataset
	for row in pyRowData:
		newRow = []
		for column in row:
			newRow.append(column)

		#If dragging to the row after the last one, there will need to be an adjustment made.
		#The index was deleted earlier, so the new one will be out of range.
		#We need to reduce the index by 1
		if sourceDataset_OrignalCount == dropIndexLocation:
			sourceDataset = system.dataset.addRow(sourceDataset, dropIndexLocation-1, newRow)
		#If the row was not dragged to the end, nothing needs to be changed
		else:
			sourceDataset = system.dataset.addRow(sourceDataset, dropIndexLocation, newRow)

	# Add the altered dataset back to the table object.
	self.setData(sourceDataset)
	

1 Like

This allows a single row or multiple rows to be moved between the same table. Seems to work with multiple and single interval selection mode. Sorry for the list comprehensions. The try and except block are needed because of indexing issues when moving rows to the bottom of a dataset.

data = self.data

allRows = [[rowData.getValueAt(rowNdx, colNdx) for colNdx in range(rowData.columnCount)] for rowNdx in range(rowData.rowCount)]

try:
	self.data = system.dataset.deleteRows(self.data,rows)
	self.data = system.dataset.addRows(self.data, dropIndexLocation, allRows)
except:
	data = system.dataset.addRows(data, dropIndexLocation, allRows)
	data = system.dataset.deleteRows(data,rows)
	self.data = data

self.selectedRow = -1
3 Likes