Codeigniter form helper dropdown - php

I want to add both an ID and NAME for my drop down (so that I could get the value using jQuery.
but it seems that CI won't allow. Is there another way?
$device = array(
'name' => 'device',
'id' => 'device'
);
$device_opts = array(
'monitor' => 'Chair',
'keyboard' => 'Keyboard',
'mouse' => 'Mouse',
'table' => 'table',
'UPS' => 'UPS',
);
echo form_dropdown($device, $device_opts);

try this
echo form_dropdown("device", $device_opts,"","id='device'");

Try like
$device = array(
'id' => 'device'
);
form_dropdown('device', $device_opts, '', $device);
You need to give name as first option for the DropDown and the remaining will come under extra options for that DropDown

Related

PrestaShop FormHelper preload multilanguage values

here I am seeing a small problem, I have two tables template and template_text, in template table I store the template type and visibility data and in the template_text table I store the template text in many languages, atm. I have two langs. such as EN and DE. So, when I am editing the template I preload the current template values into the form, but there is a problem with the values like autoemailer_title and autoemailer_body, when the from helper generates the form field it is generating this fields this way autoemailer_title_1, autoemailer_title_2 according the language ID, so, the question would be how to preload all this values the right way? I could use javascript and ajax get request to get the values and then set them by using the input ID, but I think there is a better way using the form helper.
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l($params['title']),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select', // This is a <select> tag.
'label' => $this->l('Template type'), // The <label> for this <select> tag.
'desc' => $this->l('Choose a template type'), // A help text, displayed right next to the <select> tag.
'name' => 'autoemailer_type', // The content of the 'id' attribute of the <select> tag.
'required' => true, // If set to true, this option must be set.
'options' => array(
'query' => array(
array(
'id_option' => 'static', // The value of the 'value' attribute of the <option> tag.
'name' => 'Static' // The value of the text content of the <option> tag.
),
array(
'id_option' => 'dynamic',
'name' => 'Dynamic'
),
), // $options contains the data itself.
'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
)
),
array(
'type' => 'text',
'label' => $this->l('News letter title'),
'name' => 'autoemailer_title',
'desc' => $this->l('Title'),
'lang' => true,
// 'style' => 'width:300px',
'class' => 'fixed-width-lg',
),
array(
'type' => 'textarea',
'label' => $this->l('Text'),
'name' => 'autoemailer_body',
'desc' => $this->l('Email body'),
'lang' => true,
'cols' => 60,
'rows' => 10,
'class' => 'rte', // we need this for setuping the tiny mce editor
'autoload_rte' => true, // we need this for setuping the tiny mce editor
),
),
'submit' => array(
'title' => $this->l('Save')
)
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'newTemplate';
$helper->currentIndex = $this->getThisUrl() . '&emailer_action=main';
$helper->token = Tools::getAdminTokenLite('AdminModules');
return $helper->generateForm(array($fields_form));
Solved this one. So, when we have more than one lang then the attribute for which we apply the multilangiage option becomes an array, so to set the value according the language we can just do this way :
$lang_id = 1; // setting the language ID, usually it's done by foreaching the records from ps_lang table
$helper->fields_value['description'][$lang_id] = 'my default value'; // setting the default value
return $helper->generateForm(array($fields_form)); // returning the output of generated form

Jquery attr doesnt work on Yii

I'm trying to change the element value using Jquery but it's not working...
This is my widget where I have my element 'tag' which i want to change it to textField on edit...
$this->widget('EditableGrid', array(
'dataProvider' => $dataProvider->searchbyID($invoice_id),
'template' => '{items}{buttonCreateRow}{pager} ',
'id' => 'InvoiceLine-grid',
'rowTemplate' => $row_template,
'columns' => array(
array(
'class' => 'EditableGridColumn',
'header' => '',
'name' => 'InvoiceLine_{gridNum}_{rowNum}_edit',
'imageurl'=> Yii::app()->request->baseUrl.'/images/update.png',
'tag' => 'button',
'tagHtmlOptions' => array(
'onclick'=>'editline(this)',
)
),
array(
'class' => 'EditableGridColumn',
'header' => 'StentysRef',
'name' => '[{gridNum}][{rowNum}]stentysproductref',
'tag' => 'laabel',
'tagHtmlOptions' => array(
'style'=>'background-color:#FFF;border-color:#FFF',
'onkeyup'=>'stentysref(this)',
'readonly'=>true
)
),
My Jquery is,
(as you can see the removeAttr works but attr doesn't)
function editline(obj){
$("#InvoiceLine_1_"+row+"_stentysproductref").attr("tag","textField");
$("#InvoiceLine_1_"+row+"_stentysproductref").removeAttr("readonly");
}
Use .prop() and removeProp() which is added in jQuery 1.6, as the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.
function editline(obj){
$("#InvoiceLine_1_"+row+"_stentysproductref").prop("tag","textField");
$("#InvoiceLine_1_"+row+"_stentysproductref").removeProp("readonly");
}
API Doc for .prop()
If you are using EditableGrid widget without pointing AR model, then the row id looks like
$('#_1_'+row+'stentysproductref').
I'll take it into consideration for the future updates. Thank you.

how to display array to CGridView (yii framework)

I have next variables:
$type_model = ProductTypeModel::model()->findByPk($id);
$prod = $type_model->product;
Now in $prod:
array
(
0 => ProductModel#1
(
[CActiveRecord:_new] => false
[CActiveRecord:_attributes] => array
(
'product_id' => '6'
'product_type_id' => '5'
)
...
)
1 => ProductModel#2
(
'product_id' => '8'
'product_type_id' => '5'
)
...
How i can display my products in CGridView?
Thx.
I Suppose you are using CarrayDataProvider. So in your controller
$dataProvider = new CArrayDataProvider($prod);
Here $product could be any array you want to display in CgridView. Now
In you view write this.
$gridColumns = array(
array(
'header' => 'First Name',
'value' => 'ProductTypeModel::model()->findByPk($data->product_id)->key',
'htmlOptions' => array('style' => 'text-align:center;')
),
$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider,));
As in CarrayDataprovider array is obtained so we cant use relations in it. Thats why u have to write 'ProductTypeModel::model()->findByPk($data->product_id)->key'
Here you can display anything attribute of ProductTypeModel so u can replace above mentioned key with your desired attribute
Try this ...
it automatic convert to array data provider.
$dataProvider=new CArrayDataProvider($type_model->product);
Thanks all. By means of answers "naveen goyal" and "jailed abroad" i did like this:
$dataProvider=new CArrayDataProvider($type_model->product);
$dataProvider->keyField = 'product_id';
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'Title',
'value' => 'CHtml::encode($data["product_title"])',
),
)));
Nice work for me.

Conditionally adding item to multidimensional array

I have been looking how to do this and am a bit stumped.
My array is as follows:
$returndata->setup_array = array(
'General' => array(
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.
I want/need it to work like this:
$returndata->setup_array = array(
'General' => array(
$viewdets
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
$viewdets = "'View Details' => 'setup/viewdetails'";
and still be interpreted as a functioning array for use as a menu.
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
Cheers Rick!
You can use ArrayObject to have the array as a reference:
$a = new ArrayObject();
$b = array(
"a" => $a
);
$a[] = "foo";
print_r($b);
What did you try calling array_push() on? Have you tried
array_push($returndata->setup_array['General'], $viewdets);
You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:
$returndata->setup_array['General'][] = $viewdets;

Zend Framework - Set 'selected' value in select box dropdown list

I am adding a select element to a Zend_Form instance as follows:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
{
if($selected == $u->id)
{
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
//*** some way of setting a selected option? selected="selected"
}
else
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
}
I have been searching the docs but cannot find a simple way of pre-setting an option of the select element to 'selected'.
I've just worked out how to do it.
You have to use the setValue() method of the element:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
$user->setValue($selected); //$selected is the 'value' of the <option> that you want to apply selected="selected" to.
$form->addElement('select','foo',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'multiOptions' => array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
),
)
);
As above, you can use 'value' => 'blue' for making 'blue' => 'Bleu' selected.
I hope this will help you..
In Zend Framework 2 set the 'value' attribue. For example to default the Select to 'Yes':
$this->add( array(
'name' => 'isFlexible',
'type' => 'Select',
'options' => array(
'label' => 'Is it flexible?'
,'label_attributes' => array( 'placement' => 'APPEND')
,'value_options' => array(
'' => 'Select Below',
'0' => 'No',
'1' => 'Yes',
'2' => 'N/A',
),
),
'attributes' => array(
'id' => 'is_flexible',
'value' => 1,
),
));
i think this should work:
$form->setDefault('user', 'value'); // Set default value for element
To set default values you can try both setDefault or populate
$form->populate( $array_keypair_values );
I just try following code to show drop-down value as selected from controller and it work fine.
$user->setValue($value); //$value is the 'value' of the and $user is the element of from.
The mentioned solutions won't work for Zend Framework 2, For those who use Zf2, I suggest using the following instruction to set a default value:
$formX->get('<Select element Name>')->setValue(<the id of the selected item>);

Categories