网格视图获取Yii中的所有记录

时间:2014-05-12 05:21:28

标签: php yii yii-extensions yii-components

我是Yii的新手。我通过修改我的操作中的条件来执行常规查询操作。我的网格视图从条件查询中提取特定记录,但如果我单击第二页,它将再次显示所有记录。请帮帮我。我被困在这里这么久。提前谢谢。

我的观点:

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'nimsoft-alerts-form',
    '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')));?>

</div>


<?php $this->endWidget(); ?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;



     <?php //zii.widgets.grid.CGridView 
$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()
    {
        $model=new NimsoftAlerts;
                if(isset($_POST['NimsoftAlerts']))
        {
                        $model->attributes=$_POST['NimsoftAlerts'];
                        $params=array(
                'host_start_date'=>$model->host_start_date,
                'host_end_date'=>$model->host_end_date,
                        );
                        if($model->validate())
                        { 
                        $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,
                            'pagination'=>array(
                        'params'=>$params,
                ),
                'sort'=>array(
                        'params'=>$params,
                        'attributes'=>array('host_start_date','host_end_date'),
                ),
                            ));
                       }
                       else
                       $dataProvider=new CActiveDataProvider('NimsoftAlerts');

                }
        else
                 $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(

                     'pagination'=>array(
                        'params'=>$params,
                ),
                'sort'=>array(
                        'params'=>$params,
                        'attributes'=>array('host_start_date','host_end_date'),
                ),
                         ));

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

        }


        $viewNimsoftTktSts = $model->dispNimsoftTktSts(); 
        $this->render('index',array(
                'viewNimsoftTktSts'=>$viewNimsoftTktSts,
                'dataProvider'=>$dataProvider,
                'model'=>$model,
        ));



    }

我的模特:

<?php

/**
 * This is the model class for table "mst_nimsoft_alerts".
 *
 * The followings are the available columns in table 'mst_nimsoft_alerts':
 * @property string $alert_id
 * @property string $alert_id_nimsoft
 * @property string $alert_subject
 * @property string $alert_message
 * @property string $alert_severity
 * @property string $alert_device
 * @property string $alert_ip_address
 * @property string $alert_status
 * @property string $alert_monitor_type
 * @property string $alert_instance
 * @property string $alert_attribute
 * @property string $alert_value
 * @property string $alert_event_time
 * @property string $alert_probe
 * @property string $alert_datetime
 * @property string $alert_itsm_ack
 * @property string $alert_itsm_ack_status
 * @property string $alert_itsm_ack_datetime
 * @property string $alert_itsm_ticketnumber
 */
class NimsoftAlerts extends CActiveRecord
{
    public $host_start_date;
    public $host_end_date;
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'mst_nimsoft_alerts';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('host_start_date,host_end_date', 'required'),
                        array('host_end_date','compare','compareAttribute'=>'host_start_date','operator'=>'>', 'allowEmpty'=>false,'message'=>'{attribute} must be greater than "{compareValue}".'),
            array('alert_id_nimsoft, alert_ip_address, alert_probe', 'length', 'max'=>220),
            array('alert_device, alert_status, alert_monitor_type, alert_instance, alert_attribute, alert_value, alert_event_time', 'length', 'max'=>250),
            array('alert_itsm_ack', 'length', 'max'=>1),
            array('alert_itsm_ack_status', 'length', 'max'=>4),
            array('alert_itsm_ticketnumber', 'length', 'max'=>255),
            //array('alert_subject, alert_message, alert_severity, alert_datetime, alert_itsm_ack_datetime,host_start_date,host_end_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('alert_id, alert_id_nimsoft, alert_subject, alert_message, alert_severity, alert_device, alert_ip_address, alert_status, alert_monitor_type, alert_instance, alert_attribute, alert_value, alert_event_time, alert_probe, alert_datetime, alert_itsm_ack, alert_itsm_ack_status, alert_itsm_ack_datetime, alert_itsm_ticketnumber,host_start_date,host_end_date', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'alert_id' => 'Alert',
            'alert_id_nimsoft' => 'Alert Id Nimsoft',
            'alert_subject' => 'Alert Subject',
            'alert_message' => 'Alert Message',
            'alert_severity' => 'Alert Severity',
            'alert_device' => 'Alert Device',
            'alert_ip_address' => 'Alert Ip Address',
            'alert_status' => 'Alert Status',
            'alert_monitor_type' => 'Alert Monitor Type',
            'alert_instance' => 'Alert Instance',
            'alert_attribute' => 'Alert Attribute',
            'alert_value' => 'Alert Value',
            'alert_event_time' => 'Alert Event Time',
            'alert_probe' => 'Alert Probe',
            'alert_datetime' => 'Alert Datetime',
            'alert_itsm_ack' => 'Alert Itsm Ack',
            'alert_itsm_ack_status' => 'Alert Itsm Ack Status',
            'alert_itsm_ack_datetime' => 'Alert Itsm Ack Datetime',
            'alert_itsm_ticketnumber' => 'Alert Itsm Ticketnumber',
                        'host_start_date'=>'Start date',
                        'host_end_date'=>'End Date',
                            );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */


        public function dispNimsoftTktSts()
    {
        $criteria = new CDbCriteria;
        $criteria->select='alert_datetime,alert_itsm_ack_status,alert_itsm_ticketnumber,alert_itsm_ack_datetime';

    $criteria->limit = 3;
    $criteria->order='alert_datetime DESC';
    $dispLimit=$this->findAll($criteria);
    return $dispLimit;
}

public function searchForAutoTickets()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('alert_id',$this->alert_id,true);
    $criteria->compare('alert_id_nimsoft',$this->alert_id_nimsoft,true);
    $criteria->compare('alert_subject',$this->alert_subject,true);
    $criteria->compare('alert_message',$this->alert_message,true);
    $criteria->compare('alert_severity',$this->alert_severity,true);
    $criteria->compare('alert_device',$this->alert_device,true);
    $criteria->compare('alert_ip_address',$this->alert_ip_address,true);
    $criteria->compare('alert_status',$this->alert_status,true);
    $criteria->compare('alert_monitor_type',$this->alert_monitor_type,true);
    $criteria->compare('alert_instance',$this->alert_instance,true);
    $criteria->compare('alert_attribute',$this->alert_attribute,true);
    $criteria->compare('alert_value',$this->alert_value,true);
    $criteria->compare('alert_event_time',$this->alert_event_time,true);
    $criteria->compare('alert_probe',$this->alert_probe,true);
    $criteria->compare('alert_datetime',$this->alert_datetime,true);
    $criteria->compare('alert_itsm_ack',$this->alert_itsm_ack,true);
    $criteria->compare('alert_itsm_ack_status',$this->alert_itsm_ack_status,true);
    $criteria->compare('alert_itsm_ack_datetime',$this->alert_itsm_ack_datetime,true);
    $criteria->compare('alert_itsm_ticketnumber',$this->alert_itsm_ticketnumber,true);
    $criteria->limit = 10;
    $criteria->order='alert_datetime DESC';


    return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            "pagination" => false
    ));
}


public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('alert_id',$this->alert_id,true);
    $criteria->compare('alert_id_nimsoft',$this->alert_id_nimsoft,true);
    $criteria->compare('alert_subject',$this->alert_subject,true);
    $criteria->compare('alert_message',$this->alert_message,true);
    $criteria->compare('alert_severity',$this->alert_severity,true);
    $criteria->compare('alert_device',$this->alert_device,true);
    $criteria->compare('alert_ip_address',$this->alert_ip_address,true);
    $criteria->compare('alert_status',$this->alert_status,true);
    $criteria->compare('alert_monitor_type',$this->alert_monitor_type,true);
    $criteria->compare('alert_instance',$this->alert_instance,true);
    $criteria->compare('alert_attribute',$this->alert_attribute,true);
    $criteria->compare('alert_value',$this->alert_value,true);
    $criteria->compare('alert_event_time',$this->alert_event_time,true);
    $criteria->compare('alert_probe',$this->alert_probe,true);
    $criteria->compare('alert_datetime',$this->alert_datetime,true);
    $criteria->compare('alert_itsm_ack',$this->alert_itsm_ack,true);
    $criteria->compare('alert_itsm_ack_status',$this->alert_itsm_ack_status,true);
    $criteria->compare('alert_itsm_ack_datetime',$this->alert_itsm_ack_datetime,true);
    $criteria->compare('alert_itsm_ticketnumber',$this->alert_itsm_ticketnumber,true);

            return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return NimsoftAlerts the static model class
 */
public static function model($className=__CLASS__)
{
    return parent::model($className);
}

}

1 个答案:

答案 0 :(得分:0)

这可以使用&#39; get&#39;对于表单方法而不是&#39; post&#39;你似乎习惯使用的方法。

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'method' => 'get',
    'id'=>'nimsoft-alerts-form',
    'enableAjaxValidation'=>false,
)); ?>

在您的操作中,使用以下代替$_POST变量:

$_GET['NimsoftAlerts']