【问题标题】:Using a modal window to add a record to the current page and updating via ajax in Yii,使用模态窗口向当前页面添加记录并通过 Yii 中的 ajax 进行更新,
【发布时间】:2013-06-10 22:01:13
【问题描述】:

我有一个包含帖子和 cmets 的页面。我有一个评论按钮,可以打开一个模式窗口并允许发表评论。我可以得到评论保存没问题。我可以让页面更新或模式关闭,但我不能让两者同时发生。我能找到的每个例子都把这个搞砸了 4 个小时。我很感激任何帮助。

这是我的控制器。

public function actionIndex(){  
        $this->layout='account2';
        $feedItems=NewsfeedItem::model()->getFeed('account');
        $player=Players::model()->findByPK(Yii::app()->user->id);
        $this->render('index', array('player'=>$player, 'feedItems'=>$feedItems));
    }

public function actionAddcomment(){
    $comment=new NewsfeedComment;
    $comment->text=$_POST['comment'];
    $comment->players_id=Yii::app()->user->id;
    $comment->newsfeed_item_id=$_POST['newsfeeditem'];
    $comment->datetime=date('Y-m-d h:i:s');
    $comment->save();
    $feedItems=NewsfeedItem::model()->getFeed('account');              
    $this->renderPartial('_newsfeed', array('feedItems'=>$feedItems), false, true);     
}

这些是我的视图文件

索引

Welcome, <?php echo $player->firstname;?>
<br />
<br />
<div id='newsfeed'>
    <?php $this->renderPartial('_newsfeed', array('feedItems'=>$feedItems)); ?>
</div>

_newsfeed

<?php 
foreach($feedItems as $item){
    $onclick='$(mydialog' . $item->id . ').dialog("open"); return false;';
    echo "<h6>" . $item->getAuthor() . "</h6><br />";
    echo '"' . $item->text . '" - ';
    echo CHtml::link('Comment', '#', array(
       'onclick'=>$onclick,
    ));
    echo '<br />';
    $comments=array_slice($item->comments, -5);
    foreach($comments as $comment){

        echo '<h6>';
        echo $comment->text;
        echo '</h6><br />';
    }
    $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
        'id'=>"mydialog" . $item->id,
        // additional javascript options for the dialog plugin
        'options'=>array(
            'title'=>'Comment',
            'autoOpen'=>false,
        ),
    ));

        ?>
        <form method='POST' action='/account/addcomment'>
            <textarea name='comment'></textarea>
            <input type='hidden' name='newsfeeditem' value='<?php echo $item->id; ?>'>
            <?php $closeclick='js:function(){$(mydialog' . $item->id . ').dialog("close");}'; ?>
            <div class="row buttons">
                <?php echo CHtml::ajaxSubmitButton('Add Comment', '/account/addcomment', array(
                    'update'='#newsfeed', //REMOVING THIS LINE ALLOWS MODAL TO CLOSE
                    'success'=>'js:'
                        .'function(){'
                          .'$("#mydialog' . $item->id . '").dialog("close");'
                        .'}',
                )); ?>
            </div>
        </form>

    <?php
    $this->endWidget('zii.widgets.jui.CJuiDialog');


    echo "<br />";
}
?>

【问题讨论】:

    标签: php jquery yii modal-dialog


    【解决方案1】:

    在您的 renderPartial 中再定义 2 个参数:

    $this->renderPartial('_newsfeed', array('feedItems'=>$feedItems),false,true);
    

    【讨论】:

    • 这似乎没有解决问题。按照现在的代码,如果我发表评论,模式会关闭,但不会更新页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2010-12-15
    • 2015-07-26
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多