从Yii2高级模板Gridview获取值过滤

时间:2022-10-05 09:10:34

here is my code :

这是我的代码:

index.php

的index.php

<?= Html::a('Download Excel',NULL, ['class' => 'btn btn-primary','onclick'=>'
          var query;
          $.ajax({
            type: "GET",
            url:"'.Url::to(['excel']).'",
            data:query,
            success:function(response){
              response = response.replace(/^\s+|\s+$/g,"");
              location.href = response;
            }
          })
']) ?>

<?php Pjax::begin(['id' => 'pjax-gridview']) ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider2,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        //'m_id',
        [
          'attribute'=>'ins_id',
          'format' => 'text',
          'filter' => ArrayHelper::map($data,'ins_id','ins_nama'),
          'value' => function($data){
                $ins = new Instansi();
                $nama = $ins->find()->where(['ins_id'=>$data->ins_id])->one();
                return $nama->ins_nama;
          },
        ],
        'm_kegiatan',
        'm_location',
        [
          'attribute'=>'m_sifat',
          'format' => 'text',
          'filter' => Array('baru' => 'Baru', 'lanjutan' => 'Lanjutan', 'rehab' => 'Rehab', 'perluasan' => 'Perluasan'),
        ],
        'm_volume',
        [
          'attribute'=>'m_biaya',
          'format' => 'text',
          'value' => function($data){
                return 'Rp. '.number_format($data->m_biaya,'0',',','.');
          },
        ],
        //'created_by',
        //'created_at',
        //'updated_by',
        //'updated_at',
        //'m_status',
    ],
]); ?>
<?php Pjax::end() ?>

i want to get value from grid view filters like when i add filter on grid the index URL will become localhost/frontend/controller_name/index?ModelSearch[name]=filter_value

我想从网格视图过滤器中获取价值,比如当我在网格上添加过滤器时,索引URL将变为localhost / frontend / controller_name / index?ModelSearch [name] = filter_value

so the URL of my ajax will be localhost/frontend/controller_name/excel?ModelSearch[name]=filter_value

所以我的ajax的URL将是localhost / frontend / controller_name / excel?ModelSearch [name] = filter_value

but i don't know how to get value from the gridview filter, i was try to serialize with Pjax id but it's now working maybe you guys can help ?

但我不知道如何从gridview过滤器中获取价值,我尝试使用Pjax id进行序列化,但它现在正在工作,也许你们可以帮忙吗?

1 个解决方案

#1


0  

'Url::to(['excel'])' would become 'Url::to(['excel', Html::getInputName($searchModel,'name'])=>Html::getAttributeValue($searchModel,'name')])'

'url :: to(['excel'])'将成为'url :: to(['excel',Html :: getInputName($ searchModel,'name'])=> Html :: getAttributeValue($ searchModel,'名称')])'

Start your PJax widget before the Html a tag so the link is also updated when PJax is triggered by the Gridview search

在Html a标记之前启动PJax小部件,以便在Gridview搜索触发PJax时也更新链接

#1


0  

'Url::to(['excel'])' would become 'Url::to(['excel', Html::getInputName($searchModel,'name'])=>Html::getAttributeValue($searchModel,'name')])'

'url :: to(['excel'])'将成为'url :: to(['excel',Html :: getInputName($ searchModel,'name'])=> Html :: getAttributeValue($ searchModel,'名称')])'

Start your PJax widget before the Html a tag so the link is also updated when PJax is triggered by the Gridview search

在Html a标记之前启动PJax小部件,以便在Gridview搜索触发PJax时也更新链接