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.
Related
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;'],
],
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.
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 have gone through this:
http://www.yiiframework.com/extension/yii-barcode-generator-8-types/
https://github.com/Vilochane/Yii-Barcode-Generator
http://www.yiiframework.com/extension/yii2-barcode-generator-8-types/
But doesn't get it work. My gridView:
<?= GridView::widget([
'dataProvider' => new yii\data\ActiveDataProvider(['query' => $model->getLibBookMasters()]),
'summary' => '',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
//'type' => 'raw',
'value'=> function($model){
return \barcode\barcode\BarcodeGenerator::widget(
[
'elementId' => 'lbm_barcode_no',
'value'=> 'lbm_barcode_no',
'type'=>'ean13',
]);},
],
],
]); ?>
I need to pass elementId that do the trick but doesn't found it.
I just installed Barcode Generator and don't know how to play around with.
You need to pass different elementIds. As your code is currently your are passing the literal 'lbm_barcode_no' instead of the value of the lbm_barcode_no attribute of your models. In addition, you have to create the divs where the barcode is to be shown and set the format of the column to raw or html:
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
'format' => 'raw',
'value'=> function($model){
return yii\helpers\Html::tag('div', '', ['id' => 'barcode-'.$model->lbm_barcode_no]).
\barcode\barcode\BarcodeGenerator::widget([
'elementId' => 'barcode-'.$model->lbm_barcode_no,
'value'=> $model->lbm_barcode_no,
'type'=>'ean13',
]);
},
],
],
I prefixed the tags with barcode- to avoid collisions (you never know).
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;}