KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

How to json_encode run()->render() #664

Open Andrew Borell opened this topic on on Feb 11, 2019 - 2 comments

Andrew Borell commented on Feb 11, 2019

I had a situation where I wanted to receive the rendered page via ajax by first checking a jwt auth token, then user rights before turning the report around. Having the ability to direct the assets to a public directory while keeping my other pages private is really nice, but the only way I see documented to turn around JSON is by sending back the datastore('some_ds_name')->toJson, which I did not find to be particularly useful. The underlying problem is if my headers say I am sending back json data, but the rendered page is echoed or printed without being encoded, then despite my xhr request being status 200 or 201 my xhr is now invalid.

As a workaround to this situation, I retained the ability to utilize the built-in view rendering using the myreport->run()->render() by using the output buffer to suppress the automatic output when a page is rendered. This is similar to how other output options are handled, but I wanted to throw this out there for anyone else who might find it useful.

	ob_start();
	$someVar = new someClass; 
	$someVar->render()->run();	
	$data = ob_get_contents();
	ob_end_clean();

	$data = json_encode($data);

Now you can send back the data and use JSON.stringify on the data key client-side.

	$response = json_encode(
		array(
			"jwt" => $jwt,
			"data" => $data
		)
	);
		
	echo $response;	

I havent tested all features with this approach but it works for my purposes.

KoolReport commented on Feb 12, 2019

Thank you very much for your sharing!

You may do it this way as well:

$report = new MyReport;

$report->run();

$response = json_encode(
    array(
        "jwt" => $jwt,
        "data" => array(
            "saledata"=>$report->dataStore("saledata")->data(),
            "other_datastore"=>$report->dataStore("other_datastore")->data()
        )
    )
);
		
echo $response;
Andrew Borell commented on Feb 12, 2019

Ill check both ways out. I was thinking this JSON approach would break drilldown reports probably but my intention is to confirm user rights for each report when it is requested; Thus I am chaining reports in a more manual fashion so these permissions can exist.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None