十月 CMS 重置报告小组件属性



我想通过以下两种方式之一重置我的 ReportWidget 的属性:

1) 刷新包含报告小部件的页面时

2) 某些属性发生更改时

我的报表小部件有 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() 函数来侦听响应,如果它捕获了年份,它将不返回任何内容。现在这有效,但显然我的月份列表不包含任何月份。然后,我必须关闭属性框并重新打开它以列出月份。

关于如何实现此功能,有什么想法吗?谢谢。

嗯,我已经重写了您的组件,在这里它似乎可以工作

init()我们可以根据需要将所有值重置为默认值,之后pageload用户可以根据自己的要求进行选择,并且小部件使用post requests values来计算内容并根据该值工作。 但是现在,当用户refresh page值将从init()设置为默认值并根据默认值进行计算时,no post values

试试这个,

<?php
namespace OctoberDemoReportWidgets;
use BackendClassesReportWidgetBase;
use RainLabUserModelsUser;
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');
}
}

如果您有任何问题,请发表评论。

最新更新