How to show json data in yii2 gridview

I want to display json data in a gridview. I am getting data in a console window. and i want to show in gridview.how for this. below givencode

In the controller

public function actionCompanyinfo(){     

    $text_in_search = $_GET['text_in_search'];
    $left_items_cat = ltrim($_GET['left_items_cat']);

    if($left_items_cat == "Companies"){

         $query = (new \yii\db\Query())
                ->select(['c.name', 'c.id'])
                ->from(['companies as c'])
                ->where('c.name LIKE :query') 
                ->addParams([':query'=>'%'.$text_in_search.'%'])
                ->all(); 

        $response['comapnies_matching'] = $query;
            return \yii\helpers\Json::encode([
        $response
    ]);  

    }
}

      

In sight

$form = ActiveForm::begin();
 $typeahead = $form->field($model, 'name')->textInput(['maxlength' => true]);

$getinfobtn = Html::SubmitButton( 'Get info', [ 'class' => 'btn btn-success' , 'id' =>'getinfo']) ;
   ActiveForm::end();  

      

In the Js file

$("#getinfo").click(function(){
        var text_in_search = $("#companies-name").val();
        var left_items_cat = $('#left-items li.active').text();
        var url = "index.php?r=companies/companyinfo";

        $.ajax({
        url: url,
        dataType: 'json',
        method: 'GET',
        data: {text_in_search,left_items_cat},
        success: function (data, textStatus, jqXHR) {           
             // $( "#country"+id ).html(data[0].countries);
             console.log(data[0]);
           // **want to show this data in yii2 grid view**
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });

      

console.log

comapnies_matching
:
Array(3)
0
:
{name: "ADC Therapeutics Sarl", id: "402"}
1
:
{name: "ADC Therapeutics Sarl", id: "407"}
2
:
{name: "ADC Therapeutics Sarl", id: "412"}

      

In the console window, I get the output I want, but I want it to display this array as a grid. How to do it?

+1


source to share


1 answer


You can achieve this by using a Pjax widget in a framework.

In your view: (if you are not using a model, do not use ActiveForm)

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;

//Get active filters from request GET
$get = Yii::$app->request->get();
$name = isset($get['name']) ? $get['name'] : '';
$left_items_cat = isset($get['left_items_cat']) ? $get['left_items_cat'] : '';
?>

<?php Pjax::begin(); ?>
<?= Html::beginForm(['companies/companyinfo'], 'get', ['data-pjax' => '',]); ?>
<?= Html::dropDownList('left_items_cat', $left_items_cat,
    ['' => '', 'Companies' => 'Companies', 'Other' => 'Other'], ['class' => 'form-control']) ?>
<?= Html::input('text', 'name', $name, ['class' => 'form-control']) ?>
<?= Html::submitButton('Get Info', ['class' => 'btn btn-success', 'name' => 'getInfo']) ?>
<?= Html::endForm() ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
    ],
]); ?>
<?php Pjax::end(); ?>

      



And in your controller:

public function actionCompanyinfo()
{
    // Init DataProvider empty
    $dataProvider = new ArrayDataProvider(['allModels' => []]);
    // Manage the params send in request GET
    $text_in_search = $_GET['name'];
    $left_items_cat = ltrim($_GET['left_items_cat']);

    if ($left_items_cat == "Companies") {
        // Query rows and populate DataProvider
        $query = (new \yii\db\Query())
            ->select(['c.name', 'c.id'])
            ->from(['companies as c'])
            ->where('c.name LIKE :query') 
            ->addParams([':query'=>'%'.$text_in_search.'%'])
            ->all();

        $dataProvider->allModels = $query;
    }

    return $this->render('view', ['dataProvider' => $dataProvider]);
}

      

0


source







All Articles