Yii2:使用带分页的Pjax POST方法在GridView中搜索 [英] Yii2 : Search in Gridview using Pjax POST Method with pagination

查看:142
本文介绍了Yii2:使用带分页的Pjax POST方法在GridView中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者yii2&试图在搜索按钮上使用Pjax在Gridview中搜索字段。我用GET方法完成了这一点,但我想通过使用POST方法来做到这一点。那么我怎样才能用Yii2 Pjax(post方法)进行分页?



下面是我的代码:



_details.php

 < ;?php 

使用yii \helpers\Html;
使用yii\widgets\ActiveForm;
使用yii\widgets\Pjax;
使用kartik\depdrop\DepDrop;


$ js =< //获取表单ID并设置事件
$('#bank-details-form' ).on('beforeSubmit',function(e){
var form = $(this);
if(form.find('。has-error')。length){
返回false;
}

$ .ajax({
url:form.attr('action'),
type:'post',
data:form.serialize(),
成功:函数(响应){
var txtValue = $(#bankdetails-bank_name)。val();
if(txtValue == )
{
alert(请选择银行名称);
返回false;
}

var bank_name = $('#bankdet ails-bank_name选项:selected')。text();
var state = $('#bankdetails-state option:selected')。text();
var district = $('#bankdetails-district option:selected')。text();
var city = $('#bankdetails-city option:selected')。text();
var url = form.attr('action')+'& BankDetails [bank_name] ='+ bank_name +'& BankDetails [state] ='+ state +'& BankDetails [district] ='+ district +' &安培; BankDetails [城市] ='+城市;

$ .pjax.reload({url:url,container:'#bank-grid'});
}
}); ('submit',function(e){
e.preventDefault();
})。

JS;
this-> registerJs($ js); ?>

< div class =col-lg-5>

<?php

Pjax :: begin(['id'=&'; bank-form']);
$ form = ActiveForm :: begin(['id'=>'bank-details-form',
'method'=>'post',
]);

if($ _ REQUEST ['bank_name'])
{
$ searchModel-> bank_name = $ selected;
}

// Bank level 1
echo $ form-> field($ searchModel,'bank_name') - > widget(DepDrop :: classname(),[
'data'=> $ bankName,
'options'=> ['placeholder'=>'Select Bank'],
'type'=> DepDrop :: TYPE_SELECT2 ,

'select2Options'=> ['pluginOptions'=> ['allowClear'=> true]],
'pluginOptions'=> [
'depends '=> [''],
'url'=> Url :: to(['/ students / child-account']),
]
]);

//状态级别2
echo $ form->字段($ searchModel,'state') - >小部件(DepDrop :: classname(),[
'类型'=> DepDrop :: TYPE_SELECT2,
'data'=> $ bankState,
'options'=> ['placeholder'=>'选择状态'],

'select2Options'=> ['pluginOptions'=> ['allowClear'=> true]],
'pluginOptions'=> [
'depends'=> [ 'bankdetails-bank_name'],
'url'=> Url :: to(['/ students / child-account']),
'loadingText'=>'Select Bank',
]
]);

//区域级别3
echo $ form->字段($ searchModel,'区域') - >小部件(DepDrop :: classname(),[
' data'=> $ bankState,
'options'=> ['placeholder'=>'Select District'],
'type'=> DepDrop :: TYPE_SELECT2,

'select2Options'=> ['pluginOptions'=> ['allowClear'=> true]],
'pluginOptions'=> [
'depends'=> [ 'bankdetails-state'],
'url'=> Url :: to(['/ students / auto-populate-districts']),
'loadingText'=>'Select District' ,
]
]);

//城市级别4
echo $ form->字段($ searchModel,'city') - >小部件(DepDrop :: classname(),[
' data'=> $ bankCity,
'options'=> ['placeholder'=>'Select City'],
'type'=> DepDrop :: TYPE_SELECT2,

'select2Options'=> ['pluginOptions'=> ['allowClear'=> true]],
'pluginOptions'=> [
'depends'=> [ 'bankdetails-district'],
'url'=> Url :: to(['/ students / auto-populate-cities']),
'loadingText'=>'Select City' ,
]
]);

?>

< div class =form-group>< br />
<?= Html :: submitButton('Search',['class'=>'btn btn-success'])?>
< / div>

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

< / div>


解决方案

尝试使用Pjax的post方法:


$ b $

  $。pjax.reload({url:url,method:'POST',container:'#bank-grid'}); 


I am beginner to yii2 & trying to search fields in Gridview using Pjax on search button. I have done this with GET method but I want to do this by using POST method. Then how can I do this with Yii2 Pjax(post method) with pagination?

Here is my code :

_details.php:

<?php          

    use yii\helpers\Html;                             
    use yii\widgets\ActiveForm;         
    use yii\widgets\Pjax;  
    use kartik\depdrop\DepDrop; 


    $js = <<<JS                       
           // get the form id and set the event                         
           $('#bank-details-form').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 txtValue = $("#bankdetails-bank_name").val();   
                    if(txtValue == "")  
                    {  
                        alert("Please select bank name");  
                        return false;  
                    }   

                    var bank_name = $('#bankdetails-bank_name option:selected').text();     
                    var state     = $('#bankdetails-state option:selected').text();     
                    var district  = $('#bankdetails-district option:selected').text();      
                    var city      = $('#bankdetails-city option:selected').text();           
                    var url       = form.attr('action')+ '&BankDetails[bank_name]='+bank_name+'&BankDetails[state]='+state+'&BankDetails[district]='+district+'&BankDetails[city]='+city;         

                    $.pjax.reload({url: url, container:'#bank-grid'});        
                }       
            });                                          
        }).on('submit', function(e){                              
                e.preventDefault();                         
        });

        JS;
        this->registerJs($js); ?>

        <div class="col-lg-5">                                            

        <?php                                  

            Pjax::begin(['id' => 'bank-form']);                          
            $form = ActiveForm::begin(['id'  => 'bank-details-form',           
                                    'method' => 'post',  
                                    ]);         

            if($_REQUEST['bank_name'])
            {
                $searchModel->bank_name = $selected;
            }

            // Bank level 1
            echo $form->field($searchModel, 'bank_name')->widget(DepDrop::classname(), [
            'data'    => $bankName,
            'options' => ['placeholder' => 'Select Bank'],
            'type'    => DepDrop::TYPE_SELECT2,

            'select2Options'  => ['pluginOptions'=>['allowClear'=>true]],
            'pluginOptions'   => [
                'depends'     => [''],
                'url'         => Url::to(['/students/child-account']),
                ]
            ]);

            // State level 2
            echo $form->field($searchModel, 'state')->widget(DepDrop::classname(), [
            'type'    => DepDrop::TYPE_SELECT2,
            'data'    => $bankState,
            'options' => ['placeholder'=>'Select State'],

            'select2Options'  => ['pluginOptions'=>['allowClear'=>true]],
            'pluginOptions'   => [
                'depends'     => ['bankdetails-bank_name'],
                'url'         => Url::to(['/students/child-account']),
                'loadingText' => 'Select Bank',
            ]
            ]);

            // District level 3
            echo $form->field($searchModel, 'district')->widget(DepDrop::classname(), [
            'data'    => $bankState,
            'options' => ['placeholder' => 'Select District'],
            'type'    => DepDrop::TYPE_SELECT2,

            'select2Options'  => ['pluginOptions'=>['allowClear'=>true]],
            'pluginOptions'   => [
                'depends'     => ['bankdetails-state'],
                'url'         => Url::to(['/students/auto-populate-districts']),
                'loadingText' => 'Select District',
            ]
            ]);

            // City level 4
            echo $form->field($searchModel, 'city')->widget(DepDrop::classname(), [
            'data'    => $bankCity,
            'options' => ['placeholder' => 'Select City'],
            'type'    => DepDrop::TYPE_SELECT2,

            'select2Options'  => ['pluginOptions'=>['allowClear'=>true]],
            'pluginOptions'   => [
                'depends'     => ['bankdetails-district'],
                'url'         => Url::to(['/students/auto-populate-cities']),
                'loadingText' => 'Select City',
            ]
            ]);

        ?>

        <div class="form-group"><br/>
            <?= Html::submitButton('Search', ['class' => 'btn btn-success']) ?>
        </div>

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

    </div>

解决方案

Try using post method of Pjax:

$.pjax.reload({url: url, method: 'POST', container:'#bank-grid'}); 

这篇关于Yii2:使用带分页的Pjax POST方法在GridView中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆