I've been trying to add a class to the images' column in the CRUD GridView in Yii2. So far, I've managed to display the image, but at its full width and height. I need to add a 'col-md-3' bs class to the image.
This is what I pulled off:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'emp_firstname',
'emp_lastname',
'emp_photo' => [
'format' => 'image',
'attribute' => 'emp_photo',
'value' => 'emp_photo',
'contentOptions' => ['class' => 'col-md-3'],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
if you want to add id or class or both to the table
try this.
'tableOptions' => [
'id' => 'theDatatable',
'class'=>'table table-striped table-bordered'
],
form the column group you can use options http://www.yiiframework.com/doc-2.0/yii-grid-column.html#$options-detail
'emp_photo' => [
'format' => 'image',
'attribute' => 'emp_photo',
'value' => 'emp_photo',
'options' => ['class' => 'col-md-3'],
],
but you should complete the "bootstrap grid" in the other columns
Related
I'm trying to modify a field of the GridView that I obtained following the Gii tutorial on the Yii framework website.
GII PAGE
I'm not satisfied on how the population field looks, so I'm trying to convert it with some separators.
This is the index.php of the Country View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'label' => 'Population',
'value' => 'population',
/* 'value' => Yii::$app->formatter->asDecimal((integer)population) */
/* 'value' => Yii::$app->formatter->asDecimal($model->population) */
/* 'value' => Yii::$app->formatter->asDecimal('population') */
/*'value' => Yii::$app()->format->number('population')*/
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
I don't know why in the CountryModel the population field is listed as integer:
['population'], 'integer'
And then when I try to convert it in the view I have some problems because 'population' is basically a String.
I commented out some of my attempts.
You can use yii\i18n\Formatter
Go to your common\config\main.php if you are using app-advanced or the app/config/main.php if app-basic and add the following under components array.
'formatter' => [
'thousandSeparator' => ',',
],
Now you can format any given number like below
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'attribute' => 'population',
'label' => 'Population',
'value' => function($model){
return Yii::$app->formatter->asInteger($model->population); //Output 1,000
},
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
Update: Like was mentioned in comments, You can also use this two technics to format the value:
//Gridview
....
[
'attribute' => 'population',
'format' => 'integer',
],
....
and/or:
//Gridview
....
'population:integer',
....
how can I set width of column of gridview in view yii2,
reference link as below,
try this,
<?=
GridView::widget([
'model' => $model,
'options' => ['style' => 'width:80%'],
'attributes' => [
'id',
],
])
?>
To set width of one column individually something like the following:
[
'attribute' => 'attribute_name',
'headerOptions' => ['style' => 'width:20%'],
],
For all columns try:
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'width:100px;'],
],
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'pk_int_payroll_id',
[
'attribute' => 'fk_int_emp_id',
'value' => 'fkIntEmp.vchr_name',
'filter' => Html::activeDropDownList($searchModel, 'fk_int_emp_id', ArrayHelper::map(TblPayroll::find()->asArray()->all(), 'fk_int_emp_id', 'fk_int_emp_id'),['class'=>'kartik\grid\ActionColumn','prompt' => 'Select name'])
],
'vchr_worked_hours',
'vchr_actual_hours',
[
'attribute' => 'fk_int_payroll_month',
'value' => 'fkIntPayrollMonth.vchr_month'
],
[
'attribute' => 'fk_int_payroll_year',
'value' => 'fkIntPayrollYear.year'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?> </div>
this is my gridview with search. i want to search and result should show in
gridView. it working correctly, but before search the gridview show all the data,## how to hide gridview before search??? is it possible
I have two tables "attendance" with attributes id, status, date and "staff". staff_id is used as foreign key in attendance table. In _form.php of attendance I used
<?= $form->field($model, 'status')->dropDownList([ 'Present' => 'Present', 'Absent' => 'Absent', 'Leave' => 'Leave',], ['prompt' => 'Select status']) ?>
for dropdown. Now I want to a dropdown in gridview search columns with property of filtering and searching. I would like my gridview to be filtered by the dropdown list I have. So when I choose a value from the dropdown list, it should search on base of choosed value. Any help would be highly appriciated.
I think your question is about status field
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
........
[
'attribute' => 'status',
'label' => 'Status',
'filter' => [ 'Present' => 'Present', 'Absent' => 'Absent', 'Leave' => 'Leave',]
],
......
add this code in your cgridview,
array(
'name'=>'name_of_field',
'value'=>function($data){
echo $data->relation_name->name;
},
'filter'=>CHtml::listData(Model::model()->findAll('condition_if_any'),'id','name'),
'htmlOptions' => array('style' => "text-align:center;"),
),
Try this for add drop down in filter.
[
'attribute' => 'name_of_field',
'value' => function($model){
return $model->relationName->name;
},
'filter' => \yii\helpers\ArrayHelper::map(Model::find()->all(), 'id', 'name'),
],
fetch data from tables automatically:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'deptname',
'value' => 'deptname',
'filter'=>ArrayHelper::map(Attendance::find()->joinWith('staff')->orderBy('id')->all(), 'status', 'status'),
],
]
]);
?>
i have Gridview in index i want to show width and height both in one column how can i do it
here is the view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => 'fldSize.fld_width',
],
// 'fld_size_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
i have relation fldSize in model here it is just only displaying fld_width i want to show it in the format fld_width."x".fld_height how can i do it in Yii2
You should simply use value callback, e.g. :
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => function ($model) {
return $model->fldSize->fld_width . 'x' . $model->fldSize->fld_height;
},
],
Sorry that after more than one year, but it works (not a dropdown but Select2).
Here is the code for the form
<?= $form->field($model, 'ID_MACH')->widget(Select2::classname(), [
'data'=> ArrayHelper::map(Maschines::find()->all(),'ID_MACH','fullname'),
'language'=>'ru',
'theme'=>'krajee',
'options'=>['placeholders'=>'suda...',
'prompt'=>'10-'],
'pluginOptions'=>[
'allowclear'=>true
],
Next is the Model for Mashines:
public function getFullName()
{
return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}