Multiplication of two column and result in third column
Multiplication of two column and result in third column
jigar311982
Posts: 70Questions: 31Answers: 0
Hello,
I am using datatables in below steps,
- Table initialise with blank rows.
- Then I am using custom JS for adding rows with data pulled from the server.
- Data appeared in a row. works perfectly fine.
- Now, here one column have some numbers(say price) coming from the server, and the second column have a custom render of Touchspin(where you can increase value by pressing plus and minus icon),
- what I need to have a multiplication of Price and Number, and that value should appear in 3rd column.
- Just like excel, where one column with fix value and another column can change value and 3rd column should show multiplied resuts.
Is that possible with any script, API?
Thanks,
Jigar.
This question has accepted answers - jump to:
This discussion has been closed.
Answers
You can use
columns.render
, the third parameter is the data for all the row so you can use that in your sums,Colin
Hello Colin,
I need to get multiplication of two cell values and get in 3rd cell of the same row, how can i do that?
Take a look at this example using
columns.render
. It is adding two strings together but instead you can multiply two numeric columns for a numeric result.Kevin
Hello Kevin,
That works, but I have a cell with form input,
I need to get the value of this form input and then do multiplication, is that possible?
{
targets: 3,
width: "25%",
render: function(data, type, full, meta) {
return '\<input type="text" class="form-control touch" name="item_quantity" placeholder="Add Qunt" />';
},
},
In that case you will want to use
rowCallback
instead. You will need an event handler to redraw, usingdraw()
, the table when an input is changed. In therowCallback
function you will get the input value, perform the calculation then write it to the desired cell. Here is a simple example based on yourinput
.http://live.datatables.net/momabajo/1/edit
Kevin
Hello Kevin,
This works perfectly fine for existing data which either came from the server or local HTML data,
But when I add a row with the row.add(), it does not calculate.
My datatables have columns field defined,
and i am adding row with below Jquery,
Hi Kevin,
To get working in my code, need to change rowcallback data as data.item_price instead of data[2].
By the way, I have one more question,
How can we send this complete dataTable data back to the server where we have to render form input?
You would need to go through the
rows()
orcells()
, getting theval()
of thenode()
. It may be worth considering Editor, as that will keep the table and the server in sync.Colin
Is that possible to do sum at footer after multiplication values? means the total of total column change after change in multiplier? I can get a sum of default values only, but while changing multiplier, footer sum is not updating.
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
@colin , Can you please look at this link?
live.datatables.net/qenoguko/1/edit
See this thread asking the same question about totaling rendered columns.
Kevin
@kthorngren, I added below code in your link, but that does not do some for multiplied column result values, it stays at 0.
can you run this in your multiplication example url?
Please provide an updated test case link with what you are trying to do.
Kevin
@kthorngren , here is link,
I could able to get then sum of Age column, but I could not get the sum of Total column,
Total column cells will get change based on multiplier input and then I need a sum of Total column
live.datatables.net/fihowere/1/edit
It's because you're updating the HTML of the table with the total - not the table's data. DataTables uses cached information, so wouldn't know about the change.
I added a hack,
to force DataTables to reread the table, but the better solution would be to use
cell().data()
to update the table.Here's the updated example,
Colin
@colin , thanks for this,
Somehow my additional plugin for multipler(touchspin) is getting conflict with this,
Can you please rewrite this example with cell.data() instead of my html one?
Also can you help me to understand what is null and 4 in cells argument?
Updated the
rowCallback
with this:Removed the
this.api().cells(null, 4).invalidate();
that Colin used.http://live.datatables.net/fihowere/5/edit
Read the
cell()
docs and therow-selector
andcolumn-selector
docs for all the details. Thenull
is therow-selector
which the docs state "No selector - Select all rows" so this iterates all rows. The4
is the column number.Don't know what that is. If you think its in conflict please update the test case to show the issue.
Kevin
@kthorngren , this works perfectly fine....:-)
Thanks a lot for this super support. Appreciate.