简体   繁体   English

DropDownList在Yii中影响ClistView

[英]DropDownList effects a ClistView in Yii

I am still very new to this Yii framework, and I would like assistance with this code. 我对这个Yii框架还很陌生,希望获得有关此代码的帮助。 I currently manage to get a dropdownlist dependent on another dropdownlist but I can't seem to get the dropdownlist to effect what gets displayed in the ClistView. 我目前设法得到一个依赖于另一个下拉列表的下拉列表,但是我似乎无法使该下拉列表影响ClistView中显示的内容。

profile Controller 配置文件控制器

/* 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',
    )));*/
}

View Profile 查看资料

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

As you can see in the cListView. 如您在cListView中看到的。 I was debating on creating a _viewTeamMessage which will display the team message + sendtime. 我正在讨论创建一个_viewTeamMessage,它将显示团队消息和发送时间。 But I realize, I wouldn't be able to pass a dataprovider without re rendering the page, and i am trying to avoid heading into that direction. 但是我意识到,如果不重新呈现页面就无法通过数据提供程序,并且我正努力避免朝那个方向前进。

You could pull your Team messges out into a partial view and then just use a render partial to render just the messages into your page usig Ajax. 您可以将您的团队信息拉到局部视图中,然后只使用呈现局部视图就可以将消息仅呈现到您的usig Ajax页面中。 If the partial view is named _teamMessages.php it would look something like this (untested): 如果部分视图名为_teamMessages.php,它将看起来像这样(未经测试):

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

Then you modify your profile view to look like: 然后,将您的个人资料视图修改为如下所示:

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

Then finally you change your controller to something like this: 最后,您将控制器更改为以下形式:

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

this should just cause the message widget to be recreated instead of the whole page. 这只会导致重新创建消息窗口小部件,而不是整个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM