I am generating options for my dropdown using
// view.php
foreach ($plants as $row):
$options[$row->plant_id] = $row->plant_name;
endforeach;
and then lower in the HTML part of view.php
//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);
The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.
The HTML source also shows 'Select' in form_dropdown was ignored.
I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.
Any idea what is going on here or how to solve this issue?
You need to have the default option in your $options array...
So before your foreach, do:
$options = array('Select');
I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.
If you wanted to explicitly set this value as the default you would pass 0 as the default argument.
Related
I am working in gravityforms to pre-populate a form with database values dynamically, which is working. I need to be able to specity which of these options is selected by default when the form is built, but I can't find the option to do so. I have seen the placeholder, but I presume this doesnt actualy select anything on the dropdown. I have been unable to find any docs or references that allow me to set the "selected" options once I have built all the text/value pairs in the array and set the choices.
The function (which I have redacted here) works fine and populates the field, I just need to populate a placeholder and/or default selected value.
add_filter('gform_pre_render_1', 'getFieldValues');
add_filter('gform_pre_validation_1', 'getFieldValues');
add_filter('gform_pre_submission_filter_1', 'getFieldValues');
function getFieldValues($form) {
global $wpdb;
foreach ($form['fields'] as $field) {
if ($field->id == '40') {
// get the list of values from the Database
$list1Q = "SELECT <data> FROM <table> WHERE <params> = <value>";
$list1R = $wpdb->get_results($list1Q);
// Generate a nice array that Gravity Forms can understand
if (!empty($list1R)) {
$list1A[] = array();
foreach ($list1A as $listItem) {
// Add current value to the array for dropdown choices
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable);
}
}
// Set choices to field
$field->choices = $List1C;
***** THIS IS WHERE I WOULD LIKE TO SET THE SELECTED VALUE *****
}
}
return $form;
}
If there is a better way to go about populating this field, I am open to suggestions at it seems that the form loads a bit slow using this method. Otherwise, I would love to know how to set a selected choice value after populating the choices.
the quick answer is, there is an option that i found for "isSelected" that can be included in the array when defining choices. I added a ternary operation to match on what I wanted selected and it set the "selected value for me.
$isSelected = ($listItem->variable == "value I want to select") ? true : false;
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable, 'isSelected' => $isSelected);
Adding the ternary choice and changing the array push allowed me to set a value as selected.
When i select value from drop down list,
and I submit form, i want my drop down list to have selected value,
not to be on default value again.
I try like this, but not working:
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array('name'=>'countriesIssued'), $select = array($_POST['countriesIssued']));?>
Also i would like to add a first value to be default, not from db, i want to do it in the code like this, array('empty'=>'--Select country--')
but not working.
Thanks
If you must change the name of your dropdownList you must manually set the value of code to $_POST['countriesIssued'] in your controller/view. As for the default, prompt is used to set this.
<?php if(!$model->code) $model->code=$_POST['countriesIssued'];?>
<?php echo $form->dropDownList($model, 'code', $countriesIssuedList, array(
'name'=>'countriesIssued','prompt'=>'--Select country--'
));?>
The second parameter (currently 'code') must be a key in this array: $countriesIssuedList
See for example here.
"Also i would like to add a first value to be default" ... perhaps you can use array_merge()?
I'm trying to add extra HTML attributes to some select options in a dropdown list in Joomla 2.5, and want to use the built-in HTML helpers rather than write the HTML by myself. The current output is:
<select>
<option value="Red">Red</option>
</select>
but I would like it to be something like:
<select>
<option value="Red" data-img="red.jpg">Red</option>
</select>
so that I can access the data-img attribute using Javascript when the selected option changes.
I'm creating the select options like so:
$select = JHtml::_('select.option', "Red", "Red");
and then passing them to JHTML to create the generic list HTML:
$html = JHTML::_('select.genericlist', ...);
I've looked through the documentation and tried passing various different parameter to the functions, but it's very confusing in terms of all the options (option.attr, attr etc) that the functions use, and Google has turned up nothing either.
Can anyone tell me what extra parameters I need to pass to the functions to get it properly add the extra attributes to the <option> elements?
Thanks in advance!
I was struggling on this exact scenario today, needing to add some extra data with the select's options. After a thorough analyze of the joomla/libraries/joomla/html/html/select.php file, I succeeded to do this with a few downside...
First, in my case, the data used for the select is coming from the database, and need some preparation for this scenario :
$db =& JFactory::getDBO();
$sql = 'SELECT nom AS value , nom AS text, prix FROM #__reservation_nourritures order by `ordering` asc';
$db->setQuery($sql);
$nourritures = $db->loadObjectList();
foreach ($nourritures as $nourriture){
//the data to use MUST be in an array form for the extra html attributes...
$nourriture->data = array('data'=>$nourriture->prix);
}
Once the data is ready, you can pass it to the JHTML function to build the select :
echo JHTML::_('select.genericlist',$nourriture,'myId',array('class'=>'nourritures','option.attr'=>'data'));
In short, the 'option.attr' must be used to insert attributes into the options. Note : The select.genericlist function MUST have only 3 arguments for this to work. From what I understand from the function, attributes only get merged into the options if you pass exactly 3 arguments to the function, otherwise it just ignores it. So if you want, as exemple, to define a preselected option with the additional parameters, you are out of luck. Here is the part regarding this in the function :
if (is_array($attribs) && func_num_args() == 3)
{
// Assume we have an options array
$options = array_merge($options, $attribs);
}
To my understanding, this is a bug and/or a bad behavior. I'll fill in a bugg in the joomla tracker when I get time.
You can do something like this
$option = JHtml::_('select.option', "Red", "Red", array('option.attr' => 'optionattr', 'attr' => array('data-img' => "red.jpg")));
or like this
$option = JHtml::_('select.option', "Red", "Red");
$option->optionattr = array(
'data-img' => "red.jpg"
);
I am trying following to make a dropdown readonly:
$element = $this->CreateElement('select', 'type');
$element->addMultiOptions( $Types );
$element->setRequired(true);
$element->setAttrib('readonly',true);
$element->setLabel('Type');
$elements[] = $element;
A dropdown is shown but I am able to select other values.
How to make a dropdown readonly ?
Thanks
You can use the read only attribute. You could avoid outputting a dropdown at all if it is not the best element to use in your particular case.
$element->setAttrib('disabled','disabled');
If you mean to only have one value selected, you could always disable it. Using your current framework, you might do something like:
$element->setAttrib('disabled', true);
i plan to set a checkbox with selected option in my form.
but i am unable to show my checkbox content in the form, i cant see any value instead of just a box for me to select.
how to show value while i using checkbox? i able to show my value while i using select.
this is in a HABTM model. any hints?
here is my selection code.
input('User',array('label'
=> 'Select Related Potential',
'multiple'=>'checkbox',
//'options' => $users,
'legend'=>$users,
//'value'=>$users,
//'id'=>$ownUserId,
'default'=>$ownUserId,
'style'=>'width:200px;height:100px',
'selected' => $ownUserId, )); ?>
This may be relevant:
You cannot use default to check a checkbox - instead you might set the value in $this->data in your controller, $form->data in your view, or set the input option checked to true.
For example:
// in Controller
$this->data['Model']['field'] = true;
Causes the field to have the value true, which will result in a checked checkbox for this field.
Or:
$form->input('Model.field', array('checked' => true));
Always checks the checkbox.
Or:
$form->input('Model.field', array(
'checked' => ($this->data['Model']['field'] == 'xxx')
));
Dynamically sets the checkbox based on whether $this->data['Model']['field'] is 'xxx' or not.
Sorry, completely misunderstood the question.
Did you find your users via list? The options array needs to be in a particular format, a normal find() won't do that.