How to pass "elementId" in barcode generator in Gridview Yii2? - php

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).

Related

Modifty GridView::Widget content Yii

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 to set width of gridview in yii2

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;'],
],

Is it possible to hide column Yii2?

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.

How can I show two attributes values in one column through relation in Yii 2 GridView

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;}

Yii Framework 2.0 GridView and data from the join table

I have two database tables 'user' and 'role'. I used Yii framework 2.0 Gii to create CRUD with User model and UserSearch model. By default, Gii uses GridView::widget for the index page for the 'user' model.
In the search($params) method inside of the UserSearch model, I used the following code to join the above tables together
$query = User::find()->with('role');
Everything works fine with the query.
By default Gii does not include the data from the joined table 'role' in the GridView::widget inside of the views/user/index.php page. With the join query above I could retrieve data from both tables. In the views/user/index.php page I have injected the GridView::widget with the following code so that it also includes the data and column names from the joined table (role).
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
'role.role_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Everything works fine with the role data 'role_name included in the GridView::widget. But the problem is that there is no search box for the role_name. The GridView::widget creates search box for the User properties only. Is there any way to add search box for the properties of the joined table 'role' because I also would like to search through 'role_name' as well as through other properties of the User model.
Try this way:
In your UserSearch model add
UserSearch extends ...
{
public $roleFilterInputName; //the name of the filter search input
//important
function rules()
{
//add roleFilterInputName as safe
return [
[['xxx', 'roleFilterInputName'], 'safe'], //!!!!
];
}
}
in your grid:
'columns':
[
//...
[
'attribute' => 'roleFilterInputName',
'value' => 'role.role_name'
],
//...
]
in UserSearch::search()
$query->andFilterWhere(['like', 'role.role_name', $this->roleFilterInputName])
But I guess you'll have to use 'joinWith' instead of 'with'.
Inside CGridView add below code. It will enable filter with dropDownList.
[
'attribute' => 'act_role_id',
'label' => 'Actor Role',
'value' => 'actRole.role_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\ActorRole::find()->orderBy('role_name')->asArray()->all(),'act_role_id','role_name')
],
CGridView code Snippet is as below:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
[
'attribute' => 'act_role_id',
'label' => 'Actor Role',
'value' => 'actRole.role_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\ActorRole::find()->orderBy('role_name')->asArray()->all(),'act_role_id','role_name')
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Try it with
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'userid',
'username',
//'role.role_name',
['attribute' => 'role', 'value' => 'role.role_name'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I just have tried it in my code, so I'm not sure if this works also with your code. But if so, I don't know why it has to be defined this way.
I guess the answer of rajesh ujade also includes this definition, however, for Yii 1.
This worked for me
Table = Lead (id , year_id)
Table = Year (id, text)
Added text in lead (index.php)
Year::find()->all() = This code pull all value from table/ all years.
[
'attribute'=> 'year_id',
'format' => 'html',
'value' => 'year.value',
'label' => 'Year',
'filter' => Html::activeDropDownList($searchModel, 'year', yii\helpers\ArrayHelper::map(Year::find()->all(), 'year_id', 'value'), ['class' => 'form-control', 'prompt' => '---']),
],
Also it shows dropdown as well sorting.
image Showing Dropdown in Grdiview
#Ekonoval is going in right way.
Just add following in serch function of UserSearch:
After initializing ActiveDataProvider object like this:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
]);
$dataProvider->sort->attributes['roleFilterInputName'] = [
'asc' => ['role.role_name' => SORT_ASC],
'desc' => ['role.role_name' => SORT_DESC]
];
You may write query in your UserSearch model like
if($this->role)
{
$query->join('LEFT JOIN','role','role.user_id = user.id')->andFilterWhere(['role.item_name' => $this->role]);
}

Categories