分页获取所有记录而不在YII中过滤

时间:2014-05-07 06:33:04

标签: php yii

我是Yii的新手。我已经完成了正常的标准搜索,并在Yii的网格视图中渲染它们。如果我在搜索/过滤后点击第二页,它会再次在网格视图中为我提供整套记录。

我的观点:

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'nimsoft-alerts-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
)); ?>

<h1>Missing Hosts List</h1>
<?php //echo $form->errorSummary($model); ?>
<div style="float:left;">
    <div class="row">
    <?php echo $form->labelEx($model,'host_start_date'); ?>
    <?php
        Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
        $this->widget('CJuiDateTimePicker', array(
            'attribute' => 'host_start_date',
            'language' => '',
            'model' => $model,
            'options' => array(
                'mode' => 'focus',
                'dateFormat' => 'yy-mm-dd',
                //'minDate'=>'0',
                'showAnim' => 'slideDown',
            ),
            'htmlOptions' => array(
                'style'=>'height:20px;',
                'value' => $model->host_start_date,
            ),
        ));
    ?>
    <?php echo $form->error($model,'host_start_date'); ?>      
    </div>
</div>
<div style="float:left;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
<div style="float:left;">
    <div class="row">
        <?php echo $form->labelEx($model,'host_end_date'); ?>
        <?php
            Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
            $this->widget('CJuiDateTimePicker', array(
                'attribute' => 'host_end_date',
                'language' => '',
                'model' => $model,
                'options' => array(
                'mode' => 'focus',
                'dateFormat' => 'yy-mm-dd',
                //'minDate'=>'0',
                'showAnim' => 'slideDown',
                ),
                'htmlOptions' => array(
                    'style'=>'height:20px;',
                    'value' => $model->host_end_date, 
                ),
            ));
        ?>
        <?php echo $form->error($model,'host_end_date'); ?>      
    </div>
</div>
<div class="row buttons">
    <?php echo CHtml::button('Search',array('submit' => array('Site/index')));?>
    <?php echo CHtml::button('Search and Export',array('submit' => array('Site/Export')));?>
</div>


<?php $this->endWidget(); ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!--<div class="row buttons">  
    <a href="<?php //echo $this->createUrl('Site/Index',array('id'=>$cust_id,'isXLSDownload'=>1));?>" title="Export For All Customers" class="btn btn-primary circle_ok" style="text-decoration: none; color:#FF3333;" ><b>Export All</b></a>
 </div>-->
<?php 
    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'enableSorting' => false,
        'columns'=>array(
            array(            // display 'create_time' using an expression
                'name'=>'alert_device',
                'value'=>'$data->alert_device',
            ),
            array(            // display 'create_time' using an expression
                'name'=>'alert_event_time',
                'value'=>'$data->alert_event_time',
            ),
            array(            // display 'create_time' using an expression
                'name'=>'alert_datetime',
                'value'=>'$data->alert_datetime',
            ),
        ),
    //'itemView'=>'_view',
    ));     
?>
</div>

我的行动:

public function actionIndex()
{
    //$this->layout=false;
    $model=new NimsoftAlerts;
    if(isset($_POST['NimsoftAlerts']))
    {

        $model->attributes=$_POST['NimsoftAlerts'];
        if($model->validate())
        { 
            $pagination=1;
            $criteria = new CDbCriteria();
            $criteria->condition = "alert_datetime  >= '$model->host_start_date' and alert_datetime <= '$model->host_end_date' and alert_itsm_ack_status IS NULL";
            $details = NimsoftAlerts::model()->findAll($criteria);
            $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
                'criteria'   => $criteria,));
        }
        else $dataProvider=new CActiveDataProvider('NimsoftAlerts');
    //  if(isset($pagination))
    //  {
    //      $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
    //          'criteria'   => $criteria,));
    //  }
    }
    else
    { $dataProvider=new CActiveDataProvider('NimsoftAlerts'); }

    if($_REQUEST['isXLSDownload']=='1')
    {
        $xlsName='Missing_Host_Details_'.date('YmdHis').'.xls';
        $sheetName='Missing Host Details';
        $headerTxt='Host Details';
        $arrTh=array(
            'alert_device'=>array('label'=>'Alert Device'),
            'alert_event_time'=>array('label'=>'Alert Event Time'),
            'alert_datetime'=>array('label'=>'Alert Datetime'), 
        );
        $this->generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
        //GlobalFuncs::generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
    }

    $nimsoftAlerts = new NimsoftAlerts;
    $viewNimsoftTktSts = $nimsoftAlerts->dispNimsoftTktSts(); 
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $this->render('index',array(
        'viewNimsoftTktSts'=>$viewNimsoftTktSts,
        'dataProvider'=>$dataProvider,
        'model'=>$model,
    ));        
}

2 个答案:

答案 0 :(得分:0)

点击分页按钮后,这就是所谓的 -
$dataProvider=new CActiveDataProvider('NimsoftAlerts');
而不是
if(isset($_POST['NimsoftAlerts'])) { [......] }

所以试试这个 -

$model=new NimsoftAlerts;
$model->unsetAttributes();  // clear any default values

//Removed the content from here.

if(isset($_POST['NimsoftAlerts'])) 
{
    $model->attributes=$_POST['NimsoftAlerts'];
    if($model->validate())
    {
        [.....]
    }
    else {
        $dataProvider = $model->search();
    }
}
else {
    $dataProvider = $model->search();
}

$model->search()将添加过滤器,分页将包含从$_GET[]收到的过滤值。

我希望它有所帮助。

答案 1 :(得分:0)

使用分页时,似乎不会发送自定义搜索表单中的所选日期,因此会获取所有记录。所以你必须手动发送它们。

我在我的示例项目中实现了相同的场景。尝试在项目中使用它。

在您的视图文件中 -

//Your form has datepicker, this form has a textbox. On submit the values are posted to apply the filter.
<form id="filter_form" method="POST">
    <input name="Skill[name]" id="Skill_name" value="<?php echo $model->name; ?>" />
    <input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>

<script>
    $(document).ready(function() {
        $('.page a').click(function(ev) {
            ev.preventDefault();
            $.fn.yiiGridView.update('skill-grid', {data: $('#filter_form').serialize()});       //This will include the values from the form (dates in your case)
            return false;
        });
    });
</script>

在你的控制器动作中 -

$model = new Skill;
$model->unsetAttributes();

if(isset($_REQUEST['Skill'])) {
    $model->attributes = $_REQUEST['Skill'];
}
$dataProvider = $model->search();

$this->render('admin', array(
    'dataProvider' => $dataProvider,
    'model' => $model,
));`

重要提示:替换&#39;技能&#39;使用&#39; NimsoftAlerts&#39;。

的型号名称

我希望它有所帮助。