我想通过以下两种方式之一重置 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() 函数来侦听响应,如果它捕捉到年份,它将不返回任何内容。现在这可行,但显然我的月份列表不包含任何月份。然后我必须关闭属性框并重新打开它以列出月份。
关于如何实现此功能有什么想法吗?谢谢你。