show only one content in yii widget - php

Hai I am trying to display some products view on yii if some condition is applied i have to limit contents in the following code:
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model->name), ['view', 'id' => $model->id]);
},
]) ?>
I had added something like
pagination' => [
'pageSize' => 1,
],
But unable to get the desired result when i use pagination I am getting error
Setting unknown property: yii\widgets\ListView::pagination
how to limit content in view? is it possible?

I think you did mistake. You have to use it in form:
...
'pager' => ['pagination'=> ['pageSize' => 1]],
...
pagination is under the pager option

Related

How to pass the database data and display it in yii2 highCharts?

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;
}

Yii2 Customized Row's total in footer Kartik v ( display total of row values in footer)

I want to display total of row vaules in footer.
I have tried several ways but doesn't show proper result.
Any help will be highly appriciated.
I am using kartik grid.
I have tried this Yii2: Kartik Gridview sum of a column in footer
but doesn't work in my case.
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'width' => '36px',
'header' => '',
'headerOptions' => ['class' => 'kartik-sheet-style']
],
[
'attribute' => 'vno',
'footer'=>'Total'
],
[
'attribute' => 'fliters',
'label' => 'Total Fuel Consumption Liters',
'footer'=> true // Here i want to display the sum of column filer ( Note : the value in grid of filter is a sum(fuel_liters) )
],
[
'attribute' => 'fuel_rate',
'label' => 'Fuel Rate (Per Ltr.)',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_rate ( Note : the value in grid of filter is a sum(fuel_rate) )
],
[
'attribute' => 'famt',
'label' => 'Total Fuel Consumption Amount',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_amount ( Note : the value in grid of filter is a sum(fuel_amount) )
],
];
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
So far I manage to print total in footer
Im my Controller i have wriiten this
$total_ltrs = FuelConsumption::find()->sum('fuel_liters');
$total_amt = FuelConsumption::find()->sum('fuel_amount');
$total_rate = FuelConsumption::find()->sum('fuel_rate');
return $this->render('petrol-report', [
'total_ltrs' => $total_ltrs,
'total_amt' => $total_amt,
'total_rate' => $total_rate,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
And passed 'footer'=>$total_ltrs 'footer'=>$total_rate and 'footer'=>$total_amt Respectively in all footers.
Now when i do search throgh From Date and TO Date the result is dislaying in grid as per filter.
But the Result of Total in footer keeps unchanged.
I want to do some of only those rows which are in grid.
Can anybody help me on this ??
in your GridView, enable showFooter
in your column, set the footer text
now you will need a function to return your sum and set is a the footer's text.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showFooter' => true,
'columns' => [
[
'class' => '\common\grid\CheckboxColumn'
],
[
'attribute' => 'myfield1',
'footer' => 'my footer'
],
],
]);
if you want a function, use a static helper:
class MyHelper {
public static function footer() { time(); }
}
'footer' => MyHelper::footer()

Yii2 : 'Trying to get property of non-object' in GridView

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']);

How i do custom Yii2 gridview sort?

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'
..
],

Yii2 Listview how to display data in view

Greetings
I want to display data from my Atividades model in Yii2
My AtividadesController has the following code for actionView2
public function actionView2()
{
$query = new Query;
$dataProvider = new ActiveDataProvider([
'query' => $query->from('Atividades'),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $dataProvider->getModels();
return $this->render('view2', ['dataProvider' => $dataProvider, 'posts' => $posts]);
}
And in my View 2 i have the following List View that appears with the message showing 4 of 4 items, but doesn't shows the items
<?= ListView::widget([
'dataProvider' => $dataProvider,
]); ?>
In Y1.xx i had a property called 'attributes' for display the model fields
How can i display the model fileds in Yii2 inside this Listview
Many thanks in advance
I have solved it by myself :)
It was not hard
In my View2 Written the following code
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_view2',
]); ?>
And then duplicated an original view file, changed its name to _view2, put it in the same folder, and style it has needed
In my ActividadesController changed the actionView2 code to:
public function actionView2()
{
$dataProvider = new ActiveDataProvider([
'query' => Atividades::find(),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $dataProvider->getModels();
return $this->render('view2', ['dataProvider' => $dataProvider]);
}
_View2 code
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'atividade',
'descricao:ntext',
//'ativo',
],
]) ?>
SOLVED

Categories