【问题标题】:How to filter the rows of a gridview in yii using radio buttons如何使用单选按钮在 yii 中过滤 gridview 的行
【发布时间】:2013-07-24 10:36:46
【问题描述】:

我使用下面的代码在 Yii 框架中创建了两个单选按钮(批准消息和拒绝消息)

<?php echo CHtml::activeRadioButtonList($model, 'Approved', array('Approved Messages', 'Rejected Messages'), array('labelOptions'=>array('style'=>'display:inline'),'separator'=>'')) ?>

现在,当我单击“已批准的消息”单选按钮时,我必须过滤并显示表的 CGridView 中“已批准”列的值 = 1 的所有行,以及“已批准”列的表的 CGridView 中的所有行' 当我点击“被拒绝的消息”单选按钮时,值 = 0。我该怎么做呢

【问题讨论】:

    标签: php javascript yii


    【解决方案1】:

    我为此使用了一个下拉菜单,值为 Yes 和 No。只需使用以下代码将 approved 列转换为文本:

    array(
      'name' => 'approved',
      'value' => '($data->approved ? "Yes" : "No")',
      'filter' = >CHtml::dropDownList('Approved', '',  
                array(
                    ' '=>'All',
                    '1'=>'On',
                    '0'=>'Off',
                )
            ),
    )
    

    这个链接是我得到这个信息的地方:http://www.yiiframework.com/forum/index.php/topic/30694-cgridview-filter-dropdown-from-array/

    我使用cgridview filter example 进行了谷歌搜索

    【讨论】:

      【解决方案2】:

      好吧,让我们放入单选按钮而不是所有下拉菜单哈哈。 我假设您的视图设置如下:

      // view/index.php  (or similar)
      $this->widget('zii.widgets.grid.CGridView', array(
          'dataProvider'=>$model->search(),
          'filter'=>Message::model(),
          'columns'=>
          [
              'id',
              'username',
              'email:email',
              'approved'=>[
                  'name'=>'approved',
                  'filter'=>$this->approvedFilter(), 
                  // I like moving stuff like this out of the way.
                  // But maybe it's smarter to put it in your model instead?
              ]
          ]
      ));
      

      接下来是控制器。

      // MessageController.php  (or similar)
      public function actionIndex()
      {
          $model = Message::model();
          // All we need to do is to assign the incoming value to the model we are using...
          if ( isset( $_GET['Message']['Approved'] )){
              $model->approved = $_GET['Message']['Approved'];
          }
      
          $this->render('index', ['model'=>$model]);
      }
      
      // Oh yeah the filter. I just copied your code.
      public function approvedFilter(){
          return CHtml::activeRadioButtonList(
              Message::model(), 'approved',  array(0,1),
              array(
                 'labelOptions'=>array('style'=>'display:inline'),
                 'separator'=>''
              )
          );
      }
      

      此代码已经过测试,但我在最后一刻做了一些更改,如果它崩溃了,非常抱歉! 而且我仍然认为一个简单的 'approved:boolean' 更干净。 ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-16
        • 2014-10-12
        • 2020-02-08
        • 2017-06-15
        • 1970-01-01
        相关资源
        最近更新 更多