Fetch item in jinja template

hello,
i want to customize sales order by jinja template.
i have this code:

<table class="table table-condensed cart no-border">
	<thead>
	   {%- for item in doc.items -%}
	    <tr><th colspan=6>{{ item.item_name }}</th></tr>
	    <tr>
	        <th width="5%">{{ _("Sr.") }}</th>
	        <th width="15%" class="text-right">{{ _("Widht") }}</th>
	        <th width="15%" class="text-right">{{ _("Hight") }}</th>
	        <th width="15%" class="text-right">{{ _("QTY SQM") }}</th>
	        <th width="15%" class="text-right">{{ _("RATE") }}</th>
	        <th width="15%" class="text-right">{{ _("AMOUNT") }}</th>
	   </tr>
	  </thead>
	  <tbody>
	      <tr>
	          <td>{{loop.index}}</td>
	          <td class="text-right">{{ item.width }}</td>
	          <td class="text-right">{{ item.hight }}</td>
	          <td class="text-right">{{ item.qty }}</td>
	          <td class="text-right">{{ item.rate }}</td>
	          <td class="text-right">{{ item.amount }}</td>
	       </tr>
	 {%- endfor -%}
	</tbody>
	 <tfoot>
    <tr>
    	<td colspan=6></td>
    </tr>
  </tfoot>
</table>   

and the output will come like this:

and i need like this:

my target to achieve all items from one product below name of product.
please if some one can help me.

You can try the Jinja groupby function

https://jinja.palletsprojects.com/en/2.11.x/templates/#groupby

how can i do this? i trying many option
i have this error

Traceback (most recent call last):
File “/home/frappe/benches/bench-version-13-f1-2/apps/frappe/frappe/utils/jinja.py”, line 83, in render_template
return get_jenv().from_string(template).render(context)
File “/home/frappe/benches/bench-version-13-f1-2/env/lib/python3.6/site-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/home/frappe/benches/bench-version-13-f1-2/env/lib/python3.6/site-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/home/frappe/benches/bench-version-13-f1-2/env/lib/python3.6/site-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File "

this is code:

<table class="table table-condensed cart no-border">
	<thead>
	       {%- for item, item in doc.items|groupby("item.item_name") -%}
	    <tr><th colspan=6>{{ item.item_name }}</th></tr>
	    <tr>
	        <th width="5%">{{ _("Sr.") }}</th>
	        <th width="15%" class="text-right">{{ _("Widht") }}</th>
	        <th width="15%" class="text-right">{{ _("Hight") }}</th>
	        <th width="15%" class="text-right">{{ _("QTY SQM") }}</th>
	        <th width="15%" class="text-right">{{ _("RATE") }}</th>
	        <th width="15%" class="text-right">{{ _("AMOUNT") }}</th>
	   </tr>
	  </thead>
	  <tbody>
	       {%- for item in doc.items -%}
	        <tr>
	          <td>{{loop.index}}</td>
	          <td class="text-right">{{ item.width }}</td>
	          <td class="text-right">{{ item.hight }}</td>
	          <td class="text-right">{{ item.qty }}</td>
	          <td class="text-right">{{ item.rate }}</td>
	          <td class="text-right">{{ item.amount }}</td>
	       </tr>
        	 {%- endfor -%}
	 {%- endfor -%}
	</tbody>
	 <tfoot>
    <tr>
    	<td colspan=6></td>
    </tr>
  </tfoot>
</table>

You need the header only once. So why put it inside the loop? You only need the body inside the loop.

Yes i agree with you, this error, but the main issue still not fixing.
I am trying to find how can i loop 1 items many times inside loop.

Try this

<table class="table table-condensed cart no-border">
	<thead>
	       {%- for group in doc.items | groupby("item_name") -%}
	    <tr><th colspan=6>{{ group.grouper }}</th></tr>
	    <tr>
	        <th width="5%">{{ _("Sr.") }}</th>
	        <th width="15%" class="text-right">{{ _("Widht") }}</th>
	        <th width="15%" class="text-right">{{ _("Hight") }}</th>
	        <th width="15%" class="text-right">{{ _("QTY SQM") }}</th>
	        <th width="15%" class="text-right">{{ _("RATE") }}</th>
	        <th width="15%" class="text-right">{{ _("AMOUNT") }}</th>
	   </tr>
	  </thead>
	  <tbody>
	       {%- for item in group.list -%}
	        <tr>
	          <td>{{loop.index}}</td>
	          <td class="text-right">{{ item.width }}</td>
	          <td class="text-right">{{ item.hight }}</td>
	          <td class="text-right">{{ item.qty }}</td>
	          <td class="text-right">{{ item.rate }}</td>
	          <td class="text-right">{{ item.amount }}</td>
	       </tr>
        	 {%- endfor -%}
	 {%- endfor -%}
	</tbody>
	 <tfoot>
    <tr>
    	<td colspan=6></td>
    </tr>
  </tfoot>
</table>
1 Like

Great its working, Thank you very much.

here is the result:
Capture