Adding the values of column together to display to another column
Adding the values of column together to display to another column

{
"data": null,
"render": function(data,type,row) { return (data["valueA"] + data["valueB"])} //Try to add these value
},
When trying to use the "+" symbol, nothing happens and only show the valuesA and valueB like this (16.0016.00)
When using the (* and -) symbol, it works as intended. Was wondering if there was a way to add them together
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Not sure what you mean.
Looks like
data["valueA"]
anddata["valueB"]
are being treated as stirngs. You can convert them. Try this:return ( Number(data["valueA"] ) + Number(data["valueB"]) )
Kevin
I've tried the suggestion above. The ouput comes out as NaN .
In terms of symbol, this is what I mean. As seen in the screenshots below (Both valueA & valueB are 16). When trying to do minus & multiply, they output both as 256 & 0. But when trying to add them together, it just output as "16.0016.00"
Addition +
Minus -
Multiply *
I don't understand your examples. The code is the same in each of them.
If you are sure the data type is string, try parseInt().
https://www.w3schools.com/jsref/jsref_parseInt.asp
I've made a mistake in the above comment as the opeartors are not correct.
This is the code for the table, trying to get the sum of the 2 columns and output it in another column
This example shows what tangerine and I are talking about:
http://live.datatables.net/hekubina/1/edit
Both a and b are strings. Multiplying them works but adding them doesn't unless you convert them.
If you need help with this then please post a link to your page or a test case so we can help debug. Otherwise you can use
console.log
andtypeof
to debug the data types fordata["valueA"
anddata["valueB"
. Or use the browser's debugger.Kevin
What was your return statement when you got this result?
Kevin
This is the return statement. Its kinda difficult to show the program as it is reading from a DB, so you wouldn't be able to see the full program. Below is something similar to what I want, but, Value A & B are coming from a DB table. Is there a way to change the data type to number?
is what you want.
Allan
Yep that did the trick, thanks allan