In my project, I have created a search box. when I type something and click the button "get info" I get all information in console window using ajax call. now I want to populate this data in yii2 gridview. data will different at every runtime. I want to give this data to $dataprovider of gridview is it possible??
here is code- CompaniesController.php
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
]);
}
}
companies/index.php
$form = ActiveForm::begin();
$typeahead = $form->field($model, 'name')->textInput(['maxlength' => true]);
$getinfobtn = Html::SubmitButton( 'Get info', [ 'class' => 'btn btn-success' , 'id' =>'getinfo']) ;
ActiveForm::end();
myjsfile.js
$("#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 window
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"}
how to show/ populate this data in gridview??