YII2 add a sum column in the gridview - yii2

How to add a sum column in the gridview?? I have a table with the a grant_amount, and lcc_amount. I want to add grant and lcc and display it in the gridview. How is that done?
I have successfully displayed already the data from the table to the gridview perfectly.

You need set new column and set value, label property like that:
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
...
'grant_amount',
'lcc_amount',
[
'label' => 'Sum',
'value' => function ($model) {
return $model->grant_amount + $model->lcc_amount;
}
],
For more doc

Related

How to display values according to it's name in DetailView yii2?

I'm trying to display category values according to it's name, not id. I'm trying to do like so:
[
'attribute' => 'category_id',
'value' => 'category.name',
],
But then attribute doesn't get displayed. It displays: name instead of Category. Category name is displayed correctly.
Also tried 'category.name' ,but it's displaying same values, and 'category_id' is displaying label correctly, but the name - according to id's.
How should I solve that?
Are you define relation for category, like this?
public function getCategory()
{
return $this->hasOne(Category::className(),['id'=>'category_id']);
}
If you want to display in GridView, you can do it like you
[
'attribute' => 'category_id',
'value' => 'category.name',
],
If you want to display in DetailView, you can do it like this
[
'attribute' => 'category_id',
'value' => $model->category ? $model->category->name : '',
],
seems you need a label.
the simplest way is add the label direcly in the detail view item
[
'label' => 'Category',
'attribute' => 'category_id',
'value' => ....,
],

How to display names instead of id's in GridView Yii2?

I have a question here. I set the relations between item and sale tables and now my GridView column of Item name is displaying id's of it. But what I need is that it would display Item names instead of ID's. How should I do that?
Here is my GridView column:
[
'attribute' => 'item_id',
'value' =>
],
I was thinking that I should write a function with if statement, but I have a lot of names and it would be very long. Is there an easier way to solve it?
Assuming your relationship is called getItems(), and the field for the item's name is called name:
[
'attribute' => 'items.name'
],
In my case it is company table and products table.
comp_id is is the primary key in the company table and it has related with products table.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'prod_id',
'name',
'description:ntext',
[
'attribute' => 'comp_id',
'value' => 'comp.name' //getComp()
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
//getcomp function the product model page.
public function getComp()
{`enter code here`
return $this->hasOne(Company::className(), ['comp_id' => 'comp_id']);
}

Yii2 kartik detailview hide label

If I'm doing 'label' => false, it is still showing the empty cell of the label. Is it possible completely not to render label cell somehow? Many thanks!
UPDATE: 'labelColOptions' => ['hidden' => true] works!
You can use visible property :
For example,
[
'attribute' => 'name',
'value' => whatever,
'visible' => (!empty($model->name)),
]

yii2 join model as dataProvider

I've joined 2 tables like following:
$model = SalesEntry::find()
->joinWith('salesItems')
->all();
then in view used DataProvider like following:
GridView::widget([
'dataProvider' => $model,
'columns' => [
'date', // sample field from first table to see if ok
],
]);
and I’ve got following error:
Call to a member function getCount() on a non-object
What am I doing wrong here?
That is because an ActiveQuery instance is not a DataProvider, which the widget expects. You need to wrap it in an ActiveDataProvider for it to work:
GridView::widget([
'dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model]),
' columns' => [
'date', // sample field from first table to see if ok
],
]);
DataProvider in GridView should be an instance of yii\data\DataProviderInterface
See docs.

Multiple values in one field Yii2

How to display multpiple values in one field.. I Use Select2 Widget. If i use $courses_model[0] it display only one value
Controller
public function actionUpdateteachers($id)
{
$courses_model = ReferenceTeachersCourses::find()->where(['reference_teachers_id' => $id])->all();
.....
}
View
...
<?= $form->field($courses_model[0], 'reference_course_type_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map($courses,'id','name'),
'options' => ['multiple' => true],
'pluginOptions' => [
'allowClear' => true,
],
]);
...
?>
This widgets works fine - but your $courses_model[0]->reference_course_type_id must have an array of ids as a value if you want to see multiple values selected.