Yii2 Gridview数据在Pjax中没有变化

时间:2015-03-25 08:44:13

标签: yii2

您好我在yii2中使用pjax执行搜索。我关注this link。 提交的给定的上述链接工作正常,但是想在滑块的变更进行。
Here is screenshort of my form .

的index.php

<div class="bank-index">    
<?php  echo $this->render('_search', ['model' => $searchModel]); ?>
<?php \yii\widgets\Pjax::begin(); ?>

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'bank_name',
        'state',
        'district',
        'city',
        'branch',
        'time',
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

_search.php

<div class="bank-search">

<?php Pjax::begin(['id' => 'Pjax_bank_id']); ?>

<?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
]); ?>

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

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

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

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

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

<?= 
 $form->field($model, 'time')->widget(Slider::classname(), [
'pluginOptions'=>[
'min'=>0,
'max'=>20,
'step'=>1
]
]);
?>

<?php // echo $form->field($model, 'branch') ?>

<div class="form-group">
    <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>
**我如何注册js文件**     registerJs($这 - &GT;呈现(&#39; searchJs.php&#39;)); ?&GT;

searchJs.php

$('#banksearch-time').on('slideStop', function(){
alert('hello');
       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 time = $('#banksearch-time').val();
                var url = 'index'+ '&_csrf='+csrf+'&BankSearch[bank_name]='+bank_name+'&BankSearch[state]='+state+'&BankSearch[district]='+district
                           +'&BankSearch[city]='+city +'&BankSearch[time]='+time;
                $.pjax.reload({url: url, container:'#Pjax_bank_id'});

            }
        });    
    }).on('submit', function(e){
    e.preventDefault();
});

2 个答案:

答案 0 :(得分:0)

已解决问题是我没有在gridview中将ID分配给Pjax。
<?php yii\widgets\Pjax::begin(['id' => 'gridresult']) ?>

我已经分配了 _search .php 的Pjax id 我更改了发件人: $.pjax.reload({url: url, container:'#gridresult'});
TO: $.pjax.reload({url: url, container:'#gridresult'});

答案 1 :(得分:0)

你好解决了这个问题。这是另一种方法。有人可能会觉得这很有帮助:

$url = \Yii::$app->getUrlManager()->createUrl('bank/index');

<?= $form->field($model, 'time')->widget(Slider::classname(), [
        'pluginOptions'=>[
            'min'=>0,
            'max'=>20,
            'step'=>1
        ],
        'pluginEvents' => [
            "slideStop" => "function() { refreshGridview($(this).val())}",
        ]
    ]);  ?>


$js1 = <<<JS
        function refreshGridview(value)
        {
            $.ajax({
                url: "$url",
                type: 'post',
                data: { "BankSearch[id]": value,  },
                success: function(response) { 
                    var csrf = yii.getCsrfToken();
                    var bank_id = value;
                    $.pjax.reload({
                        url: "$url", 
                        method: 'POST', 
                        data: { 
                                _csrf: csrf, 
                                "BankSearch[id]": value, 
                        }, 
                        container:'#bank'
                    });
                }
            });    
        }
JS;
$this->registerJs($js1);

slidestop插件事件中,使用当前滑块值调用自定义函数,然后通过Pjax更新gridview。