Yii,自定义过滤器不起作用

Yii, custom filter is not working

我的自定义过滤器不起作用。谁能纠正我吗?

在我的

public function actionAdmin($mid=null) {
    // the appropriate codes here... 

    $date = ">= ".date("Y-m-d");

    $this->render('admin', array(
        'model' => $model,
        'mid' => $mid,
        'date'=>$date,
    ));
}

在我的 admin.php 中,我在适当的字段中添加了这一行,在本例中,dateEnd

已更新

array(
        'name'=>'dateEnd',
        'htmlOptions'=>array('width'=>'150px'),
        'filter'=>array('0'=>'', '1'=>$date),
),

好的,问题来了。无论我点击什么,它都不会过滤任何东西。我希望它过滤空白 space 或今天的日期。

请问我做错了什么吗?请随时纠正我。谢谢!

您无法在 cgridview 中访问传递的变量 。为此,您可以在控制器中定义一个全局变量,并在 cgridview 中访问它,如下所示:

class Yourcontroller extends Controller {

    public $date;
    public function actionAdmin($mid=null) {
      // the appropriate codes here... 

      $this->date = ">= ".date("Y-m-d");

      $this->render('admin', array(
        'model' => $model,
        'mid' => $mid
      ));
   }
}

现在,您可以访问 date 内部网格:

array(
    'name'=>'dateEnd',
    'htmlOptions'=>array('width'=>'150px'),
    'filter'=>array('0'=>'', '1'=>$this->date),
 ),

更改此行:

'filter'=>array('0'=>'', '1'=>$date), 

到这一行:

'filter' => array(">= ".date("Y-m-d") => Yii::t('app', 'Still On Leave')),

基本上第一个参数是真正的值,第二部分是要显示的View。

在第一行中,'1'=>$date 表示 1 个值,但要在 CGridView 的过滤器上显示一个 $date 字段。它将采用值 1 来过滤而不是 $date 字段。

像 HTML 中的 Select 元素一样对待它,其中有这一行:

<select>
    <option value="1">One</option>
    <option value="2">Two</option>
</select>

Javascript 基本上采用值而不是显示的单词 One

这个也是一样的解释。