我想重置ReportWidget的属性有两种方法:
1)刷新包含报告小部件的页面时
或
2)当某些属性发生变化时
我的ReportWidget有3个属性:年,季,月
默认情况下,我显示当前年份的信息。如果用户更改年份,然后可能指定该年度的季度或月份,则报告会相应显示。
现在,当他们返回页面时,会保存这些设置。我想要做的是在重新加载或刷新页面时将属性重置为默认值。
此外,如果用户每年更改一次,则其他属性应重置。
我试图用这段代码解决这个问题:
public function defineProperties() {
return [
'year' => [
'title' => 'Year',
'default' => $this->getDefaultYear(),
'group' => 'Date',
'type' => 'dropdown',
'options' => $this->getYearOptions(),
],
'quarter' => [
'title' => 'Quarter',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getQuarterOptions(),
'depends' => ['month']
],
'month' => [
'title' => 'Month',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getMonthOptions(),
'depends' => ['year']
]
];
}
public function getYearOptions() {
$query = User::all();
$years = [];
foreach($query as $user) {
$year = date('Y', strtotime($user->created_at));
$years[$year] = $year;
}
$years = array_unique($years);
return $years;
}
public function getQuarterOptions() {
$monthCode = Request::input('month'); // Load the year property value from POST
$yearCode = Request::input('year');
if ($yearCode && $monthCode) {
return;
}
if ($yearCode) {
return [
1 => '1st',
2 => '2nd',
3 => '3rd',
4 => '4th'
];
}
}
public function getMonthOptions() {
$yearCode = Request::input('year'); // Load the year property value from POST
if ($yearCode) {
return;
}
$months = [];
for ($m=1; $m<=12; $m++) {
$month = date('m', mktime(0,0,0,$m, 1, date('Y')));
$months[$month] = date('M', mktime(0,0,0,$m, 1, date('Y')));
}
return $months;
}
所以这里发生的是,如果年份发生变化,它将调用getMonthOptions()函数来监听响应,如果它捕获了年份,它将不返回任何内容。现在这可行,但显然我的月份列表不包含任何月份。然后我必须关闭属性框并重新打开它以列出月份。
对于如何实现此功能有任何想法吗?谢谢。
答案 0 :(得分:0)
嗯,我已经重写了你的组件,这里似乎正在工作
在
init()
中,我们可以根据需要将所有值重置为默认值,pageload
用户可以根据自己的要求选择并使用post requests values
来计算内容并根据需要进行操作。但现在再次将用户refresh page
值设置为默认值init()
,并将默认值计算为no post values
,
尝试这个,
<?php
namespace October\Demo\ReportWidgets;
use Backend\Classes\ReportWidgetBase;
use RainLab\User\Models\User;
use Request;
class TestWidget extends ReportWidgetBase
{
public function defineProperties() {
return [
'year' => [
'title' => 'Year',
'default' => $this->getDefaultYear(),
'group' => 'Date',
'type' => 'dropdown',
'options' => $this->getYearOptions(),
],
'month' => [
'title' => 'Month',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getMonthOptions(),
'depends' => ['year']
],
'quarter' => [
'title' => 'Quarter',
'type' => 'dropdown',
'group' => 'Date',
'options' => $this->getQuarterOptions(),
'depends' => ['month']
],
];
}
public function init() {
// it will set default values on every refresh
// we will not use $this->setProperties() as it will set
// multiple props at single time but it will remove column size etc props as well so
$this->setProperty('year' , $this->getDefaultYear());
$this->setProperty('month' , '01');
$this->setProperty('quarter' , '1st');
}
public function getDefaultYear() {
// may be dynamic current year (I become sloppy here and hard coded)
return '2018';
}
public function getYearOptions() {
$query = User::all();
$years = [];
foreach($query as $user) {
$year = date('Y', strtotime($user->created_at));
$years[$year] = $year;
}
$years = array_unique($years);
// hard-coded for testing as i don't have users so
// $years = ['2014', '2015', '2017', '2018', '2019'];
// $years = array_combine($years, $years);
return $years;
}
public function getMonthOptions() {
// Load the year property value from POST
$yearCode = Request::input('year');
// PRIORITY is user choise if he choose something this
// condition will be false and we dont use default property
// if he dosn't choose means condition true we can use default value
// (so it should be page refresh)
if(!$yearCode) {
// so if page is refreshed we use default value
// which we set in init() method
$yearCode = $this->property('year');
}
// based on $yearCode Calulate -> month
$months = [];
for ($m=1; $m<=12; $m++) {
$month = date('m', mktime(0,0,0,$m, 1, date('Y')));
$months[$month] = date('M', mktime(0,0,0,$m, 1, date('Y')));
}
return $months;
}
public function getQuarterOptions() {
// Load the month and year property value from POST
$monthCode = Request::input('month');
$yearCode = Request::input('year');
// PRIORITY is user choise if he choose something this
// condition will be false and we dont use default property
// if he dosn't choose means condition true we can use default value
// (so it should be page refresh)
if (!$yearCode && !$monthCode) {
// so if page is refreshed we use default value
// which we set in init() method
$yearCode = $this->property('year');
$monthCode = $this->property('month');
}
// now based on $yearCode and $month code calulate -> quarter
if ($yearCode) {
return [
1 => '1st',
2 => '2nd',
3 => '3rd',
4 => '4th'
];
}
}
public function render()
{
return $this->makePartial('widget');
}
}
如果您有任何问题,请发表评论。