Setting empty text in kartik dynagrid

那年仲夏 提交于 2020-01-15 10:53:45

问题


Am using kartik dynagrid and i would like to setup a text that shows when the dataprovider returns empty

The grid

echo DynaGrid::widget([
            'columns' => $columns,
            'showPersonalize' => true,
         'emptyText'=>'Sorry all pr have pritems',///-----------------This is what i had set
            'options' => ['id' => 'assignsolic-grid'],
            'gridOptions' => [
                'options' => ['id' => 'grid'],
                'dataProvider' => $dataProvider,
                 'filterModel' => $searchModel,
                 'showPageSummary'=>false,
                'pager' => [
                    'firstPageLabel' => 'First',
                    'lastPageLabel' => 'Last',
                    'maxButtonCount' => 10,
                ],
                   'panel' => [
             'type' => GridView::TYPE_PRIMARY,
                       // 'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i> </h3>',
                      'before'=>'<i>Select the Prs to assign solicitation and then click the Assign Solicitation button</i>',
                      'after' =>

        Html::button(' <i class=" glyphicon glyphicon-road "></i> Assign Solicitation ', ['value' => Url::to('assignsolc'),'class' => 'btn btn-danger', 'id' => 'assignsolic']),

                        'footer' => false
                    ],
                'toolbar' => [
                    ['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'],
                    '{export}',
                    '{toggleData}'
                ],
                'pjax' => true,
                'bordered' => false,
                'striped' => true,
                'condensed' => true,
                'responsive' => true,
                'responsiveWrap' => false,
                'containerOptions'=>['style'=>'overflow:scroll'],
            ]
        ]) ;

This returs an error of Setting unknown property: kartik\dynagrid\DynaGrid::emptyText how can i set the empty text


回答1:


You can define the value for null display directly in confi/main.php formatter component

'components' => [
   .......
        'formatter' => [
        'class' => 'yii\i18n\Formatter',
        'dateFormat' => 'dd.MM.yyyy',
        'decimalSeparator' => ',',
        'thousandSeparator' => ' ',
        'currencyCode' => 'EUR',
        'nullDisplay' => '',           // **** this param 
    ],
 ..... 

Otherwise if the widget don't provide a proper attribute you can use an anonymous function for value

  [
    'attribute' => 'your_attribute',
    'value' => function ($model) {
        if ( $model->your_attribute == NULL) {
            return  'Sorry all pr have pritems';
        } else {
            return $model->your_attribute;
       }
     },
  ],



回答2:


Since am using $dataProvider i found out that i just have to check if the dataProvider is empty by

if (!$dataProvider->totalCount > 0) { pass in message to display }
 else{?>

  SHOW THE GRID HERE

 <?php
  }
  ?>

   ?>


来源:https://stackoverflow.com/questions/38803908/setting-empty-text-in-kartik-dynagrid

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