Report viewer refresh

Hello erveryone,

I have a report with two custom properties.

The user selects one or more rows from a table and clicks a button to print the relative labels.

The problem is that a multiple selection does not update the report parameters and I basically get the same label printed n times.

I have debugged the code and I can see that in the for loop the data I expect is infact correct

I have tried to add an invokelater function to pause the code for the report to update but it’s not working.

What am I missing ??

Thanks in advance,

t = event.source.parent.getComponent('Table')
t1 = t.getTable()

# righe selezionate
sr = t1.getSelectedRows()
# totale righe selezionate
src = t1.getSelectedRowCount()

def	refresh():
	if nome_stampante != '':
		reportViewer = event.source.parent.getComponent('Report Viewer')
		reportViewer.print(nome_stampante, False)


for x in sr:
	
	order_id = t1.getValueAt(x, 0)
	codice = t1.getValueAt(x, 2)		

	event.source.parent.getComponent('Report Viewer').CODICE = codice
	event.source.parent.getComponent('Report Viewer').ORDER_ID = order_id

	#system.gui.messageBox(str(codice) + ' ' + str(order_id),'')
	
	system.util.invokeLater(refresh,2000)		

	#exit()
	
		

You can't do this will a simple loop in a script. system.util.invokeLater() does not wait in the caller--you get a bunch of refresh() calls piled up together in the event queue. All of your assignments to codice and order_id have executed before the first invoked refresh() runs.

What could work would be a single invokeLater to print the first label, and that refresh doing the next invokeLater for another label. You will need to learn to pass arguments to refresh via partial() or default args in order to track the place in the list.

Supplied code is for 8.3

When you set the parameters (CODICE, ORDER_ID), the ReportViewer reloads the report with the new parameters applied. If the ReportViewer is already busy loading a report, it will queue the load request until the ReportViewer has completed reloading that report. After it completes loading, it will then load the most recent request in the queue.

Here’s a project to more easily understand the problem. It has a 5 second delay when loading the dataset. It will popup a Print window that you can hit cancel and dismiss or print. Note what you see in the ReportViewer, when the Print window pops up, is what will be printed.

report_print_from_table_selection_illustration_of_problem.zip (12.7 KB)

In the above project, if you select all 3 items in the table what is loaded is not deterministic due to queuing. It might load the report for order_id 1 and order_id 3 but not order_id 2.

When you call print(), this will print what’s currently displayed, or if the viewer is showing loading, what was last displayed. So in the above scenario, if order_id 2 was never loaded, it can never be printed, or if the print() call is not called for the loaded order_id that won’t be printed either.

One way to get this to work is to do the following:

  1. Set the parameters (CODICE, ORDER_ID) this will trigger a load of the report
  2. Wait until the report finishes loading
  3. Print the report
  4. Go to the next selected item

For step 2 above, we use ReportViewer#onReportGenerated which is called after a report loads. Here’s a solution.
report_print_from_table_selection_solution_1.zip (13.2 KB)

Another possible route is to drop a PDFViewer on the same Window:

  1. Use system.report.executeReport to generate the report and get the PDF bytes
  2. Use PDFViewer’s loadPDFBytes() to load the bytes obtained in step 1
  3. Use PDFViewer’s print() to the print the report
3 Likes

Hello Vi_Quang,

Thankyou for your reply and the file you sent!

At runtime , I’m getting an error in the processSelectedItems() function at line 16 , “the object dataset is unscriptable…”

I debugged and the data is there…not sure what’s going on ; I am using Ignition V8.1.49

Thanks,

Dominic

You have to convert the dataset to PyDataSet in 8.1. See the attached.

report_print_from_table_selection_solution_1_8.1.zip (17.9 KB)

Hello Vi_Quang,

Excellent job! It’s working fine.

Thankyou for your support, much appreciated!

1 Like