Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template
https://dominus77.github.io/yii2-advanced-start/
MIT License
23 stars 12 forks source link

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

Closed polinwei closed 7 years ago

polinwei commented 7 years ago

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

        },
    ],
Dominus77 commented 7 years ago

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

  1. Connect SweetAlert2 https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L9

  2. We register https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L12

  3. Js function confirm https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L21-L64

  4. Delete button https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L237-L263

  5. and Controller https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/controllers/backend/DefaultController.php#L255-L290

polinwei commented 7 years ago

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']);

    }
Dominus77 commented 7 years ago

You did not specify a format in the controller: https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/controllers/backend/DefaultController.php#L259

polinwei commented 7 years ago

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

Dominus77 commented 7 years ago

Use Pjax https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L75-L79

Dominus77 commented 7 years ago

Updating the page is started in the function: https://github.com/Dominus77/yii2-advanced-start/blob/3ecb9bc88e546392847a989467aeb625d0f092fc/modules/users/views/backend/default/index.php#L50

polinwei commented 7 years ago

Yes!! It works !! Thanks.