Yii2 Pjax not working

拥有回忆 提交于 2019-11-30 04:14:29

Thanks Edin. It helped me to solved the problem. Here is what I did. It might help someone facing the same problem.

As Edin mentioned you need to pass the url along with the search parameters to the Pjax in order to refresh the gridview.

Here's my edited code :

    $js = <<<JS
        // get the form id and set the event
        $('#bank-form-id').on('beforeSubmit', function(e) { 
           var form = $(this);
            if(form.find('.has-error').length) {
                return false;
            }
            $.ajax({
                url: form.attr('action'),
                type: 'post',
                data: form.serialize(),
                success: function(response) { 
                    var csrf = yii.getCsrfToken();
                    var bank_name = $('#banksearch-bank_name').val();
                    var state = $('#banksearch-state').val();
                    var district = $('#banksearch-district').val();
                    var city = $('#banksearch-city').val();
                    var url = form.attr('action')+ '&_csrf='+csrf+'&BankSearch[bank_name]='+bank_name+'&BankSearch[state]='+state+'&BankSearch[district]='+district+'&BankSearch[city]='+city;
                    $.pjax.reload({url: url, container:'#bank'});
                }
            });    
        }).on('submit', function(e){
        e.preventDefault();
    });
JS;
$this->registerJs($js);

The way Pjax is working is by sending another request with special headers. When pjax request is detected only html required to update container is returned from server. Line

$.pjax.reload({container:\"#bank\"});

will send another request, and inside actionIndex queryParams will be empty.

You can solve this by storing search parameters to session or by specifing pjax url with parameters in query string.

Try following:

  var url = urlWithFilters(); 
  $.pjax({url: url, container: '#bank'});

In this case you don't need to create own ajax call, just create url with with filters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!