Yii CGridView没有使用JS调用更新

时间:2013-06-28 21:15:33

标签: php ajax yii cgridview

使用以下代码我想要完成的是当'roleSelector'下拉列表更改时,它会向'admin / permissions / assign'url触发ajax请求,这会为所选角色设置一个状态。我需要它作为一个状态,所以我可以在CGridView中的CLinkColumn列中使用它。但是,当成功的javascript触发并且网格视图重新加载时,cgridview的内容不会更新,而摘要会更新(它显示应该在网格视图中的结果数)。

我不确定为什么,但这似乎是唯一一个我必须使用'ajaxUrl'属性才能使$ .fn.yiiGridView.update()调用工作的CGridView。如果我省略url属性,它会返回一个错误,指出它无法找到我不记得必须在过去设置的url。

行动:

class AssignAction extends CAction {

    // Used in the url to determine if we are assigning or revoking permissions
    const FLAG_APPLY = 'apply';
    const FLAG_REVOKE = 'revoke';

    // Identifier to the user saved state of the role selected
    const STATE_ROLE = 'roleSelected';

    public function run($name='', $action='') {

        // Get the role the user selected
        if (isset($_POST['roleSelector'])) {
            $roleSelected = $_POST['roleSelector'];
            Yii::app()->user->setState(self::STATE_ROLE, $roleSelected);
            Yii::app()->end();
        }

        // Get all of the roles from the system
        $roleSelected = Yii::app()->user->getState(self::STATE_ROLE);
        $roles = AuthItem::model()->byTypes(array(CAuthItem::TYPE_ROLE))->findAll();
        $authItemProvider = new CActiveDataProvider('AuthItem', array(
            'criteria' => array(
                'condition' => (!$roleSelected) ? '1=0' : '',
            ),
            'pagination' => array(
                'pageSize' => 30,
            )
        ));

        $this->getController()->render('assign', array(
            'roles' => $roles,
            'roleSelected' => $roleSelected,
            'authItemProvider' => $authItemProvider,
        ));
    }
}

观点:

<div id="content-header">
    <h1 class="p-mt10">Assign Permissions</h1>
    <div class="p-fr p-pb10">
        <?php
            $roleList = CHtml::listData($roles, 'name', 'label');
            echo CHtml::dropDownList('roleSelector', $roleSelected, $roleList, $htmlOptions=array(
                'empty' => 'Select a Role',
                'ajax' => array(
                    'type' => 'POST',
                    'url' => $this->createUrl('/admin/permissions/assign'),
                    'data' => array('roleSelector' => 'js: $(this).val()'),
                    'success' => 'js: function() { $.fn.yiiGridView.update("GridView-AuthItem") }',
                ),
            ));
        ?>
    </div>
</div>

<div id="content-body">
    <table>
        <?php
            $this->widget('zii.widgets.grid.CGridView', array(
                'id' => 'GridView-AuthItem',
                'dataProvider' => $authItemProvider,
                'columns' => array(
                    'label:properCase',
                    'typeName:properCase',
                    'description',
                    'bizrule',
                    array(
                        'class' => 'CLinkColumn',
                        'label' => 'Assign',
                        'urlExpression' => 'array("/admin/permissions/assign",
                        "name" => $data->name,
                        "action" => (AuthItemChild::model()->byParentAndChild(Yii::app()->user->getState(AssignAction::STATE_ROLE), $data->name)->find()
                            ? AssignAction::FLAG_REVOKE : AssignAction::FLAG_APPLY),
                        )',
                        'htmlOptions' => array(
                            'style' => 'text-align: center',
                        ),
                        'linkHtmlOptions' => array(
                            'class' => 'button gray icon i_stm_edit',
                        ),
                    ),
                ),
            ));
        ?>
    </table>
</div>

1 个答案:

答案 0 :(得分:0)

我忘了我把表标签放在那里,因为它最初是一个CListView。删除这些标签似乎解决了我的问题。