Calculating cells values
Calculating cells values
Hi Guys I have 2 datatables
dtInvoices & dtInvoiceDetails
dtInvoice has the following columns
date - orderNo - net - vat -Total
I have 2 methods of editing dtInvoiceDetails inline & the editor window.
In this table I have columns
quant - sell - nett - lineTotal - vatRate
What is the event so I can calculate the lineTotal when the quant/ sell amounts change ( either by inline or the editor window)
I also have need the sum of lineTotal to add to the dtInvoice table.
I have this so far.
On the dtInvoice click
$('#dtInvoices').on( 'click', 'tbody td', function () {
//alert( tableInvoices.cell( this ).data() )
currentSelectedInvoiceIndex = tableInvoices.row( this ).index() ;
alert(currentSelectedInvoiceIndex);
} );
The invoice total is column 2 so I can set the sum to cell(currentSelectedInvoiceIndex,2)
I can't find an example showing this.
So I need
- Get the onChange event of a row/cell and calculate the line total
- Then calculate the sum of the column lineTotal
- Set the total in dtInvoices.
Cheers
Steve Warby
This question has an accepted answers - jump to answer
Answers
Use a renderer. That will perform the calculation every time the data is requested by DataTables.
Allan
Hi Allan,
This works.
but I have a column from the database 'inv_list.lineTotal' so i want to update this column with the calculated amount.
What is the correct syntax for this please.
I also need to trigger a sum of the column 'inv_list.lineTotal' and update the invoice datatable
The invoice total is column 2 so I can set the sum to cell(currentSelectedInvoiceIndex,2)
What is the syntax for this please?
Cheers
Steve Warby
As in you want to update the total in the database as well? Three options:
dependent()
to update the values based on inputs in other fields.Field->setValue()
in a server-side event -preCreate
andpreEdit
.Personally I'd use option three - it is a derived value, so you don't need to have it stored in the database (unless you want to be able to search and sort on it).
Allan
The data is stored in the database.
I have
The calculation does not fire first time. The second time it shows the first result.
What am I not understanding here ?
Cheers
Steve Warby
Not sure - I'm missing something as well. Can you give me a link to the page showing the issue?
Allan
sent private message
Hi Allan,
a screen grab of what is happening.
www.toolfolks.com/docs/dependencyProblem.mov
How do I update the server side.
I have this working
So this updates tableInvoices with what I need.
But the data is not posted to the database. If I click edit then update it works.
What is the syntax for update tableInvoices to server?
Cheers
Steve Warby
DataTables itself won't submit that change to the server. You have two options:
I would argue though that since those values are computed, they shouldn't really be saved on the server since they can just be recomputed when needed. Storing them is storing redundant information.
Allan
Whats the syntax to do the editor request.
I have an editor created.
tableInvoices - goto edit mode - exit edit mode and post.
The database is already existing and other programmes need the calculated columns.
Cheers
Steve Warby
Eventually got there....
editorInvoices.edit( currentSelectedInvoiceIndex, false ).submit();
Cheers
Steve Warby