Yii2-editable-widget. Doesn't work with few widgets - yii2

I'm using editable widget (2amigos/yii2-editable-widget or kartik-v/yii2-editable). It works fine, but when I try it in foreach it works with first element only. I want to use a several widgets for a model. How can I solve it?
Here is the code:
foreach ($models as $model) {
echo Editable::widget( [
'model' => $model,
'attribute' => 'name',
'url' => 'site/rename',
'type' => 'text',
'mode' => 'pop',
'clientOptions' => [
' pk' => $model->id,
]
]);
}

I guess you have to set a unique id for each Editable Widget. Look how the js code for the widget is embedded.
e.g. if you use kartiks widget
echo Editable::widget([
'model' => $model,
'attribute' => 'name',
'type' => 'primary',
'size'=> 'lg',
'inputType' => Editable::INPUT_TEXT,
'editableValueOptions' => ['class' => 'text-success h3'],
'options'=> [
'id'=>'name-editable'.uniqid(),
]
]);

Related

yii2 render remaining character in form field

I use jlorente remainingcharacters widget to show remaining character countdown for Inputfield in _form.php:
<?= $form->field($model, 'char52', ['showLabels'=>true])->widget(\jlorente\remainingcharacters\RemainingCharacters::classname(), [
'type' => \jlorente\remainingcharacters\RemainingCharacters::INPUT_TEXTAREA,
'text' => Yii::t('app', '{n} characters remaining'),
'label' => [
'tag' => 'p',
'id' => 'my-counter',
'class' => 'counter',
'invalidClass' => 'error'
],
'options' => [
'rows' => '1',
'class' => 'col-md-12',
'maxlength' => 52,
'placeholder' => Yii::t('app', 'Write something')
]
]) ?>
<?= $form->field($model, 'text', ['showLabels'=>true])->widget(\dosamigos\ckeditor\CKEditor::className(), [
'options' => ['rows' => 1],
'preset' => 'full'
]) ?>
As I rendered form fields the height of textInput is not the same as standard.
Is there any solution for this problem.
I am also searching for other widgets for remaining charachters as I want to use kartik\form\ActiveForm for rendering on the same text input remaining charachter count and ActiveField Prepend Addon.
Try adding "form-control" class to your widget:
'options' => [
...
'class' => 'col-md-12 form-control',
...
]

Yii2 Custom filter Dropdown for gridview

I am trying to add a custom filter named Sort By Month in the grid view header here is an example of the previous version of the site which I am revamping see below image
I was looking into the layout option of the grid view and added a drop-down in the layout template before {items}
GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{summary}{errors}' . \kartik\widgets\Select2::widget(
[
'model' => $searchModel,
'attribute' => 'filter_month',
'theme' => \kartik\widgets\Select2::THEME_DEFAULT,
'data' => $searchModel->getFilterMonths(),
'pluginEvents' => [
"select2:select" => 'function() { $("#w2").submit();}',
// 'select2:select'=> new \yii\web\JsExpression("function(){console.log('here')}"),
],
'options' => [
'placeholder' => '--Select Month--',
],
'pluginOptions' => [
'allowClear' => true,
'width' => '160px',
],
]
) . '{items}{pager}',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'title',
'catalog',
'upc_code',
[
'attribute' => 'created_on',
'label' => 'Created On',
'filter' => \yii\jui\DatePicker::widget(['dateFormat' => 'yyyy-MM-dd', 'model' => $searchModel, 'attribute' => 'created_on']),
'format' => 'html',
],
[
'attribute' => 'status',
'label' => 'Status',
'format' => 'raw',
'value' => function ($data) {
switch ($data->status) {
case 0:
return "Being Edited (" . $data->created_on . ")";
break;
case 1:
return ($data->maxdate == '') ? 'Active' : 'Active';
break;
case 2:
return "Expired";
break;
}
},
],
['class' => 'yii\grid\ActionColumn'],
],
]
);
Now I want to submit the filters form when I select an option from the drop-down, but could not figure out how to attach the default filter submit event with the drop-down options so that it filters the results when I select any option in the drop-down.
For Custom filter you have used the perfect layout but you have to provide your custom field as a filterSelector.
"filterSelector" => "#". Html::getInputId($searchModel, 'AttributeName'),

Remove Dollar sign from Column value(Indian Currency) Yii2 kartik gridview

Below is the snapshot of my index page in Yii2 with Kartik Gridview widget;
Below is the gridview code for the same.
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'headerOptions' => ['class' => 'kartik-sheet-style'],
],
//'line_id',
'metal_name',
'item',
'unit_name',
'weight',
['attribute' => 'quantity',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->quantity;
}
}],
'rate_per_gram',
'making_charges',
[
'attribute' => 'total',
'format'=>'currency',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->total;
}
}
],
['class' => 'kartik\grid\ActionColumn'],
],
'responsive'=>true,
'hover'=>true,
'export'=>false,
'showPageSummary' => true,
]); ?>
I have tried lot of stuff to get plain number format with 2 decimals, but i could succeed in one.
How can I Format my Total column with Indian Currency, viz. Rs. 35670.00 or just 35670.00?
One way is to set currency directly:
[
'attribute' => 'total',
'format' => ['currency', 'INR'],
'pageSummary' => true,
],
PS. You don't need to set value key unless you want to replace it with something else in certain case.

Kartiv yii2 datepicker with model and custom id

I am using kartik datepicker extension http://demos.krajee.com/widget-details/datepicker in Yii2.
Issue :
If I use this with custom id for input it does not show show model validations
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...', 'id' => 'patient_dob'], **// with id clientside validations for model does not work**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
While below code works
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...'],**//id is not used here**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
But I want to use custom id .Any suggestions ?
You have to override the selectors, example:
$form->field($model, 'comment', ['selectors' => ['input' => '#myCustomId']])
->textarea(['id' => 'myCustomId']);?>
See https://github.com/yiisoft/yii2/issues/7627
<?=
$form->field($model, 'sales_date')->widget(DateControl::classname(), [
'name' => 'sales_date',
'value' => date('d-m-Y h:i:s'),
'type' => DateControl::FORMAT_DATETIME,
'autoWidget' => true,
'displayFormat' => 'php:d-m-Y h:i:s',
'saveFormat' => 'php:Y-m-d h:i:s',
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
],
])
?>
by using the following option you can add extra form tag to input
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
'class' => 'sales_date',
]
In my case out was
<input type="hidden" id="sales-sales_date" name="Sales[sales_date]" form="sales-form-red">
check by using link
Date Control Demo

yii2 grid view searching ,sorting ,filtering for two table

How to searching ,sorting ,filtering for two table in yii2 grid view? after generating from gii tool ,i got searching sorting for one table columns but i have to apply searching ,sorting ,filtering on columns of another table.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
// 'account_activation_token',
'email:email',
'profile.name',
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
//here i want to use the filter option just like in yii 1.1 but i dont know how to use the textbox search in yii2.0
],
// 'usertype',
// 'status',
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Try Using:
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
'filter' => function($model){
return Html::activeTextInput($searchModel, $this->name),
}
],