I use Pjax library for Yii2 to refresh data without reloading page.
<?php \yii\widgets\Pjax::begin(['id' => 'some-id-you-like',
'timeout' => false,
'enablePushState' => false,
'clientOptions' => ['method' => 'POST']]);
echo GridView::widget([
....
]);
\yii\widgets\Pjax::end(); ?>
Everything is ok. But i want to make pagination works with GET request (I want to see ?page= in url ).
Any advice.
Set 'enablePushState' => true or remove it (default value true).
Related
I have a yii2 activeform where the functionality of the form can change based on other things within the form. So, I have a clubs field, that can be multiple in some instances but not multiple in others.
<?= $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'data' => $club_data,
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
])->label('Club(s)'); ?>
I need to programmatically change the multiple pluginOption to true and false. This should be when the user goes into the form but also immediately when a dropdown is changed on the form. I can do it when the user initially goes into the form but not immediately when another dropdown is changed.
I've made separate fields, one actually linked to a field in the database and another not, this kind of works but it's far from elegant!
I've looked in both the kartik select2 documentation and the standard jquery select2 documentation and I can't spot anything. Any help or pointers would be much appreciated.
Reference
I was working on a Yii2-formwizard plugin a few months back where I had a situation of providing tabular functionality and cloning the fields really became the pain in the ass when it came to using 3rd party plugins like Select2 and Datepicker and I figured out the following code along with a lot of other (as the other part isn't relevant to your problem so not adding it).
Approach to the problem
The way Kartik-select2 works it stores the select2 plugin-specific options in the data-krajee-select2 attribute of the element you are using the select2 on.
And the theme or say the Kartik's plugin-specific options like theme and others are saved in the variable whose name is saved in the data-s2-options attribute. You can look for both of them in view-source of the page where you are using it.
Solution
So what you need to do is to
Steps
Get the applied options
Add/Update the option
Apply back the options
As you just added a single field and not the other one on who's selection it would change, I would demonstrate an example where you can change the behavior of the select2 from multiple to single using 2 different buttons, clicking on the relevant button the select2 will work accordingly. You can adjust the javascript code where ever you want to.
But before I add any code you need to fix one thing, you don't need the main data option when you are using the ajax option inside the plugin options. In other words, it does not have any effect on your select2 and is adding page load time only you should remove it.
You select2 should look like below, I added id to the options of the select2 for the example you can change it to the active form formatted name like model-field_name format if you like, but don't forget to change the id in the javascript code too
<?php echo $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)','id'=>'clubs'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
]
]
])->label('Club(s)');?>
Add the following buttons on your page
echo Html::button('multiple', ['class' => 'btn btn-info', 'id' => 'multi']);
echo Html::button('single', ['class' => 'btn btn-info', 'id' => 'single']);
And now the magic thing, add it on the top of your view
$js = <<<JS
var element=$("#clubs");
$('#multi').on('click',function(e){
//reset select2 values if previously selected
element.val(null).trigger('change');
//get plugin options
let dataSelect = eval(element.data('krajee-select2'));
//get kartik-select2 options
let krajeeOptions = element.data('s2-options');
//add your options
dataSelect.multiple=true;
//apply select2 options and load select2 again
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
$('#single').on('click',function(e){
element.val(null).trigger('change');
let dataSelect = eval(element.data('krajee-select2'));
let krajeeOptions = element.data('s2-options');
dataSelect.multiple=false;
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
Now if your select2 is working correctly click on the multiple button and you will see the multiple selections are enabled for the select2 and when clicked on the single button.
Hope it helps.
I am trying this code but does not work.
$form=ActiveForm::begin(['id'=>
'id' => 'login-form',
'options' => ['class'=>'darkBg','enableClientValidation'=>false,'validateOnBlur' => false],
]); ?>
<?= $form->field($model, 'email')->textInput(['class'=>'form-control','type'=>'text','enableClientValidation'=>false,])->label(false) ?>
I wanted to disable Yii2 default error msg.
You just have to add
->error(false)
This way no text is being shown, but field still gets marked red on failure.
Hi i am using kartik select2 in my yii application. The select box for selecting multiple options is working fine. However i am facing one small issue.
Like if i type ba then it will display all matching options. but when i press enter then Bakam get selected but the text ba remains in select box. It should get removed.
Here is code for my select box
<?= $form->field($model, 'area')->widget(Select2::classname(), [
'data' => [],
'options' => ['placeholder' => 'Select Location'],
'pluginOptions' => [
'closeOnSelect' => false,
'tags' => false,
'multiple' => true,
],
])->label("Reassign Location")
?>
I checked it on kartik website the code seems fine then why the text remains there?
Set 'closeOnSelect' => true, in pluginOptions to remove selected option.
I have a typical Yii2 form for updating my model with a typical submit button. Next to it, I have a "Delete photo" button, that appears, if there is any photo to delete. The piece of view looks like that:
<?= Html::submitButton('Save changes', ['class' => 'btn btn-primary', 'name' => 'edit-button']) ?>
<?php $image = isset($page->photo) ? $page->photo->getImageUrl() : null; ?>
<?php if (isset($image)): ?>
<?= Html::a('Delete photo', ['delete-image', 'lab' => $lab->id, 'kind' => $page->kind], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Do you really want to delete this photo?',
'method' => 'post'
],
]) ?>
<?php endif; ?>
When there is a photo attached to this model and these two buttons appear next to each other, I must comment out 'method' => 'post' part in second button code. Because, if I don't this this, the second button is... submitting the form (just like the first one) instead of calling lab/delete-image route.
This is the first thing, that I don't understand. The entire code is either generated by Gii or copy-pasted from some Yii tutorials. Not even a bit of my invention and yet it works strangely. What am I missing?
It seems, that normal Html::a link (only styled by Twitter Bootstrap to look like a button, but not being a button at all) is submitting a form, instead of calling its action, when it contains data-method="post" attribute in element code. Is this a bug in Yii2 or am I missing something?
You need to place the link outside of the form. For calling actions from elements with data-method attribute yii has js function handleAction, and its documentation says:
This method recognizes the data-method attribute of the element. If the attribute exists, the method will submit the form containing this element. If there is no containing form, a form will be created and submitted using the method given by this attribute value (e.g. "post", "put").
For hyperlinks, the form action will take the value of the "href" attribute of the link.
Also if you use yii2 v2.0.3 or higher you can add data-params attribute which value should be JSON representation of the data, and this data will be submitted in request. As example:
echo Html::a('Delete image', ['delete-image'], [
'data' => [
'confirm' => 'Do you really want to delete this photo?'
'method' => 'post',
'params' => [
'lab' => $lab->id,
'kind' => $page->kind,
],
],
]);
In this example params array will be json encoded by yii2 internaly
I used EdataTable extension in my yii Project. I have customized the yii datatable emptyText value in EDataTable.php file. I used dynamic error message when data empty. but this message not reflected in ajax requests.
I initialized Yii datatable widget in my file.
myfile.php:
$widget=$this->createWidget('ext.EDataTables.EDataTables', array(
'id' => 'placement',
'dataProvider' => $arrayDataProvider,
'TotalRowCount' => $TotalRowCount,
'emptyMessage' => $errorMessage,
'ajaxUrl' => Yii::app()->getBaseUrl(),
'exportable' => TRUE,
'columns' => $columns,
'bootstrap' => TRUE,
));
In EDataTable.php:
"sEmptyTable" => $this->emptyMessage
this emptyMessage value not worked on ajax requests like data table sorting, searching etc. New error Message not bind to edatatable.
But it works fine when first time pages loads.
In Yii How to change the emptyText value for ajax calls?