select2 widget selected will updated other select2 - yii2

Hi all i am newbie in yii2 need your advise
i use select2 widget, when i selected value it will throw another value that i had set in array before. so how can i do this in yii2. so far i doing this.i have try using jquery function using pluginevents in select2 but still stuck..here is my code
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?= $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = $(this).val();
$("input#target").val($(this).val());
}',
]
]);
?>
<?= $form->field($model, 'nama_wp')->textInput(['id' => 'target']) ?>
how can i insert 'nama_wp' that already set in array into field nama_wp
thx for helping

First thing you need to change the
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
to
<?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
The you need to access the current value of the select2 via data.currentTarget.value, so change the code for change to the following.
<?php echo $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = data.currentTarget.value;
$("#target").val(data_id );
}',
]
]);
?>

Related

Is there a way to dynamically change maximumSelectionLength on kartik/select2 in yii2?

I am trying to update through javascript the maximumSelectionLength parameter of a select2 field (listeProduits) based on the value of a second select2 (nom_id) when it is changed.
I'm first storing the maxChildren values in a js table before loading the form so that I can access it on the client side when a new nom_id is selected.
The below approach doesn't work though as it messes up the listeProduits select2.
<script>
var nomMaxChildren = <?= json_encode(ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'max_children')); ?>;
alert(nomMaxChildren[1]);
</script>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'nom_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'nom'),
'options' => ['placeholder' => Yii::t('app','Select a name...')],
'pluginEvents' => [
"select2:select" => "function() {
$('select[name =\"listeProduits[]\"]').select2({
maximumSelectionLength: nomMaxChildren[\$(this).val()]
});
}",
]
]);?>
<label class="control-label"><?= Yii::t('app', 'Produits')?></label>';
<?= Select2::widget([
'attribute' => 'listeProduits',
'name' => 'listeProduits',
'data' => ArrayHelper::map(Produit::find()->asArray()->where(['statut_id' => 2])->andWhere(['boite_id' => null])->orWhere(['boite_id' => $model->id])->all(), 'id', 'numero_de_serie'),
'value' => (is_null($model->id)) ? null : ArrayHelper::map(Produit::find()->asArray()->where(['boite_id' => $model->id])->all(), 'id', 'id'),
'options' => ['placeholder' => Yii::t('app','Select products')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'maximumSelectionLength' => (isset($model->nom)) ? $model->nom->max_children : null,
],
'showToggleAll' => false,
]);?>
Also if you have any idea on how to validate the fact that the maximumSelectionLength is ok on client side, it would be great :D
set maximumSelectionLength property dynamically :
$('#demo-select').select2({ maximumSelectionLength: 1 });

Hide attribute in Kartik DetailView Yii 2

I want to hide attribute in my DetailView, for example:
[
'attribute' => $attribute,
'format' => 'raw',
'value' => Yii::$app->formatter->asDatetime($model->$attribute).' par '.Yii::$app->myFormatter->asUser($model->visited_by),
'visible' => false,
];
Visible property doesn't work. What do I have to do?
Do you have any idea?
Thank you
If you need to dinamically determine what is visible
Then you can try template option for te DetailView model
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'group_id',
],
'template' => function ($item, $index, $widget){
$classes = '';
$classes .= ($item['attribute'] == 'your-attr' && $item['value'] == 'your-value') ? 'hidden' : '';
return "<tr class='$classes'><th>$item[attribute]</th><td>$item[value]</td></tr>";
}
]) ?>
If you know you will not use or even need those hidden values you can also dinamically determine what attributes you pass to DetailView like:
<?php
// your logic to know what attributes display
echo DetailView::widget([
'model' => $model,
'attributes' => $attributes
]) ?>
You can use kartik's GridView and the bootstrap classes hidden and show. See Bootstrap docs.
use kartik\detail\DetailView;
....
[
'attribute' => $attribute,
'format' => 'raw',
'value' => Yii::$app->formatter->asDatetime($model->$attribute).' par '.Yii::$app->myFormatter->asUser($model->visited_by),
'rowOptions' => [
'class' => ($i_want_to_see_it ? 'show' : 'hidden'),
],
];

Select2 Krajee widget - using modal and getting the id of the value

What is the way to get the value of the select item with modal. As it is shown in demo example (Using a select2 widget inside a modal dialog):
Modal::begin([
'options' => [
'id' => 'kartik-modal',
'tabindex' => false // important for Select2 to work properly
],
'header' => '<h4 style="margin:0; padding:0">Select2 Inside Modal</h4>',
'toggleButton' => ['label' => 'Show Modal', 'class' => 'btn btn-lg btn-primary'],
]);
echo Select2::widget([
'name' => 'state_40',
'data' => $data,
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
]);
Modal::end();
I get the options to be shown and select, but I need id of the selected value to be saved somewhere so I can get it to be forwarded as part of the link in button.
For example:
Html::button('Example', ['value' => Url::to(['example/example', 'id' => ?]),'class' => 'btn btn-lg btn-primary ']);
this is a easy way. I´ve done it many times.
'options' => [
'class'=>'col-md-3','id'=>'id_select',
'placeholder' => 'Select the country',
],
with jquery :
var id_select = $('#id_select').val();
Or if you prefer ajax:
echo Html::a('<i class="fa fa-check"></i> Send',null,[
'class' => 'btn btn-primary',
'title' => Yii::t('yii', 'Enviar'),
'onclick'=>"
var id_select = $('#id_select').val();
$.ajax({
type:'post',
cache : false,
url : '".Url::toRoute(['example'])."&id='+id_select,
success : function(response) {
//ok
},
error: function() {
alert('Error');
}
});
]);
you can´t get all parameters with php, you need client languaje for get this.
However I´d invite you to use <form> with model, is more easy and fast.

yii2 kartik export menu - No results found

Using karktik export menu only. Why do the following exports an excel file with no results found.. I am sure that there are records inside the TblDv model. But in the excel it says no records found.
-in the controller
public function actionExport(){
$provider = new ActiveDataProvider([
'query' => TblDv::find(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('export', [
'dataProvider' => $provider,
]);
}
-the view
<?php
use kartik\export\ExportMenu;
use kartik\grid\GridView;
use kartik\helpers\Html;
$gridColumns = [
'id',
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'fontAwesome' => true,
]);
?>
I dont know why but it worked when I changed the gridcolumns to this..
$gridColumns=[
['class' => 'yii\grid\SerialColumn'],
'id',
];

yii2 Pjax + java script prompt

It there any way to make 'data-confirm' => Please enter the number' not just confirm but some like JS prompt and get imputed values to sent it to controller?
<?php \yii\widgets\Pjax::begin(['id' => 'pjax-orders_table','clientOptions' => ['method' => 'POST'], 'enablePushState'=>false]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'id'=>'orders_table',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
///some columns
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}{some}',
'buttons' => [
'some' => function ($url,$model,$key) {
if($model->status=='not confirm')
{
return Html::a('<span class="glyphicon glyphicon-trash"</span>',['my/some', 'id' => $model->id],[
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Please enter the number',
'data-method' => 'post',
]);
}
},
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end() ?>
In controller
public actionSome()
{ $dataProvider = new ActiveDataProvider();
$dataProvider->query = Orders::find()->all();
return $this->render('some',['dataProvider'=>$dataProvider]);
}
instead of Html::a() use Html::button()where button id = some_item_id and then write this JS code
$('.buttons_class').click(function(){
var selection;
do{
selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}
while(isNaN(selection));
if(!isNaN(selection))
{
$.post("some",{id:45,prompt_value:selection}, function(response)
{
console.log(response);
$.pjax.reload({container:'#pjax-orders_table'});
});
}
})