LazyJack
(1459148900)
1
I have an array:
public $size=[
['id' => '0', 'data' => 'Small'],
['id' => '1', 'data' => 'Middle'],
['id' => '2', 'data' => 'Large'],
];
Could somebody help me realize the Gridview below, thanks.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'size',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Use ArrayDataProvider:
http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html
$dataProvider = new ArrayDataProvider([
'allModels' => $size,
'sort' => [
'attributes' => ['id', 'data'],
],
'pagination' => [
'pageSize' => 10,
],
]);
LazyJack
(1459148900)
3
I want to realize that the field ‘size’ to be filled by ‘small’ or ‘big’ or ‘large’ rather than 0,1,2.
From the definition, $size is an array, not a field.
LazyJack
(1459148900)
5
I know how to realize this through database.
I want to realize this with the fields with static values, using database table is not needed.
If the data are organized in array, you can use ArrayDataProvider, with the example shown.
LazyJack
(1459148900)
7
This line works for Forms.
echo $form->field($model, ‘size’)->dropDownList([
'0' => 'Small',
'1' => 'Big',
'2' => 'Large',
]);
With DetailView, what shall I do?
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'size',
…
Please help me write the code for ‘size’, thanks!
I have understood. Try this:
$arrSizes=[ 0 => 'Small', 1 => 'Middle', 2=>'Large' ];
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'label' => 'Size',
'value' => $arrSizes[$model->size]
]
LazyJack
(1459148900)
9
Thank you for understanding me and for your patient.
I use the code below,
<?php
$arrSizes=[ 0 => ‘Small’, 1 => ‘Middle’, 2=>‘Large’ ];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'finish',
'value' => $arrSizes[$dataProvider->finish],
],
and got the error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: yii\data\ActiveDataProvider::finish
It works fine if I comment the code out. The field ‘finish’ is fine to show as int value.
<?php
$arrSizes=[ 0 => ‘Small’, 1 => ‘Middle’, 2=>‘Large’ ];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
/*
[
'label' => 'finish',
'value' => $arrSizes[$dataProvider->finish],
],
*/
In GridView, ‘value’ attribute of DataColumn is:
function ($model, $key, $index, $column)
http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$value-detail
So in GridView:
<?php
$arrSizes=[ 0 => 'Small', 1 => 'Middle', 2=>'Large' ];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'finish',
'value' => function ($model, $key, $index, $column) use ($arrSizes)
{
return $arrSizes[$model->finish];
}
],
LazyJack
(1459148900)
11
Thank you very much! It works.
Would you please do me one more favour. How to realize this kind of work in DetailView, many thanks.
The code below doesn’t work.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'label' => 'finish',
'value' => function ($model, $key, $index, $column) use ($finish)
{
return $finish[$model->finish];
}
],
Posted yesterday:
$arrSizes=[ 0 => 'Small', 1 => 'Middle', 2=>'Large' ];
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'label' => 'Size',
'value' => $arrSizes[$model->size]
]