I am trying to create a column that displays a glyphicon. The glyphicon will link to an url which allows the user to download a file. Any help would be massively appreciated. Current code is as follows:
GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => 'common\widgets\Pagination',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Date',
'attribute' => 'call_datetime',
'format' => 'date',
],
[
'label' => 'Time',
'attribute' => 'call_datetime',
'format' => 'time',
],
'call_from',
'call_to',
'duration',
'call_type',
'extension',
[
'label' => 'File',
'attribute' => 'fname',
'value' => 'callRecFiles.fname',
],
It is the last attribute 'fname' that the user will be downloading.
Change your fname field array to:
[
'label' => 'File',
'attribute' => 'fname',
'value' => function($model) {
//here create glyphicon with URL pointing to your action where you can download file, something like
return $model->callRecFiles ? Html::a('Download', ['myController/download-action', 'fname' => $model->callRecFiles->fname]) : null;
}
],
And prepare proper action to allow user to download file.
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',
....
In Yii2 we have GridView like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'layout' => "{items}\n{summary}\n{pager}",
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
'id',
'size',
'program' => [
'label' => 'Program',
'value' => function($data)
{
return Html::a($data->program, ($data->program), ['target' => '_blank']);
},
'format' => 'raw',
],
'version',
'platform',
'license',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Is it possible to do to hide/show column, if we click, for example on button "Hide platform", then for show "Show platform", or maybe checkbox.
I cannot understand how to do this, help me please
You can do something like this:
- Name the column you want to handle, e.g. an ID
[
'class' => 'yii\grid\SerialColumn',
'options' => [ 'id' => 'serial-column' ],
'width' => '1%',
'vAlign' => 'middle',
'hAlign' => 'right',
]
Then you modify css to have that column disappeared at the beginning
#serial-column {display: none}
Then you apply js for a checkbox to make it appear:
jQuery('#some-chkbox').click(function(){
jQuery('#serial-column').toggle();
})
Yes, you can hide and show column conditionally using "Visible" Attribute.
[
'attribute' => 'email',
'label' => 'Email',
'visible' => ($_GET['type']) == 'b') ? true : false,
],
I believe this is what you are looking for.
In short - you can add custom links and script to toggle columns of the gridview table.
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
I've encountered another dummy problem with my Yii2 project. I've got a standard gridView in my view, defined liek this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{items}{summary}',
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => [
'style' => 'vertical-align: middle;'
]
],
[
'attribute' => 'name',
],
[
'attribute' => 'type',
'value' => function($model){
/* #var $model app\models\Object */
return $model->typeNames()[$model->type];
},
'filter' => Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'theme' => Select2::THEME_BOOTSTRAP,
'hideSearch' => true,
'options' => [
'placeholder' => 'Wybierz typ obiektu...',
'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null
]
]),
],
[
'attribute' => 'countryId',
'value' => 'country.name',
'filter' => Select2::widget([
'name' => 'ObjectSearch[countryId]',
'data' => Country::forWidgets(),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'Wybierz paĆstwo...'
]
]),
],
//other columns
],
]); ?>
I've defined a searchModel class:
class ObjectSearch extends Object
{
public function rules()
{
return [
[['type'], 'integer'],
[['name', 'city', 'countryId'], 'string']
];
}
//some other functions
public function search($params)
{
$query = Object::find()->where(['userId' => Yii::$app->user->id]);
$query->joinWith('country');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['countryId'] = [
'asc' => ['countries.name' => 'ASC'],
'desc' => ['countries.name' => 'DESC']
];
if (!$this->load($params) && $this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'objects.name', $this->name])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['=', 'objects.countryId', $this->countryId])
->andFilterWhere(['=', 'type', $this->type]);
return $dataProvider;
}
}
Sorting and searching works fine - I've got correct results. So what's my problem? For standard columns when I type some text in textInput the value of this input stays in it after search. But when I choose some value in Select2 widget search work, but after search the selected value disappear and I've got just a placeholder.
Thanks for your help,
Kamil
You should simply use initValueText param :
initValueText: string, the text to displayed in Select2 widget for the initial value.
e.g. :
Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'initValueText' => $searchModel->type,
// ... other params
])
You could also use it as an InputWidget :
Select2::widget([
'model' => $searchModel,
'attribute' => 'type',
'data' => Object::typeNames(),
// ... other params
])
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;}