Sort the Qweb report line numerically in POS_Sale_detail report - reporting

I want to sort the qweb report from descending to ascending based on the below marked field,on the image
enter image description here
the xml code for the report is as follows, the table must be sorted based on the field which is written in bold
Products
<table class="table table-condensed">
<thead><tr>
<th>Product</th>
<th>Quantity</th>
<th>Cost Price</th>
<th>Total Cost </th>
<th>Price Unit</th>
<th>Amount without VAT</th>
<th>VAT</th>
<th>VAT Amount</th>
<th>Total Amount</th>
</tr></thead>
<tbody>
<tr t-foreach="products" t-as="line">
<td>
<t t-esc="line['product_name']"/>
<td>
<t t-esc="line['quantity']"/>
<t t-if="line["uom"] != "Unit(s)"">
<t t-esc="line["uom"]"/>
</t>
</td>
<td>
<t t-esc="'%.2f'%(request.env['product.product'].browse(line['product_id']).standard_price)"/>
</td>
<td>
<t t-esc="'%.2f'%(line['quantity']*request.env['product.product'].browse(line['product_id']).standard_price)"/>
</td>
<td>
<t t-esc="'%.2f'%line["price_unit"]"/>
<t t-if="line["discount"] != 0">
Disc: <t t-esc="line["discount"]"/>%
</t>
</td>
<td>
<t t-esc="'%.2f'%((line['quantity']*line['price_unit'])/1.05)"/>
</td>
<td>
5%
</td>
<td>
<t t-esc="'%.2f'%(((line['quantity']*line['price_unit'])/1.05)*0.05)"/>
</td>
****<td>
<t t-esc="'%.2f'%(line['quantity']*line['price_unit'])"/>
</td>****
</tr>
</tbody>
</table>
kindly give the code for the same field, and where to place the code

Assuming products is a list of dicts and you need to sort using Total (quantity * price_unit).
You can use sorted with t-foreach:
<tr t-foreach="sorted(products, key=lambda d: d['quantity']*d['price_unit'])" t-as="line">
If you want to reverse the order, set reverse argument to True.

Related

How can I create dynamic table in Thymeleaf..?

I am new to Thymeleaf and trying to create a dynamic table on Themeleaf template.
How can I do it..??
I have been googling by I didn't got any proper answer. The issue is I cannot iterate List< Map< String,Object >>. I can have any number of columns and columns name could be any thing.
<tr class="headings">
<th class="column-title">ID</th>
<th class="column-title">Name</th>
<th class="column-title">Salary</th>
<th class="column-title">Status</th>
</tr>
</thead>
<tbody>
<tr class="even pointer" th:each="row:${rows}" id = "tablerow">
<td class=" " th:text="${row.getId()}">Id</td>
<td class=" " th:text="${row.getName()}">Name</td>
<td class=" " th:utext="${row.getSalary()}">Salary</td>
<td class=" " th:text="${row.getStatus()}">Active</td>
</tr>
</tbody>
I need to set values dynamically since if query of result will keep changing . right now column name are hard coded and value are also getting by row.getId what if there is no Id, it could be anything in rows what shall I use than..? example row.<>.
rows is obtained as List< Map< String, Object>>.
Thanks in advance.
You can iterative over a Map just as easily as you can a List. The simplest form of this would be:
<tbody>
<tr class="even pointer" th:each="row: ${rows}" id="tablerow">
<td th:each="field: ${row}" th:text="${field.value}" />
</tr>
</tbody>
However, since Maps don't have a specific ordering (unless you're using something like a TreeMap), the way I would do it would be something like this (complete example should match your example table):
Controller
List<String> headers = Arrays.asList("ID", "Name", "Salary", "Status");
List<Map<String, Object>> rows = new ArrayList<>();
rows.add(Map.of("ID", "1", "Name", "Jim", "Salary", "50000", "Status", "active"));
rows.add(Map.of("ID", "2", "Name", "Sally", "Salary", "50000", "Status", "inactive"));
Template
<table>
<thead>
<tr class="headings">
<th th:each="header: ${headers}" class="column-title" th:text="${header}" />
</tr>
</thead>
<tbody>
<tr class="even pointer" th:each="row: ${rows}" id="tablerow">
<td th:each="header: ${headers}" th:text="${row.get(header)}" />
</tr>
</tbody>
</table>
Which will produce:
<table>
<thead>
<tr class="headings">
<th class="column-title" >ID</th>
<th class="column-title" >Name</th>
<th class="column-title" >Salary</th>
<th class="column-title" >Status</th>
</tr>
</thead>
<tbody>
<tr class="even pointer" id="tablerow">
<td >1</td>
<td >Jim</td>
<td >50000</td>
<td >active</td>
</tr>
<tr class="even pointer" id="tablerow">
<td >2</td>
<td >Sally</td>
<td >50000</td>
<td >inactive</td>
</tr>
</tbody>
</table>

Why Xpath 3.0 works, but Xquery 3.0 doesn't work with the same expression

I launched Xpath in Oxygen. In Xpath 3.0 found what i need but in Xquery 3.0 doesn't find.
This is my Xpath expression
//table[tbody/tr/th/p[contains(text(), 'All Water System Contacts')]]/tbody/tr[3]/td[1]
This is my xml code
I put part code.
<table border="1" cellpadding="1" cellspacing="1" summary="." width="640">
<tbody>
<tr>
<th colspan="3">
<p>All Water System Contacts </p></th>
</tr>
<tr>
<th>Type</th>
<th>Contact</th>
<th>Communication</th>
</tr>
<tr>
<td align="center">AC - Administrative Contact - GENERAL MANAGER </td>
<td align="center">GRANT, JOHN, W <br/> PO BOX 869<br/> BIG SPRING, TX 79721-0869 </td>
<td align="center">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
width="100%">
<tbody>
<tr>
<th><b>Electronic Type</b></th>
<th><b>Value</b></th>
</tr>
</tbody>
</table>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
width="100%">
<tbody>
<tr>
<th><b>Phone Type</b></th>
<th><b>Value</b></th>
</tr>
<tr>
<td align="center">BUS - Business</td>
<td align="center">432-267-6341 </td>
</tr>
<tr>
<td align="center">FAX - Facsimile</td>
<td align="center">432-267-3121 </td>
</tr>
<tr>
<td align="center">BUS - Business</td>
<td align="center">432-267-6070 </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center">OW - Owner </td>
<td align="center">COLORADO RIVER MUNICIPAL WATER DISTRICT <br/> PO BOX 869<br/> BIG
SPRING, TX 79721-0869 </td>
<td align="center"> </td>
</tr>
</tbody>
</table>
I tried different functions.
I don't know why it doesn't work and what difference
Please help me.
I suspect your real, complete input has an XHTML default namespace declaration xmlns="http://www.w3.org/1999/xhtml" and in oXygen for XPath you have the setting enabled to "use the default namespace of the root element" so your path works with XPath out of the box while for XQuery you need to make sure you explicitly set
declare default element namespace 'http://www.w3.org/1999/xhtml';
in the prolog of your XQuery file or code sample.

Unable to select column with its header through XPath

My HTML
<table id="flex1" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr class="hDiv">
<th width="6%">
<div class="text-left field-sorting asc" rel="IFSC_CODE"> IFSC CODE </div>
</th>
<th width="6%">
<div class="text-left field-sorting " rel="BRANCH_NAME"> BRANCH NAME </div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="sorted" width="6%">
<div class="text-left">SACS011151</div>
</td>
<td width="6%">
<div class="text-left">check</div>
</td>
</tr>
<tr class="erow">
<td class="sorted" width="6%">
<div class="text-left">SACS011152</div>
</td>
<td width="6%">
<div class="text-left">Motiram</div>
</td>
</tr>
<tr class="erow">
<td class="sorted" width="6%">
<div class="text-left">SACS011158</div>
</td>
<td width="6%">
<div class="text-left">TESTNAME</div>
</td>
</tr>
</tbody>
</table>
My XPath
//table/tbody/tr/td[count(//table/thead/tr/th[.='BRANCH NAME']/preceding-sibling::th)+4]
Above XPath is Selecting all the column but not selecting its header name 'BRANCH NAME' and I want to select the header name with all its column.Any Idea how to do this?
You can simply use xpath union operator (|) to combine two xpath queries, for example* :
//table/tbody/tr/td[count(//table/thead/tr/th[.='BRANCH NAME']/preceding-sibling::th)+4]
|
//table/thead/tr/th[.='BRANCH NAME']
*: formatted into multiple lines just to make it visible without horizontal scroll

Optimal XPath Query for processing the sample HTML fragment

I have a feed that outputs HTML. The following segment is part of the output
<div class="leftnav">
<table border="0" cols="2">
<tr>
<td colspan="2" class="topline"><span style="font-size: 1px"> </span></td>
</tr>
<tr>
<td colspan="2"><span class="bold">Article Cat1 </span></td>
</tr>
<tr>
<td class="date" colspan="2">
ArticleTitle1</td>
</tr>
<tr>
<td width="20"></td>
<td class="date">
ArticleLink1
</td>
</tr>
<tr>
<td colspan="2" class="topline"><span style="font-size: 1px"> </span></td>
</tr>
<tr>
<td colspan="2"><span class="bold">Article Cat2 </span></td>
</tr>
<tr>
<td class="date" colspan="2">
ArticleTitle2</td>
</tr>
<tr>
<td width="20"></td>
<td class="date">
ArticleLink2
</td>
</tr>
</table>
</div>
I want to process above segment using XPATH so that output looks like this
Article Cat1
ArticleTitle1
ArticleLink1 Article Cat2
ArticleTitle2
ArticleLink2
What is the optimal XPATH that will produce the desired output? I tried //div[#class="leftnav"]/table/tr but this gives all the TR elements. I want to skip the first TR element so that I can get the output in the format I described above.
//div[#class="leftnav"]/table/tr[position() > 1]
Try the above
Stupid simple way:
substring-after(normalize-space(string(//*:div)), normalize-space(string(//*:div/*:table/*[1])))
Result: "Article Cat1 ArticleTitle1 ArticleLink1 nbsp Article Cat2 ArticleTitle2 ArticleLink2"
I don't know why, but (position() > 1) doesn't work in my environment, so I've used strings instead.

Trying to find XPath for multiple TDs

I want to extract the Address for specific Numbers (the first TD) of this table. The only unique identifier for the table is the H3.
Here is the code for the table:
<table width="95%" cellpadding=5 cellspacing=0 border=1>
<tr><td colspan="4"><h3>The list</td></tr>
<tr>
<td>Number</td><td>First Name</td>
<td>Last Name</td><td>Address</td>
</tr>
I have tried:
//table[#h3=’See this now’]/’tr/td[87] and td[107] and td[116]
I am new to xpath, and programming in general. It's pretty fun, but would love to be able to figure this one out!! Appreciate any help :D
First, your HTML is wrong.
You did not close your Table element.
You did not close your H3 element.
You must enclose your attributes in quotes.
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h3>The list</h3>
</td>
</tr>
<tr>
<td>Number</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
Once you have fixed the formatting of your XHTML. You can traverse the document tree.
XPATH
Any table, with any td that has a h3.
//table//td/h3
Will return
<h3>The list</h3>
For the number
//table//tr[2]/td[1] <-- any table, the second tr element in this table, the first td in that second tr
Will return
<td>Number</td>
So if we add multiple tables to a document and you want to find multiple results for each element in any table, this is quite simple. Say we have a XHTML document with many tables inside a parent element, for example 'root' element.
<root>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h3>The list</h3>
</td>
</tr>
<tr>
<td>123</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h3>The list</h3>
</td>
</tr>
<tr>
<td>456</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h3>The list</h3>
</td>
</tr>
<tr>
<td>789</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
</root>
We can extract the number of the first table data in each second row in every table using the following XPATH expression:
//table/tr[2]/td[1]
This will give us the result of
<td>123</td>
-----------------------
<td>456</td>
-----------------------
<td>789</td>
Now, say we have several tables, but only one table is very important to us, the table must have a H3 element, no other element is important to us, and if this table has a H3 element, we want to extract the second rows first td.
<root>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h4>Ignore me!</h4>
</td>
</tr>
<tr>
<td>1164961564896</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h1>I'm not interesting</h1>
</td>
</tr>
<tr>
<td>456456466465</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
<table width="95%" cellpadding="5" cellspacing="0" border="1">
<tr>
<td colspan="4">
<h3>IM THE IMPORTANT TABLE!</h3>
</td>
</tr>
<tr>
<td>123456789</td>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
</tr>
</table>
</root>
We can acomplish this by traversing back up the tree if we are successful in finding the H3 element, then go to the next tr.
//table//h3/../../../tr/td[1]
Will return
<td colspan="4">
<h3>IM THE IMPORTANT TABLE!</h3>
</td>
-----------------------
<td>123456789</td>

Resources