I want to ask you about using Grid View in Yii2. In my web, i want to pass parameter from controller to gridview and i use it for GET parameter.
My Controller:
$sql = "SELECT * FROM adikbinaan WHERE adikbinaan.jenjang_id = $jenjang";
$n = count(CariAdikBinaan::findBySql($sql)->all());
$adikbinaan = new SqlDataProvider([
'sql' => $sql,
'totalCount' => $n,
'sort' => [
'attributes' => [
'ADIKBINAAN_NAMALENGKAP',
],
],
'pagination' => [
'pageSize' => 20,
],
]);
$adik = new CariAdikBinaan();
return $this->render('index', [
'dataProvider' => $adikbinaan,
'data' => $adik,
]);
My View
<?= GridView::widget([
'dataProvider' => $dataProvider,
'layout'=>"{pager}\n{items}\n{summary}",
'showFooter'=>true,
'showHeader'=>true,
'showOnEmpty'=>false,
'columns' => [
'ADIKBINAAN_NAMALENGKAP',
'ADIKBINAAN_TEMPATLAHIR',
'ADIKBINAAN_TANGGALLAHIR',
[
'label'=>'Aksi',
'format' => 'raw',
'value'=>function ($data) {
return Html::a(Html::encode("Lihat"),'adikbinaan/view?id='.$data->ADIKBINAAN_ID);
},
],
],
]); ?>
And i get "Trying to get property of non-object" in return Html::a(Html::encode("Lihat"),'adikbinaan/view?id='.$data->ADIKBINAAN_ID);
It looks like Action Column but i don't want to use it.
How do i fix it?
data is an array not an object. From the API page for SQLDataProvider:
SqlDataProvider provides data in terms of arrays, each representing a row of query result.
Therefore your code should read
return Html::a(Html::encode("Lihat"), 'adikbinaan/view?id='.$data['ADIKBINAAN_ID']);
Related
I have install the miloschuman\highcharts\Highcharts, website : https://www.yiiframework.com/extension/yii2-highcharts-widget. I have a database with the table columns lme_price and lme_name which I want to display the price of the copper in the highcharts. I'm using PHP.
Below is my code which I have done. This is the code in my models. I create a static function with the query inside it to find the data that I want from the database.
public static function getCopperPrice()
{
$getCopperPrices = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->all();
return $getCopperPrices;
}
Here is my code in the view. I have a table show in the view which show every data from the database.
<div class="lme_index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'searchModel'=> $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=> 'DATE',
'attribute' => 'lme_title',
],
[
'label'=> 'MATERIAL',
'attribute'=> 'lme_name',
],
[
'label'=> 'PRICE (US$)',
'attribute'=> 'lme_price',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Here is the code for the highcharts graph.
<div class = "graph">
<?= Highcharts::widget([
'options' => [
'title' => ['text' => 'Copper Price'],
'xAxis' => [
'categories' => []
],
'yAxis' => [
'title' => ['text' => 'Price $US']
],
'plotOption' => [
'series' => [
['name' => 'Copper',
'data' => [lme::getCopperPrices()]],
]
]
]
]);?>
I want to display only 1 material price in the highcharts. I call the function from the model inside the series but the graph shows nothing on it. The coding there didn't show any error to me.
Can someone help me or tell me how to solve this? Thanks.
Chart require integer values to display
public static function getCopperPrice()
{
$cooperPrices = [];
$copperPriceList = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->column();
foreach($copperPriceList as $price) {
$cooperPrices[] = (int) price;
}
return $cooperPrices;
}
I want to use Select2 dr as filter in Yii Grid view, but it doesn't filter at all, only refreshes the page.
I take the data from 2 tables - from accounts I take only user_id, and from credits I take everything else. And every filter works, except And the 'user_id' one.
<?php
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'wp_id',
'value' => 'accounts.user_id',
'filter' => Select2::widget(
[
'model' => $searchModel,
'attribute' => 'wp_id',
'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
'options' => ['placeholder' => ' --Filter by user id-- '],
'language' => 'en',
'pluginOptions' => [
'allowClear' => true,
],
]
),
],
],
]
); ?>
Here is the action in the controller.
<?php
public function actionIndex()
{
$searchModel = new CreditsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} ?>
And this is the search model method
<?php
public function search($params)
{
$query = Credits::find()->with('accounts');
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if(!empty($this->user_id)) {
$query->andFilterWhere(['=', 'accounts.user_id', $this->user_id]);
}
// grid filtering conditions
$query->andFilterWhere([
'user_id' => $this->user_id,
'wp_id' => $this->wp_id,
'credits_bought' => $this->credits_bought,
'purchase_date' => $this->purchase_date,
'id' => $this->id,
]);
return $dataProvider;
}
} ?>
Thanks in advance!
Because you are passing the wp_id under the GridView filter and selecting the wp_id from the Accounts model as the value of the select2-dropdown
ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id')
Although it is confusing that you use the gridview column attribute wp_id and use the select2 to filter the user_id for the same column but if you are looking to do that you might need to change the above to
ArrayHelper::map(Accounts::find()->all(), 'user_id', 'wp_id')
i want to display array in view form but i don't know how ?
can any one help me :
my controller code is :
$query = new Query;
$query
->select(['Day_Name'])
->from('days')
->join( 'INNER JOIN','doctorsworkday','doctorsworkday.Day_id =days.dayid');
$command = $query->createCommand();
$dataProvider = $command->queryAll();
$dataProvider= array($dataProvider);
return $this->render('view',
[
'model' => $this->findModel($id),
'days'=>$days,
'doctorsworkday'=>$doctorsworkday,
'dataProvider' => $dataProvider,
])
my view code is :
<?=
DetailView::widget([
'model' => $dataProvider,
'attributes' => [
'Day_Name'
],
])
?>
but its shows : (not set)
when i use vat_dump($dataProvider) there is an array .. but i don't know how to show it
enter Var_Dump
Well firstly you are using a DetailView for displaying multiple results. DetailView is used to display the detail of a single data model as said in the docs. You could use a GridView to display multiple results. GridView accepts a dataProvider not an array so you'll need to turn your array into a dataProvider.
fortunately you can use the class ArrayDataProvider to do just that:
$gridViewDataProvider = new \yii\data\ArrayDataProvider([
'allModels' => $dataProvider,
'sort' => [
'attributes' => ['Day_Name'],
],
'pagination' => ['pageSize' => 10]
]);
something like this should work.
Then you pass this $gridViewDataProvider to the gridview like this:
<?= GridView::widget([
'dataProvider' => $gridViewDataProvider,
'columns' => [
'name'
]
]) ?>
Also note that in your controller you wrapped your $dataProvider in an array with this line:
$dataProvider= array($dataProvider);
You should remove that line and then everything should work as far as I can tell.
How can I sort with a customized gridview header?
Please give the difference between label and header in the Yii2 gridview widget dataprovider.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header and label perform the same function?
How can I perform sorting in $data->myTitle?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
'sort' => ['attributes' => ['myTitle']],
]);
Add the attribute option in widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'attribute' => 'myTitle',
'label' => 'Page Title'
],
]); ?>
Since myTitle is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the default class is DataColumn
'columns' => [
[
'attribute' => 'myTitle',
'label' => 'Label',
]
I am not very sure I understand your question, but sorting option can be included in your modelsearch.php. So in your case you have to do like this.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
]);
if myTitle is a field in the database, why you are using such a long syntax. Just
'Columns'=>[
..
'myTitle',
..
],
should work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],
I have an array:
$arr = ['ID' => 'A1', 'Description' => 'Item to be sold', ...]
In controller:
$provider = new ArrayDataProvider([
'allModels' => $arr,
//'sort' =>['attributes' => ['ID', 'Description'],],
'pagination' => ['pageSize' => 5]
]);
$this -> render('index', ['provider' => $arr]);
In view (index.php):
GridView::widget([
'dataProvider' => $provider,
]);
And there is no result on page. Where it is wrong?
There are few errors in your code.
1) $arr should have the structure like this:
$arr = [
['ID' => 'A1', 'Description' => 'Item to be sold'],
...
],
2) In the render parameters you passed $arr instead of $provider, should be:
$this->render('index', ['provider' => $provider]);
3) You missed return statement before render:
return $this->render('index', ['provider' => $provider]);
Also I don't recommend using spaces around the arrow.
4) You did not specified any columns in GridView. You can add ID and Description like this:
GridView::widget([
'dataProvider' => $provider,
'columns' => [
'ID',
'Description',
],
]);
5) And finally you are not echoing the GridView to the screen. Should be:
echo GridView::widget([...]);
or
<?= GridView::widget([...]) ?>