srusakov / yii2-activeresponse

Yii2的ajax模块,支持从服务器端进行主动控制

dev-master / 1.1.x-dev 2015-05-19 06:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:38:23 UTC


README

Yii2的ajax模块,支持从服务器端进行主动控制。大多数现有的Yii2的ajax模块使用javascript逻辑。yii2-ativeresponse通过服务器端进行控制。您在PHP控制器中添加的所有操作都将使用相应的jQuery函数执行。请参阅用法以获取详细信息。

安装

安装此扩展的首选方法是通过composer

运行以下命令

$ php composer.phar require srusakov/yii2-ativeresponse "dev-master"

"srusakov/yii2-ativeresponse": "dev-master"

将以下内容添加到您的composer.json文件的require部分。

用法

yii2-ativeresponse附加到您的布局文件
ActiveResponse::registerAssets($this);

这将注册所有必要的javascript文件并创建全局javascript变量。

调用服务器(不带参数)
<?= Html::a('ActiveResponse Button', null, [
    'href'=>'javascript:void(0);',
    'class' => 'btn btn-success',
    'onclick'=>"callAR('controller/action');"]) ?>
调用服务器(传递表单作为参数)
<form id="theForm" name="theForm" onsubmit="callAR('controller/action', $('#theForm').serializeObject())">

var parameters = $('#theForm').serializeObject();
parameters.your_parameter = 'your value';
callAR('controller/action', parameters);
调用服务器(带回调)
callAR('controller/action', $('#theForm').serializeObject(), function() {
    alert('Callback called!');
});
服务器端
class controllerController extends Controller {
    public function actionAction()
        {
            $ar = new \srusakov\activeresponse\ActiveResponse();

            // show standart browser's alert
            $ar->alert('called from PHP');

            // assign html to div
            $ar->html('div_id', $this->render('view'));

            // modify attribute
            $ar->attr('element', 'data-pjax' '0');

            // assign value to element
            $ar->val('element', 'new value');

            // modify css
            $ar->css('element', 'cssattr', 'value');

            //redirect
            $ar->redirect('/new_page.php');

            // evaluate javascript
            $ar->script("alert('wow!')");

            // and much, much more...
            $ar->focus('element');
            $ar->addClass('element', 'class');
            $ar->removeClass('element', 'class');
            $ar->show('element');
            $ar->hide('element');
            $ar->fadeIn('element');
            $ar->fadeOut('element');

            return $ar;
        }

使用ActiveResponse与DataGrid小部件

示例:在DagaGrid的ActionColumn内部添加删除按钮

    Pjax::begin(['options' => ['id' => 'pjax-container']]);
    echo GridView::widget([
        ...
        'columns' => [
            [
                'class' => ActionColumn::className(),
                'template' => '{delete}',
                'deleteOptions' => ['data-pjax' => 0, 'class' => 'action-delete'],
                'urlCreator' => function($action,$model,$key,$index) {
                    return  ["/controller/{$action}", 'id'=>$model->id];
                },
            ],


<script type="text/javascript">
<?php ob_start() ?>
$(document).ready(function() {
    $('body').on('click', '.action-delete', function(e){
        var href = $(this).attr('href');
        var message = $(this).data('confirm');
        if (typeof(message) !== 'undefined' && confirm(message)) {
            callAR(href);
        }
        return false;
    });
});
<?php $this->registerJs(ob_get_clean()) ?>
</script>

控制器内的PHP代码

public function actionDelete($id=null)
{
    $ar = new \srusakov\activeresponse\ActiveResponse();
    $model = Model::findOne(['id' => $id]);
    if ($model) {
        $model->delete();
    }
    $ar->script("$.pjax.reload({container:'#pjax-container'});");
    return $ar;
}

许可证

yii2-activeresponse是在BSD 3-Clause许可证下发布的。有关详细信息,请参阅附带的LICENSE.md文件。