I am using cakephp3 and select2 dropdown script.
My call to the data looks like this
$roles = $this->ParticipantsProjectsRoles->Roles->find('list', [
'keyField' => 'id',
'valueField' => 'description'
]);
within my view I call this
<?=$this->Form->input('role_id', ['options' => $roles, 'label' => false, 'class' => 'form-control select2me']);?>
The output HTML will load always the first data entry into the select.
Is there a way to have the first value always empty?
Set the empty key in the options array to true or another value, e.g. Select Role:
$this->Form->input(
'role_id', [
'options' => $roles,
'label' => false,
'class' => 'form-control select2me',
'empty' => true
]
);
Related
echo $this->Form->input('product_id', array(
'label'=>false,
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$product,
));
I am trying to adding 'checked=>true' in from input but failed
this is screenshoot of the form edit, the data already selected is not checked
You can tell CakePHP which checkboxes are checked by passing an array of selected values:-
$selected = []; // An array of selected values
echo $this->Form->input('product_id', array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $product,
'selected' => $selected
));
If you wanted to select all the values to start with you could pass the array keys of $product to the selected option:-
$selected = array_keys($product);
echo $this->Form->input('product_id', array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $product,
'selected' => $selected
));
i am using below code:-
echo $this->Form->input('my_radio',array(
'label' =>false,
'type' => 'select',
'multiple' => 'checkbox',
'class'=>'checkbox12',
'div'=>false,
'selected' => array_keys($sub_cat_name1),
'options' => $parent_cat_detail1
));
i did false the div but still the 'checkbox12' is adding to div.
the problem is not in the class , is in the mutiple value , you should put it true not checkbox :
echo $this->Form->input('my_radio',array(
'label' =>false,
'type' => 'select',
'multiple' => true,
'class'=>'checkbox12',
'selected' => array_keys($sub_cat_name1),
'options' => $parent_cat_detail1
));
I add the checkbox functionality from the yii-booster. But the widget renders the model view without the needed boxes. What's wrong?
Widjet code in view
<?php
$this->widget('bootstrap.widgets.TbExtendedGridView',array(
'id'=>'docs-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'type'=>'bordered condensed',
'template' => "{items}",
'bulkActions' => array(
'actionButtons' => array(
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Choose',
'click' => 'js:function(values){console.log(values);}'
)
),
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
'checkBoxColumnConfig' => array(
'name' => 'id'
),
),
));
If you are using bulkActions, you have to use 'columns' to list out the columns you want to display instead of using 'template'.
'columns' => array(
'id',
'title',
...
),
the problem has been in the wrong hierarchy from the example:
The 'checkBoxColumnConfig' attribute must be outside of the 'actionButtons' attribute:
'bulkActions' => array(
'actionButtons' => array(
/*array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
),*/
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
),
'checkBoxColumnConfig' => array(
'name' => 'id'
),
...
));
but now the widget doesn't work when i uncomment the array part inside 'actionButtons':
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
what might be a cause?
I want to add 2 callback to Js->submit, My first return true or false. Continuing with that I want to put action. I mean If it is true then it continue to action else halt at that moment.
What I have tried yet but its not taking 2 callback. Plaese help.
echo $this->Js->submit("Apply",
array(
'id' => 'submitForm', 'div' => false, 'class' => 'general_button', 'style' => array('float:none;', 'margin: 10px;'),
'url' => array(
'controller' => 'categories', 'action' => 'index', 'field' => $search_term, 'value' => $search_value, 'parentId' => $parentId
),
'update' => '#listID',
'confirm' => 'Are you sure you want to apply action to selected records ??',
'before' => $this->Js->event('click', 'return checkboxCount();', false),
'before' => $this->Js->get('#loaderID')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#loaderID')->effect('fadeOut', array('buffer' => false)),
)
);
How will I continue with it. As function return checkboxCount(); returns true or false. but in here case my function is not even called.
How can I add additional attributes to my select menu option tags? Like this:
<select class="test" name="data[Test][test]">
<option value="1" data-price="100">My Option</option>
</select>
How do I add the data-price="100" ?
I tried something like this but it didn't work:
<?php
echo $this->Form->select('test', $options, null, array(
'class' => 'test',
'options' => array(
'data-price' => 100
)
));
?>
check this out:
http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/
"Setting additional attributes for some select options"
you can try this
echo $this->Form->input('test', array(
'options' => array(
1=>array(
'data-price' => 100,
'value' => '1',
'name' => 'My Option'
)),'class' => 'test')
);
You can do it this way:
$options = array(
...
array('name' => 'United states', 'value' => 'USA', 'title' => 'the title that you want', 'class' => 'something'),
array('name' => 'USA', 'value' => 'USA', 'title' => 'the other title that you want', 'class' => 'otherthing'),
);
echo $this->Form->input('test', array('type'=>'select', 'options'=>$options));
You have to manually build the select HTML
also you can refer How to give select tag an attribute in cake php?
In cakephp 4
look for documentation
Controller
$countries = $this->Countries->find('all')->where([
'active' => 1
]);
$options = $examples->map(function ($value, $key) {
return [
'value' => $value->id,
'text' => $value->name,
'data-flag' => $value->iso_code
];
});
In view/template
<?= $this->Form->control('country_residence_id', [
'label' => false,
'options' => $countries,
'class' => 'form-control select2-flag-search',
'data-placeholde' => __('Select Country')
]) ?>