We have updated WebDataRocks EULA, effective as of April 18, 2024. Learn more about what's changed.

How to get the JSON data form back-end (PHP) instead of tool bar

Answered

Hi Webdatarocks,
I like this tool, it’s helpful.
But I meet a problem on get JSON data from PHP. I am using tool bar to select file that output from my back-end program, how can I make a link between pivot table and my PHP program, that I can pass my data directly?
If I place the pivot table code in my php file.

<?php ....
$data = json_encode($result_final);
?>
<div id="wdr-component"></div>
<link href="https://cdn.webdatarocks.com/1.0.0/webdatarocks.min.css" rel="stylesheet"/>
<script src="https://cdn.webdatarocks.com/1.0.0/webdatarocks.toolbar.min.js"></script>
<script src="https://cdn.webdatarocks.com/1.0.0/webdatarocks.js"></script>
<script>
var pivot = new WebDataRocks({
container: "#wdr-component",
toolbar: true,
report: {
dataSource: {
data: {{$data}}; //problem here.
}
}
});
</script>
</body>
</html>

7 answers

WebDataRocks Team WebDataRocks September 27, 2019

Hello Jacinth,
 
Thank you for the question.
We have the following recommendations:

  • Please check that the JSON is in supported format, it should be Array of Objects
  • Also, you can try the following syntax to pass JSON from PHP to JavaScript
report: {
dataSource: {
data: <?= $data ?>
}
}

// or

report: {
dataSource: {
data: JSON.parse("<?= $data ?>");
}
}

 
Hope it helps.
 
Regards,
WebDataRocks Team

Jacinth September 30, 2019

Hi WebDataRocks Team,
thanks for your reply, but it seems not works on me. I use the original way.
By the way, how can I execute setReport()/getReport() automatically instead of click button after refresh web page?

<script>
var pivot = new WebDataRocks({
container: "#wdr-component",
toolbar: true,
report: "https://localhost/report.json"
report: {
dataSource: {
dataSourceType: "json",
filename: "http://localhost/data.json",
}
}
});
</script>

 

WebDataRocks Team WebDataRocks October 3, 2019

Hello Jacinth,
We recommend trying events of WebDataRocks. It seems that reportcomplete should work fine for your case: https://www.webdatarocks.com/doc/reportcomplete/.
Please note that you should sign off the events at the right time in order to avoid dead loops: https://www.webdatarocks.com/doc/off/.
Regards,
WebDataRocks

Jacinth October 7, 2019

Hi WebDataRocks Team,
thanks for your reply, but my problem is I can’t load the “report.json” file automatically.
I have to click the “tool bar”->”open”->”local report”->choose  the “report.json” file at local path.
How can I do or something wrong in my code that I did noticed?

WebDataRocks Team WebDataRocks October 8, 2019

Hello Jacinth,
You can define the report by passing the correct url. Here is the example: 

report: "url_to_your_file"

As for your code sample, your url lacks the port. That could be the reason why it cannot be accessed. If you are using PHP, you need to use the port where your apache server is launched. Here is the possible example: 

report: "http://localhost:8080/data.json"

Regards,
WebDataRocks Team

Jacinth October 14, 2019

Hi WebDataRocks Team,
thanks for your reply, I decoded the report.json file and add content into code directly, it work functional now.
By the way, how to merge the expand index into one row? That can reduce the waste of layout space (as following pic.)

Attachments:
merge.PNG

WebDataRocks Team WebDataRocks October 15, 2019

Hello Jacinth,
Thank you for your feedback.
We glad that you have managed to resolve the case.
As for merging the expands, there are no options to change the template of the expands representation.
Regards,
WebDataRocks Team