auto sum of the values in the same row
auto sum of the values in the same row
Ghost108
Posts: 19Questions: 7Answers: 0
Hi !
this is my table:
<table id="tablePositionen" class="table-responsive table table-lg table-striped">
<tbody>
<tr>
<td><input type="text" name="menge" value="1.00" /></td>
<td><input type="text" name="preis" value="10.00 €" /></td>
<td>SUM</td>
</tr>
<tr>
<td><input type="text" name="menge" value="2.00" /></td>
<td><input type="text" name="preis" value="20.00 €" /></td>
<td>SUM</td>
</tr>
</tbody>
</table>
var tablePositionen = $('#tablePositionen').DataTable({
"paging": false,
"lengthMenu": [ [-1], ["All"] ]
});
Is there a way that the last column is the sum of "menge" and "preis" ?
It should also be possible that the sum will be dynamical recalculate if I change the value of "menge" and / or "preis".
Have you a solution for that?
Answers
You can use
columns.render
to sum the columns in the row. See this example.You will need to have Datatables update its data cache when the input is changed. See this example from this thread that shows how to use
cell().invalidate()
for this. The=api draw()
should cause Datatables to re-render that row and update the sum.Kevin
I tried this:
Output will be:
<td><input type="text" name="menge" value="1.00" /></td>
But how can I get the value ?
To get the node of a particular cell use
cell().node()
. Then use jQuery to perform actions with the cell, like getting the input value. Look at the examples in the docs. You would use something like this to get the cell node of the first column:Kevin
Hi,
at first: Thank you for the support !
But I have my problems to realize it.
Here my full code:
**JS **
This is my actual situation.
As you know, I would like to realize an auto sum of column "menge" and "preis" If the values of this input fields changed.
Can you please show me how?
Thank you !!
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
Sry for that!
Here my case:
http://live.datatables.net/gefonuto/1/
I see, thanks. Yep, Kevin's explanation is the way to go - use
cells().node()
to get the nodes, for examplecells('*', 2)
will return all the nodes for column two. You would then iterate through that list, and sum the values useval()
on each node,Colin