populate table column with different data source
populate table column with different data source
Hi,
I am returning an extra variable from the serverside using rawsql query using:
$lastyear = "SELECT * FROM agreement WHERE YEAR(start_date) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))";
$data = $db->sql( $lastyear)->fetchAll();
$json['lastyear_data'] = $db->sql( $lastyear )->fetchAll();
on the clientside I am receiving the data like this:
ajax: {
url: "terms_fetch.php",
"dataSrc": function ( json, data ) {
var lastyear = json.lastyear_data;
var thisyear = json.data;
return json.data
return json.lastyear_data
}
},
I am trying to populatethe last columns of the table with the results from rawsql query json.lastyear_data
something like this:
columns [
{data: "m1"} ,
{data: "m2"} ,
{data: "m3"} ,
{lastyear_data: "m1"},
]
The aim here is to display the finical status from current year (month1_month2....) and the retrieve the same data from last year and compare two columns.
Kind Regards,
KT
Answers
The first issue is that you have two returns in your
dataSrc
function - so line 9 won't be reached.The second issue is that the
lastyear_data
incolumns
won't work, as it's not a supported field.Could you post some of the data here so we can see what the server is returning, please.
Colin
@colin Thank you
This is how the returned data from the server looks like:
it returns two objects data and lastyear_data
Thank you
There is nothing built in to do what you want. You will need to package the data and the
lastyear_data
together to build the row data. Some options:ajax.dataSrc
copy thelastyear_data
object into a global variable that you can access incolumns.render
to for the column data.lastyear_data
so that each row/record has both sets of data.lastyear_data
.ajax.dataSrc
loop through the data to add the appropriatelastyear_data
.Kevin
Can you give an example of how a record should look, please? There are two
m1
fields, but only onem1
column - so how would you want them to look?Colin
@colin I am sorry If I was not clear, I will try to explain as much as I can.
if you see the image attached, I am populating the table like a normal using
data object
like this https://editor.datatables.net/examples/simple/server-side-processing.htmlNow the last column in the table (Total L/Y-Last Year), I want to populate this particular column with the data from same sql table data but with the data from last year (2020).
Total T/Y(this year 2021) = sum of months (m1+m2+m3+m4+m5...m12)
The last column should be like this:
Total L/Y(last year 2020) = sum of months (m1+m2+m3+m4+m5...m12)
For that case , I am querying the same sql table to get data sales (m1+m1...m12) from last year from serverside whichg returns two object data in JSON response.
The sql query loos like this:
And the JSON response contains two objects one is data and other is lastyear_data
What I am trying to achieve is to use the lastyear_data object and populate the Total L/Y column with that data.
The JSON response looks like this ( data for just one row as I have removed some data to make it smaller):
{
"data": [{
Also, what @kthorngren suggested, I used to implement option:!
not sure if the approach is right as it is now working, i can get the required values console.log(lastyear); but not working as the whole column is populated with the just first value(i=0)
Thank you
Also figured out that it can be done using SQL sum query, just wondering if there is an easy way around.
just an example :
Your
lastyear_data
is an array of objects. Likely you won't want to loop through the array for each row incolumns.render
. A more efficient option is to loop through the array once inajax.dataSrc
and convert it to object structured data with the key being field that ties this to thedata
. For example, assumingagreement_return_id
is the field:This way you can just access the correct
lastyear_data
directly by using something likelastyear_data[ row.agreement_id_fk ]
. I'm making assumption sbout your data and what you are trying to do with it. The point of this is to useajax.dataSrc
loop the data once instead of looping it for each row incolumns.render
.However its probably more efficient to use a SQL query to fetch the data.
Kevin
@kthorngren
Thank you.
I believe it is efficient to use sql query to fetch the data and most probably I will use it.
However, I have implemented the logic like this inside the dataSr function but cant figure the way to use this returned data lastyear inside column :
I can access the data using console.log(lastyear); which gives me required data
Thank you in advance
Is there something in the row data that is a key that points to the data you want from lastyear?
Kevin
@kthorngren
If I got it correctly, in the row.data, the supplier is the key that points to the data in last year.
This means, from the lastyear I would like to get the data for (m1,m2,m3...) where the supplier in lastyear is the same as key, supplier in row data.
Thank you