【问题标题】:OctoberCMS Reset ReportWidget Property(ies)OctoberCMS 重置 ReportWidget 属性
【发布时间】:2018-06-28 04:01:45
【问题描述】:

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

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

【问题讨论】:

    标签: widget octobercms octobercms-plugins octobercms-backend octobercms-widgets


    【解决方案1】:

    嗯,我已经重写了你的组件,现在它似乎工作了

    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');
        }
    }
    

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

    【讨论】:

    • 我认为我们的方向是正确的。我对其进行了更改,因此它在 init() 函数中将月份和季度设置为 null。现在,每当我更改年份时,我都需要将月份和季度设置为空,但仍然有可用的列表。
    • 但你说If a user changes the year, and then perhaps specifies a quarter or month for that year, the report displays accordingly. - 所以如果列表为空,那么用户如何选择季度或月份
    • 你能更好地分享你需要的示例数据(组合和案例),这样我们就可以这样做
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2011-01-07
    • 2017-12-10
    • 2013-12-17
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多