M
M
Max DangerPro2016-11-25 05:12:25
JavaScript
Max DangerPro, 2016-11-25 05:12:25

How to change data using pjax?

I need to somehow update the data in the database, probably using PJAX.
I have a dropDownList in it with a list of categories. As well as GridView and checkboxes (CheckboxColumn) are displayed in it.
Here is the list of categories:

Html::dropDownList('records','null', ArrayHelper::map($listData,'id','name'),
            [
                'prompt' => '-- Выбрать категорию --',
                'class' => 'form-control'
            ]);

I was offered this JavaScript code:
$(document).on("click", "#bulk", function(event) {
        event.preventDefault();
        $.post("<?= Url::current()?>", {records: $('#grid').yiiGridView('getSelectedRows')}).done(function() {
            // обновить pjax или страницу
        });
    });

In the controller I write this:
public function actionBulk(){
        if($records = Yii::$app->getRequest()->post('action')) {
            Book::save(['id' => $records]);
            Yii::$app->session->setFlash('success', "Категория у книги {$model->name} обновлена");
        }
    }

You need to update the "id" field. The ID field corresponds to the data output line through the GridView.
Questions:
1. Where should I insert this code?
2. How to refresh the page via pjax ?
3. What to write so that the data is saved in the database.
PS I don't know JavaScript at all =(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-25
@DangerPro

1. Where should I insert this code?

I understand that you mean JS, and this is the code executed on the client, respectively, it should be located in the views as a code, file or resource kit .
why google? the first answer
Read the documentation
PS You would first deal with the framework and JS, otherwise you ask questions about the basic points, for which there is a lot of information on the Internet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question