如何在Yii中为搜索按钮分配操作?

时间:2012-04-13 03:15:18

标签: php view yii

我有一个包含团队的网页,旁边有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);    

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

感谢提前!

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题是什么,那么你需要阅读这个doc章

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以满足您的需求