Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template

Home Page:https://dominus77.github.io/yii2-advanced-start/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to replace the message for 'confirmButtonText' and 'cancelButtonText'?

polinwei opened this issue · comments

I add cust action : approve, the confirmButton always show 'Yes, delete!' , How can I replace it

'template'=>'{view} {update} {delete} {approve}',
'buttons'=>
	[
		'approve'=>function($url,$model,$key)
		{
			$options=[
				'title'=>Yii::t('app', 'Approval'),
				'aria-label'=>Yii::t('app','Approval'),
				'data-confirm'=>Yii::t('app','Are you sure approve this item?'),
				'data-method'=>'post',
				'data-pjax'=>'0',
				'confirmButtonText' => Yii::t('app', 'Yes, Approve!'),
				'cancelButtonText' => Yii::t('app', 'No, do not Approve!'),
			];
			return Html::a('<span class="glyphicon glyphicon-check"></span>',$url,$options);

		},
	],

Hi!
In this variant, ajax deletion with custom confirmation is implemented.
Look at how I implemented the confirm here:

  1. Connect SweetAlert2

    use dominus77\sweetalert2\assets\SweetAlert2Asset;

  2. We register

    SweetAlert2Asset::register($this);

  3. Js function confirm

    $canceled = json_encode([
    'title' => Module::t('module', 'Cancelled!'),
    'text' => Module::t('module', 'The action to remove the user has been canceled.'),
    'type' => 'error',
    ]);
    $script = new \yii\web\JsExpression("
    function confirm(options) {
    var title = options.title,
    text = options.text,
    confirmButtonText = options.confirmButtonText,
    cancelButtonText = options.cancelButtonText,
    url = options.url;
    swal({
    title: title,
    text: text,
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: confirmButtonText,
    cancelButtonText: cancelButtonText
    }).then(function () {
    $.post(url).done(function (data) {
    swal({
    title: data.title,
    text: data.text,
    type: data.type
    });
    $.pjax.reload({container: '#pjax-container', timeout: 5000});
    })
    }, function (dismiss) {
    if (dismiss === 'cancel') {
    swal({$canceled})
    }
    });
    }
    $(document).on('ready pjax:success', function() {
    $(\"[data-toggle='tooltip']\").tooltip();
    });
    ");
    $this->registerJs($script, \yii\web\View::POS_END);

  4. Delete button

    'delete' => function ($url, $model) {
    if ($model->isDeleted()) {
    $options = json_encode([
    'title' => Module::t('module', 'Are you sure?'),
    'text' => Module::t('module', 'You won\'t be able to revert this!'),
    'confirmButtonText' => Module::t('module', 'Yes, delete it!'),
    'cancelButtonText' => Module::t('module', 'No, do not delete'),
    'url' => $url,
    ]);
    } else {
    $options = json_encode([
    'title' => Module::t('module', 'Are you sure?'),
    'text' => Module::t('module', 'The user "{:name}" will be marked as deleted!', [':name' => $model->username]),
    'confirmButtonText' => Module::t('module', 'Yes, note!'),
    'cancelButtonText' => Module::t('module', 'No, do not tag.'),
    'url' => $url,
    ]);
    }
    return Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', [
    'title' => Module::t('module', 'Delete'),
    'data' => [
    'toggle' => 'tooltip',
    'pjax' => 0,
    ],
    'onclick' => "confirm({$options}); return false;",
    ]);
    },

  5. and Controller

    public function actionDelete($id)
    {
    if (Yii::$app->request->isAjax) {
    $model = $this->findModel($id);
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    // Запрещаем удалять самого себя
    if ($model->id !== Yii::$app->user->identity->getId()) {
    if ($model->isDeleted()) {
    if ($model->delete()) {
    return [
    'title' => Module::t('module', 'Done!'),
    'text' => Module::t('module', 'The user "{:name}" have been successfully deleted.', [':name' => $model->username]),
    'type' => 'success',
    ];
    }
    } else {
    $model->scenario = $model::SCENARIO_PROFILE_DELETE;
    $model->status = $model::STATUS_DELETED;
    if ($model->save()) {
    return [
    'title' => Module::t('module', 'Done!'),
    'text' => Module::t('module', 'The user "{:name}" are marked as deleted.', [':name' => $model->username]),
    'type' => 'success',
    ];
    }
    }
    }
    return [
    'title' => Module::t('module', 'Cancelled!'),
    'text' => Module::t('module', 'You can not remove yourself.'),
    'type' => 'error',
    ];
    }
    Yii::$app->session->setFlash('error', Module::t('module', 'Not the correct query format!'));
    return $this->redirect(['index']);
    }

I got the error message : Not the correct query format!

view: index.php

'approve'=>function($url,$model,$key)
{
	$options = json_encode([
		'title' => Module::t('module', 'Are you sure?'),
		'text' => Module::t('module', 'You won\'t be able to revert this!'),
		'confirmButtonText' => Yii::t('app', 'Yes, Approve!'),
		'cancelButtonText' => Yii::t('app', 'No, do not Approve!'),
		'url' => $url,
	]);
	return Html::a('<span class="glyphicon glyphicon-check"></span>', '#', [
		'title' => Yii::t('app', 'Approve'),
		'data' => [
			'toggle' => 'tooltip',
			'pjax' => 0,
		],
		'onclick' => "confirm({$options}); return false;",
	]);
},

control

    public function actionApprove($id)
    {
        if (Yii::$app->request->isAjax) {
            $model = Comment::findOne($id);
            if ($model->approve()) //核准
            {
                return [
                    'title' => Module::t('module', 'Done!'),
                    'text' => Module::t('module', 'The comment "{:id}" have been successfully approve.', [':name' => $model->id]),
                    'type' => 'success',
                ];

            }
        }
        Yii::$app->session->setFlash('error', Module::t('module', 'Not the correct query format!'));
        return $this->redirect(['index']);

    }

You did not specify a format in the controller:

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

Got it . And How refresh page ? I get the error: TypeError: $.pjax is undefined

Use Pjax

<?php Pjax::begin([
'id' => 'pjax-container',
'enablePushState' => false,
'timeout' => 5000,
]); ?>

Updating the page is started in the function:

$.pjax.reload({container: '#pjax-container', timeout: 5000});

Yes!! It works !! Thanks.