3

I am working on the Yii2 project. In this, on page load, I want to display an empty grid table. Data should get loaded to the Kartik GridView only after button click. I am able to display the grid view table with data on button click, but not able to display the empty grid table initially. To display data, I have added code as follows:

use kartik\grid\GridView;
<?=
        GridView::widget([
            'id' => 'crud-datatable',
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'pjax' => false,
            'floatHeader'=>true,
            'floatOverflowContainer'=>true,
        ])
        ?>

I was reading about renderEmpty() but don't know how to use this also I tried sending NULL as a dataprovider but was creating the issue. Is there any other GridView property that we can use to display an empty table initially?

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
AmarjaPatil4
  • 1,640
  • 3
  • 28
  • 41
  • 1
    One way to do that, not sure it is good for you or not. Before click you can pass ```$dataProvider`` array ```empty``` from the controller and after a click on button, you can pass an original array as you wish. – HP371 Mar 02 '20 at 09:24
  • 1
    It shows error when you pass dataprovider as array, because gridview tries to count records from object. So niter array, empty object or '' works here for dataprovider – AmarjaPatil4 Mar 02 '20 at 09:40
  • 1
    I mean to say you need to create custom $dataprovider with empty array.Ref : https://stackoverflow.com/a/28452101/4781696 – HP371 Mar 02 '20 at 09:44
  • 1
    Thank you @HP371, This link solved the question – AmarjaPatil4 Mar 02 '20 at 12:19
  • Glad to help you. :-) – HP371 Mar 02 '20 at 12:35
  • Does this answer your question? [Using Yii2 with array of data and a Gridview with sorting and filter](https://stackoverflow.com/questions/28428492/using-yii2-with-array-of-data-and-a-gridview-with-sorting-and-filter) – Muhammad Omer Aslam Mar 02 '20 at 20:00
  • @MuhammadOmerAslam, as I don't have much knowledge of Yii, this link helped me partially. I will post my answer. – AmarjaPatil4 Mar 03 '20 at 04:25

1 Answers1

2

To solve this, I have passed object of ArrayDataProvider as a value of dataProvider like,

$dataProvider = new \yii\data\ArrayDataProvider();
AmarjaPatil4
  • 1,640
  • 3
  • 28
  • 41