DropDownList在Yii中影响ClistView

时间:2013-07-02 15:57:53

标签: php listview drop-down-menu yii

我仍然对这个Yii框架很新,我想帮助这个代码。我目前设法获得一个依赖于另一个下拉列表的下拉列表,但我似乎无法让下拉列表影响ClistView中显示的内容。

个人资料控制器

/* add a team message submitted by the coach of the team */
public function actionAddTeamMessage($id)
{
    /* check if team and message aren't null */
    if(isset($_POST['teamId']['addTeamMessage']))
    {
        try
        {
            /* creates a new message */
            $teamModel = new TeamMessage;
            $teamModel->teamId = $_POST['teamId'];
            $teamModel->content = $_POST['addTeamMessage'];
            $teamModel->sendTime = new CDbExpression('NOW()');
            $teamModel->save();
        }
        catch(Exception $e)
        {
            echo "Unable to save.";
        }
    }
    /* render the profile page for the current user */      
    $user=User::model()->findByPk($id);
    $this->render('profile', array(
        'model' => $user));
}

/* will handle functionality for the user dropdownlist ajax
 * under contructions
 */
public function actionDisplayMessage()
{
    $data = TeamMessage::model()->findAll('teamId=:teamId', array(
        ':teamId'=>(int) $_POST['teamId']
        )
    );

    $data=CHtml::listData($data,'id', 'content');

    echo "<option value=''>Select Message</option>";
    foreach($data as $value=>$content)
        echo CHtml::tag('option', array('value'=>$value),CHtml::encode($content),true);

    //TODO still being tested.
    /* for ClistView still debugging */
    /*$dataProvider=new CActiveDataProvider('Player', array(
        'criteria'=>array(
        'condition'=>'teamId=:teamId',
    )));*/
}

查看资料

<!-- Would allow user to access specific team messages and control how much gets display.
     still under construction. -->
    <div class="row">
        <?php 
            echo CHtml::dropDownList("teamId", 'id', Chtml::listData($model->memberOfTeams, 'id', 'teamName'),array(
                'empty'=>'Select Team',
                'ajax'=>array(
                    'type'=>'POST', // request type
                    'url'=>CController::createUrl('DisplayMessage'),
                    'update'=>'#teamMessages', // selector to update
                    'data'=>array('teamId'=>'js:this.value'),
                    )
                )
            ); 
        ?>
        <?php
            echo CHtml::dropDownList('teamMessages','',array(),array('empty'=>'Select Message'));
            /*$this->widget('zii.widgets.CListView', array(
                'dataProvider'=>$dataProvider,
                'itemView'=>'_viewTeamMessage',
                'id'=>'ajaxListView',
            ));*/
        ?>
    </div>

正如您在cListView中看到的那样。我正在讨论创建一个_viewTeamMessage,它将显示团队消息+发送时间。但我意识到,如果不重新渲染页面,我将无法传递数据提供者,而我正试图避免朝着这个方向前进。

1 个答案:

答案 0 :(得分:1)

您可以将团队消息拉出到局部视图中,然后只使用渲染部分将消息呈现到您的页面中使用Ajax。如果部分视图名为_teamMessages.php,它将看起来像这样(未经测试):

$this->widget('zii.widgets.CListView', array(
            'dataProvider'=>$dataProvider,
            'itemView'=>'_viewTeamMessage',
            'id'=>'ajaxListView',
        ));

然后您将个人资料视图修改为:

<!-- Would allow user to access specific team messages and control how much gets display.
 still under construction. -->
<div class="row">
    <?php 
        echo CHtml::dropDownList("teamId", 'id', Chtml::listData($model->memberOfTeams, 'id', 'teamName'),array(
            'empty'=>'Select Team',
            'ajax'=>array(
                'type'=>'POST', // request type
                'url'=>CController::createUrl('DisplayMessage'),
                'update'=>'.team-messages', // selector to update
                'data'=>array('teamId'=>'js:this.value'),
                )
            )
        ); 
    ?>
    <div class="team-messages">
    <?php 
       $this->renderPartial('_teamMessages',
            array('dataProvider'=>$dataProvider))
    ?>
    </div>
</div>

然后最后你将控制器更改为:

public function actionDisplayMessage()
{
    /* REMOVE
    $data = TeamMessage::model()->findAll('teamId=:teamId', array(
        ':teamId'=>(int) $_POST['teamId']
        )
    );

    $data=CHtml::listData($data,'id', 'content');

    echo "<option value=''>Select Message</option>";
    foreach($data as $value=>$content)
        echo CHtml::tag('option', array('value'=>$value),CHtml::encode($content),true);
    */

    // still being tested.
    $dataProvider=new CActiveDataProvider('Player', array(
        'criteria'=>array(
        'condition'=>'teamId=(int) $_POST['teamId']',
    )));
    $this->renderPartial('_teamMessages', array('dataProvider'=>$dataProvider);
}

这应该只是重新创建消息小部件而不是整个页面。