Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
You can extend the Text widget and query under onInit event, then set the text() value:
class MyText extends \koolreport\dashboard\widgets\Text
{
protected function onInit()
{
$dataStore = AutoMaker::table("select something")->run(); //Run query to get data
//Now you get a datastore, you can get value from it something like this:
$value = $dataStore->get(0,"columnName"); //get columnName at first row
$this->text($value);
}
}
then you register the MyText in your dashboard.
Hope that helps.
Thank you, is it possible to add a filter in there?
class DateRange extends DateRangePicker
{
protected function actionChange($request,$response)
{
$this->sibling("MyText")->update();
}
}
// MyText.php
class MyText extends \koolreport\dashboard\widgets\Text
{
protected function onInit()
{
//Get value from the date range picker
$range = $this->sibling("DateRange")->value();
$dataStore = AutoMaker::table("select something")->whereBetween("date",$range)->run(); //Run query to get data
//Now you get a datastore, you can get value from it something like this:
$value = $dataStore->get(0,"columnName"); //get columnName at first row
$this->text($value);
}
}
Nice question, actually it is not possible to use onInit
event because onInit
the DateRange value may not be available. So better we use a later event which is onRendering
, this event happen just before text is rendered.
protected function onRendering()
{
//Get value from the date range picker
$range = $this->sibling("DateRange")->value();
$dataStore = AutoMaker::table("select something")->whereBetween("date",$range)->run(); //Run query to get data
//Now you get a datastore, you can get value from it something like this:
$value = $dataStore->get(0,"columnName"); //get columnName at first row
$this->text($value);
return true; //<---This is important to allow the text to be rendered
}
Remember to return true;
at the end of onRendering
event.
Let me know if it works.
When the report first render with the default value (defaultValue($this::last30days())) the value is populated in MyText. However when filtering using the DateRangePicker getting below error:
Message: Call to a member function update() on null
Line: 19
File: C:\...\Filters\DateRange.php
Collapse
#0: C:\...\vendor\koolreport\dashboard\TAction.php Line 35 : actionChange([{},{}])
(path hidden)
Hi! I don't want to hijack this thread, but I'm looking to do the exact same thing, but i'm not sure how to execute your last comment on adding the MyText to the dashboard. Could you please show a complete example? Here's my code. Note that i'm using RAW SQL.
Here's my code :
SalesBoard.php
<?php
use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use \koolreport\dashboard\containers\Html;
class SalesBoard extends Dashboard
{SalesBoard
protected function widgets()
{
return [
Row::create()->sub([
Row::create(),
SalesDateRange::create()->width(1/3),
]),
Row::create()->sub([
Panel::create()->sub([
SalesSalesMetric::create(),
])->width(1/3),
]),
Row::create()->sub([
Panel::create()->sub([
SalesByDate::create(),
])->width(1/1),
]),
Row::create([
Panel::create()->width(1/1)
->sub([ Html::h2("Daily Sales Details") ])
->cssClass("trans-row")
]),
SalesTable::create(),
];
}
}
SalesSalesMetric.php
<?php
use \koolreport\dashboard\metrics\Value;
use \koolreport\dashboard\fields\Date;
use \koolreport\dashboard\fields\Text;
use \koolreport\dashboard\fields\Currency;
use \koolreport\dashboard\containers\Panel;
class SalesSalesMetric extends Text
{
protected function onRendering()
{
//Get value from the date range picker
$range = $this->sibling("SalesDateRange")->value();
$dataStore = Incognito::rawSQL("
select *,
cartDiscount + cartDiscountTax AS orderDiscount,
orderTotal - orderTax - orderShipping - orderShippingTax - cartDiscount - cartDiscountTax - refundAmount AS orderNet
FROM viuInvoicesRefundsByDate
WHERE transactionDateTimestamp between '" . $range[0] . "' and '" . $range[1] . "'
");
//Now you get a datastore, you can get value from it something like this:
$value = $dataStore->get(0,"orderNet"); //get columnName at first row
$this->text($value);
return true; //<---This is important to allow the text to be rendered
}
}
Mathieu, Just put in inside the widgets()
function:
<?php
use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;
use \koolreport\dashboard\containers\Html;
class SalesBoard extends Dashboard
{
protected function widgets()
{
return [
MyText::create("MyText"), //<-- Like this
Row::create()->sub([
Row::create(),
SalesDateRange::create()->width(1/3),
]),
Row::create()->sub([
Panel::create()->sub([
SalesSalesMetric::create(),
])->width(1/3),
]),
Row::create()->sub([
Panel::create()->sub([
SalesByDate::create(),
])->width(1/1),
]),
Row::create([
Panel::create()->width(1/1)
->sub([ Html::h2("Daily Sales Details") ])
->cssClass("trans-row")
]),
SalesTable::create(),
];
}
}
Hope that helps.
Tip: if you find a topic is related to your, you can create new topic of your own and put reference link to that topic, and in your topic, you are freely to describe your own issue, we can give more precise answer to your case.
OK i'm getting Message: Call to a member function state() on null Line: 20 File: /*path/reports/koolreport/dashboard/TWidgetState.php
Is it because how i select the value in $value = $dataStore->get(0,"orderNet"); ??
And thanks for the tip i'll start a new topic next time.
Sorry for the dots which cause misunderstanding, I was supposed to answer your another post but wrongly post here, I was not able to delete the post so I changed it to "...", meaning no content. Sorry for that. So the error still persist?. I have not been able to guess what went wrong. May be you can start a new topic with more detail explanation, including screenshot of the error and list of code.
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo