1

我有一个包含团队组的页面,delete team旁边有按钮。当团队不在组中时,它具有将团队添加到组的复选框和按钮。我在 actionView 中写道,它将呈现一个包含团队的组列表。

GroupController 中的 actionView

public function actionView($id) {
    $group = $this->loadModel($id);

    $teamlst = Group::getAllTeamOfGroup($id);
    $teamnotlst = Group::getAllTeamNotInGroup($id);

    // Submit
    $preSelectedItems = array();
    if (isset($_POST['teamlist'])) {
        $preSelectedItems = array();
        foreach ($_POST['teamlist'] as $selectedItem) {
            $preSelectedItems[] = $selectedItem;
        }
    }
   // $teamNo = CHtml::listData($teamnotlst, 'id', 'name');

    //Delete       




    $this->render('view', array(
        'model' => $group,
        'teamlst' => $teamlst,
        'preSelectedItems'=> $preSelectedItems,
        'group_id'=>$id,
        'teamnotlst' => $teamnotlst,
    ));

    if(isset($_POST['btndeleteteam'])){
            TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);
    }

}

在视图文件中

<div class="action">    
    <input type="submit" name="btnupdateteam" value="Update Team">        
</div>
<?php echo CHtml::endForm(); ?>
<div class ="team">

<div class="column1">
    <?php foreach ($teamlst as $team): ?>

        <div class="row">
            <?php
            echo $team->name;

            ?> 
            <input type="submit" name="btndeleteteam" value="Delete  Team">
            <?php
            if(isset($_POST['btndeleteteam'])){
                TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);    

            }?>
        </div>


    </div><!-- comment -->
<?php endforeach; ?>
    <?php    

    $preSelectedItems = array();
        if (isset($_POST['teamlist'])) {
            $preSelectedItems = array();
            foreach ($_POST['teamlist'] as $selectedItem) {
                $preSelectedItems[] = $selectedItem;
            }
        }

    $teamNo = CHtml::listData($teamnotlst, 'id', 'name');
    echo CHtml:: checkBoxList('teamlist', $preSelectedItems, $teamNo);
    ?>

</div>

<div class ="team available">

</div>

我的想法是,当您单击delete team按钮时,它将从组中删除团队,我有一个方法

TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);    

当团队不在组中时,它将具有复选框和更新按钮,如果选中复选框,则会将团队添加到组中。

感谢提前!

4

1 回答 1

2

如果我理解正确你的问题是什么,那么你需要阅读这个文档章节

http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action

您的所有操作,即deleteadd必须驻留在控制器中而不是在视图中

而不是这个:

        if(isset($_POST['btndeleteteam'])){
            TeamGroup::model()->deleteTeamGroup($team->id, $model->ID);    

        }?>

你必须在控制器中添加这样的东西

public function actionDelete($id) {
    TeamGroup::model()->deleteTeamGroup($id);    
    $this->redirect('group/view');
}

而不是这个

<input type="submit" name="btndeleteteam" value="Delete  Team">

像这样的东西必须在视图中

<a href="<?php echo Yii::app()->createUrl("group/delete",array('id'=>$team->id));?>">delete</a>

或者您可以修改 CGridView 以满足您的需求

于 2012-04-20T14:43:57.130 回答