Get dynamic columns in print formatting

I made a sample script report with dynamic columns. In print I want to get column values I tried this code below but I got an error Uncaught ReferenceError: columns is not defined. Any idea how to get the columns values?

<h2 class="text-center">{%= __("Subject Grading Report") %}</h2>
<hr>
<table class="table table-bordered">
	<thead>
		<tr>
		{% for(var i=0, l=columns.length; i<l; i++) { %}
                      <h1> test </h1>
		{% } %}
		</tr>
	</thead>
</table>
1 Like

Hi @ccfiel,

Please check how frappe gets the columns names and data from report: columns , data
This is done by micro-templates so you first need to pass dictionary to your print format template
for example:

opts = {
      columns: list_of_column,
      data: report_data
}

Then pass the opt to print format template. please frappe.render() for example.

Thanks,
Makarand Bauskar

1 Like

Hello @makarand_b,

Thanks for the reply. The link you gave me is the source of frappe so I am in the impression that data and columns are default pass to the print format as I see the code frappe/print_grid.html at develop · frappe/frappe · GitHub that it can access the columns variable in the print format because when I do this code below data has a value but columns is undefined. So why I can access data but not the columns?

<h2 class="text-center">{%= __("Subject Grading Report") %}</h2>
<hr>
<table class="table table-bordered">
	<thead>
		<tr>
		{% for(var i=0, l=data.length; i<l; i++) { %}
			<th>
				Hello
			</th>
		{% } %}
		</tr>
	</thead>
</table>

Hello @makarand_b,

I was able to figure it out how to get the columns value in the print format template using report.columns example is in erpnext/accounts/report/financial_statements.html

<h2 class="text-center">{%= __("Subject Grading Report") %}</h2>
<hr>
<table class="table table-bordered">
	<thead>
		<tr>
		{% for(var i=0, l=report.columns.length; i<l; i++) { %}
			<th class="text-right">{%= report.columns[i].label %}</th>
		{% } %}
		</tr>
	</thead>
</table>

Thanks!

1 Like