I am using kartik select2 library. I have the modal which is loading in the page from ajax with renderAjax. The modal has the select2 dropdown.
The problem is it load the js but do not load the css files of select2.
echo $form->field($assign_model, 'applicant_id')->widget(Select2::classname(), [
'data' => $applicant,
'attribute' => 'applicant_id',
'options' => ['placeholder' => 'Select an applicant', 'multiple' => 'multiple', 'style' => "width:100%"],
'pluginEvents' => [
"select2:selecting" => "function() { "
. "no_position = $('body').data('no_position');"
. "if(no_position>= " . $model->no_of_persons . "){alert('You can select only " . $model->no_of_persons . " applicant(s)');return false;} }",
"select2:select" => "function() { "
. "no_position = $('body').data('no_position');"
. "$('body').data('no_position',++no_position);}",
"select2:unselect" => "function() { "
. "no_position = $('body').data('no_position');"
. "$('body').data('no_position',--no_position);}",
],
'pluginOptions' => [
'allowClear' => true,
]
]);
Related
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 );
}',
]
]);
?>
I want that a text in gridview is link that sends this text to filter of the same column.
so far I'm doing this way:
'columns'=>[
...
[
'attribute'=>'colname',
'value'=>function($data){
return Html::a($data->colname,Yii::$app->request->url.'&MymodelSearch[colname]='.$data->colname);
},
],
...
]
but it's ugly and doesn't always work
'columns' => [
// ...
[
'attribute' => 'colname',
'format' => 'raw',
'value' => function ($data, $key, $index, $column) {
if ($data->colname)
return
"<span onclick=\""
. (new \yii\web\JsExpression("setFilterColname('"
. Html::encode($data->colname) . "');"))
. "\">"
. \yii\helpers\Html::encode($data->colname)
. "</span>";
}
// ...
]
Add this at bottom of view file
<?php
$this->registerJs("
function setFilterColname(filter_value) {
$('input[name=\"MymodelSearch[colname]\"]').val(filter_value);
$('#w0').yiiGridView('applyFilter');
// #w0 is ID of grid to be submited to filter
}
", $this::POS_END, 'set-filter-colname');
?>
I have got this error while using the code below in DetailView of Yii 2.
Object of class Closure could not be converted to string
The code is:
[
'format' => 'raw',
'attribute' => 'title',
'value' => function($model1, $key) {
if ($model1->book->language == 1) {
$m = "<p class='n'>" . $model1->book->title . "</p>";
} else {
$m = $model1->book->title;
}
return $m;
},
'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center']
],
Can you guys help me?
DetailView does not take closure like GridView for value, just string. Change it to:
'value' => $model1->book->language == 1
? "<p class='n'>" . $model1->book->title . "</p>"
: $model1->book->title,
I have used use kartik\file\FileInput; (extension) for saving multiple images from single form submit.
The images are save locally but are not saving in the database.
Here is my Model Code media.php
namespace app\models;
use yii\web\UploadedFile;
class Media extends \yii\db\ActiveRecord
{
public function rules(){
return [
[['title'], 'file', 'skipOnEmpty' => false, 'extensions' => ['gif', 'jpg', 'png', 'jpeg', 'JPG', 'JPEG', 'PNG', 'GIF'], 'checkExtensionByMimeType' => false ,'maxFiles' => 4, 'maxSize' => 1024 * 1024 * 1024],
[['extension'], 'string', 'max' => 6],
];
}
Controller code:
if ($mediamodel->load ( Yii::$app->request->post () )) {
$title = UploadedFile::getInstances ( $mediamodel, 'title' );
foreach ($title as $key => $file) {
$file->saveAs(Yii::$app->basePath . '/web/images/hotel/'. $file->baseName . '.' . $file->extension);}
foreach ($title as $key => $file){
echo $mediamodel->title."*********";
$mediamodel->title = $file->baseName . '.' . $file->extension;
echo " \Title: ".$mediamodel->title;
$mediamodel->save ();
}
}
My view code:
use kartik\file\FileInput;
$form = ActiveForm::begin([ 'layout' => 'horizontal', {label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'fieldConfig' => ['horizontalCssClasses' => ['label' => 'col-md-3','offset' => 'col-md-offset-2','wrapper' => 'col-md-4', 'error' => '','hint' => '',],],'
options' => [ 'class' => 'form-horizontal', 'enctype' => 'multipart/form-data', ], ]);?>
<?php echo $form->field($mediamodel, 'title[]')->widget(FileInput::classname(), ['options'=>['multiple' => true]]);
Html::submitButton($model->isNewRecord ? 'Add' : 'Update');
use
$mediamodel->save (false);
instead of
$mediamodel->save ();
// You should handle errors like this.
if(!$mediamodel->save()){
// handle the errors of model.
var_dump($mediamodel->getErrors());
}
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'});
});
}
})