YII Select2 Dropdown - drop-down-menu

I have a code where I want to display the countries data along with the country code. The dropdown must contain both the name and the code. I want that whenever a user selects a country the select bar must display only the code, not the name. for example if a user is from Canada the select bar should display +1. I am using select2 widget in yii2 for dropdown; Here is my code:
$countriesIsdData = ArrayHelper::map($countries_isd_codes, 'countries_isd_code','countries_name');
echo $form->field($model,'country_code')->widget(Select2::classname(),
[
'data' => $countriesIsdData,
'value'=>$count,
'options' => [
'id'=>'country_cod',
'placeholder'=>'Select Country Code',
'prompt' => 'Select Country Code','value'=>$count
],
'pluginOptions' => [
'allowClear' => true,
'dropdownCssClass'=>'bigdrop',
'value'=>$count
]
])->label('Country Code');

Related

Show/hide fields based on value of another field

I have a scenario where a field should be shown as dropdown or text field based on the value of another field.
Example:
User to select State as dropdown (California, North Carolina, etc) if the selected country is USA.
If any other country is selected (other than USA), user should be shown only a editable text field (instead of dropdown).
Please let me know if there is a way to achieve this using backpack's out-of-the box features. If not, is there any workaround possible?
My code snippet:
COUNTRY FIELD FROM DB:
CRUD::addField([
// 1-n relationship
'name' => 'countryid', // the column that contains the ID of that connected entity;
'label' => 'Country', // Table column heading
'type' => 'select2',
'entity' => 'getcountryid', // the method that defines the relationship in your Model
// optional
//'attribute' => 'name', // foreign key attribute that is shown to user
'model' => "App\Models\Country", // foreign key model
]);
STATE FIELD WHICH DEPENDS ON COUNTRY FIELD
(THIS SHOULD BE MADE AS A TEXT FILED OR DROPDOWN BASED ON THE COUNTRY SELECTED):
CRUD::addField([ // select2_from_ajax: 1-n relationship
'label' => "State", // Table column heading
'type' => 'select2_from_ajax',
'name' => 'countrystateid', // the column that contains the ID of that connected entity;
'entity' => 'countrystate', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'data_source' => url('/api/countrystate'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select an Indian state', // placeholder for the select
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['countryid'], // when a dependency changes, this select2 is reset to null
//'method' => 'GET', // optional - HTTP method to use for the AJAX call ]); (GET, POST)
]);

how to concatenate two columns in yii2 activerecord?

I am new to developing in yii2 (and yii). I have a query that returns name and last name of customers along with other columns. I am using activeDataProvider to display the records in a table but I need the name and lastname be displayed in one table column.
What I want to know is:
1. how do I concatenate the columns in yii2 activerecord?
2. How do I display the columns in one table columns made gridView?
below is what I have tried that none works:
1. $query->select(['customer.*', 'fullname'=>concat('customer.name'," ",'customer.lastname')]);
2. 'dataProvider' => $model['dataProvider'],
'columns' => [
'id',
'name'.' '.'lastname',
'customer_orders',
'created_at:datetime'
Use GridView like below.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute' => 'name',
'value' => function ($model) {
return $model->name . ' ' . $model->lastname;
}
]
'customer_orders',
'created_at:datetime'
]
]) ?>
Refer Yii2 GridView

How to add multi select value in database in October CMS Backend?

Not inserted multi select value in database.
My fields.yaml Code is :
related_recipe:
label: 'Related Recipe'
span: auto
nameFrom: recipe_title
descriptionFrom: description
attributes: {multiple:'multiple'}
type: relation
My model code is :
public $belongsTo = [
'related_recipe' => [
'Qdata\Taeq\Models\Recipe',
'conditions' => 'status = 1'
],
];
Currently only one selected value inserted in database.need add multiple value in database. Can any one have the solution of it ?
You should use a $belongsToMany relation in this case.
$belongsTo means your model is link with a single entity of your Recipe model.
To do so with relation you need to go with "belongsToMany" relation. for example:
In your Model
'related_recipes' => [
'Qdata\Taeq\Models\Recipe',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
In related another Model
'makers' => [
'Qdata\Taeq\Models\AnotherModel',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
this will save your multiselect dropdown data in the related pivot table in your database.

How to set filter in grid of admin in magento?

I am working on magento 1.7 version.
I have a grid in magento admin. When I click on a particular row it opens a form and two tabs in left sidebar.
when I click on one tab it is displaying a grid in right side.
Now I want that in this grid a filter should be auto selected.
Ex.-
http://d.pr/i/UuB4
http://d.pr/i/BN1N
In this, category should be auto selected in filter and how to get current row id in tabs.php in _beforeToHtml().
I am using following code for tabs
protected function _beforeToHtml() {
$this->addTab('form_section', array(
'label' => Mage::helper('test')->__('Category'),
'title' => Mage::helper('test')->__('Category'),
'content' => $this->getLayout()->createBlock('test/adminhtml_category_edit_tab_form')->toHtml(),
));
$this->addTab('tab_section', array(
'label' => Mage::helper('test')->__('Images'),
'title' => Mage::helper('test')->__('Images'),
'content' => $this->getLayout()->createBlock('test/adminhtml_book_grid')
->toHtml()
));
return parent::_beforeToHtml();
}
any help would be much appreciated.
you can use this code to set the filter value -
$this->setDefaultFilter(array('category'=>3));
where 3 - category id in your grid's _prepareCollection() method.

Magento Admin formfield multiselect selected

I´m currently developing a custom module for magento thats going to list employees.
I have figured out almost everything. The only thing I got left is how the selected values is going to be highlighted.
The problem I´m having is for the backend.
I got 2 tabs per employee, one for employee data and one tab for magento categories.
1 employee can have 1 or more categories.
The database table that the categories are stored in are a non-eav table.
So my question is
What in a multiselect determines which values are selected? As it is now, only one value is selected.
I think you can do this by simply passing in an array of the id's to be selected into the 'value' attribute of the field being added for the multiselect in the _prepareForm() method. Something like the following.
$fieldset->addField('category_id', 'multiselect', array(
'name' => 'categories[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('mymodule/mymodel')->getMymodelValuesForForm(),
'value' => array(1,7,10),
));
The id of the form element (e.g. category_id) must not be an attribute in your model, otherwise when the form values get set with $form->setValues() later on, the attribute value will be overwritten.
I normally store multiple selections as a text column separated by commas much like most magento modules handles stores which requires a slightly different approach as shown below.
In the form block for the tab with the multiselect, you firstly define the element to be displayed like so in the _prepareForm() method. You then get the values from the model and set put them into the form data.
protected function _prepareForm()
{
...
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
...
if ( Mage::getSingleton('adminhtml/session')->getMymodelData() )
{
$data = Mage::getSingleton('adminhtml/session')->getMymodelData();
} elseif ( Mage::registry('mymodel_data') ) {
$data = Mage::registry('mymodel_data')->getData();
}
$data['store_id'] = isset($data['stores']) ? explode(',', $data['stores']) : array();
$form->setValues($data);
}
I normally store the selected stores (categories as in your case) in the main model as a text column and comma separated values of ids, hence the explode.
In the controller for for the edit action, I put the model being edited into the mage registry so we can load it and it's values in the step above.
Mage::register('mymodel_data', $model);
Thanks for answering.
This is how my field looks like:
$fieldset->addField('npn_CatID', 'multiselect', array(
'label' => Mage::helper('employeelist')->__('Kategori'),
'class' => 'required-entry',
'required' => true,
'name' => 'npn_CatID',
'values' => $data,
'value' => array(3,5)
));
npn_CatID is the value in my db where the category id is saved.
I have tried to change the name and field ID but cant get it working.
When its the field id is like above ONE value is selected and its the last one inserted for the chosen employee
My data array looks likes
array(array('value' => '1', 'label' => 'USB'), array('value' => '2', 'label' => 'Memories'))

Resources