cakephp:从下拉列表中为报告添加过滤器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cakephp:从下拉列表中为报告添加过滤器相关的知识,希望对你有一定的参考价值。

我有一个生成视图和excel文件的报告。我很想在两者中显示它的所有行。用户需要根据日期字段“end_date”过滤数据。在我看来,我把它说:

<?php
            $options = array();
            $options[0] = 'All';
            $options[1] = 'Due last 6 months';
            $options[2] = 'Due current month';
            $options[3] = 'Due next 3 months';
            $options[4] = 'Due next 6 months';
            $options[5] = 'Due next 12 months';            
        ?>

        <div class="row">
            <?= $this->Form->create() ?>
                <fieldset>                    
                    <div class="col-sm-2"><?= $this->Form->input('select_period', array('options' => $options)); ?></div>
                    <div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button('Search', ['type' => 'submit', 'class' => 'btn btn-primary']); ?></div>
                </fieldset>
            <?= $this->Form->end() ?>
        </div>

在我的控制器中,我使用此功能:

public function reportReplacement()
{
    $assetsAssignations = $this->AssetsAssignations->find()
        ->contain([
            'AssetStatuses',
            'Assets' => [
                'AssetTypes'
                ],
            'Clients' => [
                'ClientTypes'
                ],
        ])
        ->order([
                 'AssetTypes.name' => 'asc',
         ]);
    $filter = $this->Filter->prg($assetsAssignations);
    $_filename = "xls_report_replacement_" . date('Ymd');
    $this->set(compact('assetsAssignations', '_filename'));
}     

我试着将它放在我的控制器初始化中,但它不起作用:

if ($this->request->param('action') == 'reportReplacement') {
        $select_period = $this->request->query('select_period'); // I want to get the selected [0], [1], etc..  when I debug, I always get null.

我也没能找到我必须在我的查询中将选项作为条件的地方,例如:

如果选项[1],则'end_date'> =(TODAY-6个月)和'end_date'<= TODAY;

如果选项[2],则'end_date'= TODAY(月);

如果选项[1],则'end_date'> = TODAY AND'end_date'<=(TODAY + 3个月);

等等

有什么帮助吗?

答案

感谢Greg,我找到了解决方案:

在视图中:

        <?php
            $options = array();
            $options[0] = 'Due last 6 months';
            $options[1] = 'Due current month';
            $options[2] = 'Due next 3 months';
            $options[3] = 'Due next 6 months';
            $options[4] = 'Due next 12 months';            
        ?>

        <div class="row">
            <?= $this->Form->create() ?>
                <fieldset>                
                    <div class="row">
                        <div class="col-xs-3"><?= $this->Form->input('select_period_id', ['options' => $options, 'empty' => true, 'label' => __('Select Period')]) ?></div>
                        <div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button(__('Submit')) ?></div>
                    </div>
                </fieldset>
            <?= $this->Form->end() ?>

        </div>

在控制器中:

use CakeI18nTime;

并在功能:

    $data = $this->request->data;
    $select_period = $this->request->data('select_period_id');
    $today = Time::now()->format('Y-m-d');
    $second_date = Time::now();
    $assetsAssignations = $this->AssetsAssignations->find()
        ->contain(['Assets']);

    if($this->request->is(['patch', 'post', 'put'])) 
    {
        if ($select_period == 0) {                
            $second_date = $second_date->modify('-6 months');                
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $second_date,
                'end_date <=' => $today
                    ]);
        } elseif ($select_period == 1) {
            $second_date = $second_date->modify('1 month');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);

        } elseif ($select_period == 2) {
            $second_date = $second_date->modify('3 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        } elseif ($select_period == 3) {
            $second_date = $second_date->modify('6 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        } elseif ($select_period == 4) {
            $second_date = $second_date->modify('12 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        }            
    }

    $this->set(compact('assetsAssignations'));
} 

以上是关于cakephp:从下拉列表中为报告添加过滤器的主要内容,如果未能解决你的问题,请参考以下文章

C#中为DataGrid添加下拉列表框(转载)

在asp.net的html表中添加下拉列表

如何在 s-s-rS 中创建下拉过滤器列表?

如何在 C# 中为枚举值添加描述以与 ASP.NET MVC 中的下拉列表一起使用? [复制]

在 CakePHP 3 中为文章添加评论

如何在 ASP.NET MVC 3 中为填充的下拉列表创建视图模型