chanhle
chanhle

Reputation: 168

How to assign action for earch button in Yii?

I have a page with Groups of Teams which has delete team button next to it. When team is not in group it has checkbox and button to add team to group. I wrote in actionView that will render a list of groups with teams.

actionView in GroupController

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);
    }

}

in view file

<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>

My idea is that when you click delete team button it will delete team from group and I has a method for this

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

When team not in group it will has checkbox and update button that will add team to group if checkbox is checked.

Thank for advance!

Upvotes: 1

Views: 2835

Answers (1)

llamerr
llamerr

Reputation: 3176

if i understand right what is your problem then you need to read this doc chapter

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

all your actions i.e. delete or add must reside in controller and not in view

instead of this in view:

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

        }?>

you must add something like this into controller

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

and instead of this

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

something like this must be in a view

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

or you can modify CGridView to suite your needs

Upvotes: 2

Related Questions