Yii2 Pjax无法正常工作

前端之家收集整理的这篇文章主要介绍了Yii2 Pjax无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用Pjax刷新gridview但不知何故它不起作用.这是代码

_search.PHP

<?PHP

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;

$this->registerJs("
                  $('#btnAjaxSearch').click(function(){
                        $.ajax({
                                type: 'get',data: $('.bank-search form').serializeArray(),success: function (data) {
                                      $.pjax.reload({container:\"#bank\"});
                                },error: function (XMLHttpRequest,textStatus,errorThrown) {
                                      alert('error');
                                }
                        });
                       return false;
                  });
                ",\yii\web\View::POS_END,'bank-search');
?>

<div class="bank-search">
    <?PHP Pjax::begin(['id' => 'bank-form']); ?>
    <?PHP $form = ActiveForm::begin([
        'action' => ['index'],'method' => 'get',]); ?>

    <?= $form->field($model,'bank_name') ?>

    <?= $form->field($model,'state') ?>

    <?= $form->field($model,'district') ?>

    <?= $form->field($model,'city') ?>

    <div class="form-group">
        <?= Html::Button('Search',['class' => 'btn btn-primary','id' => 'btnAjaxSearch']) ?>
    </div>

    <?PHP ActiveForm::end(); ?>
    <?PHP Pjax::end(); ?>

</div>

的index.PHP

<?PHP

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax; 

$this->title = 'Banks';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="bank-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?PHP  echo $this->render('_search',['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Bank',['create'],['class' => 'btn btn-success']) ?>
    </p>
    <?PHP Pjax::begin(['id' => 'bank']); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,'filterModel' => $searchModel,'columns' => [
            ['class' => 'yii\grid\SerialColumn'],'id','bank_name','state','district','city',// 'branch',['class' => 'yii\grid\ActionColumn'],],]); ?>
    <?PHP Pjax::end(); ?>
</div>

调节器

/**
     * Lists all Bank models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new BankSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index',[
            'searchModel' => $searchModel,'dataProvider' => $dataProvider,]);
    }

简单搜索正在运行,但Pjax不是.我是Yii2的新手,所以任何帮助都将不胜感激.谢谢.

谢谢爱丁.它帮助我解决了这个问题.这就是我做的.它可能会帮助面临同样问题的人.

正如Edin提到的,你需要将url和搜索参数一起传递给Pjax以刷新gridview.

这是我编辑的代码

$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);
原文链接:https://www.f2er.com/php/136251.html

猜你在找的PHP相关文章