Esto es una guía de uso de la clase JHTMLSelect.
Para darle un poco de color a la tarea suponemos que estamos haciendo un componente específico, en este caso se trata de uno destinado a contabilizar las víctimas fatales de la caída de un meteorito del tamaño de Saturno sobre una aldea de 200 habitantes.
booleanlist
Genera 2 botones de radio, con las opciones afirmativa y negativa.
string booleanlist (string $name, [string $attribs = null], [mixed $selected = null], [ $yes = 'yes'], [ $no = 'no'], [ $id = false])
* string $name: nombre del elemento html
* string $attribs: Atributos adicionales para la etiqueta select, El único que tiene sentido en este caso sería "tabindex" (es el orden en que el usuario salta por los distintos controles con la tecla TAB).
* mixed $selected: Valor seleccionado por defecto (esto con firefox no marcha).
* $yes Palabra que se ve al lado del botón afirmativo
* $no Palabra que se ve al lado del botón negativo.
* $id id para los campos si no queremos que sea como el nombre
- Código: Seleccionar todo
echo "¿Ud. es de los que han muerto?" .
JHTMLSelect::booleanlist('pregunta','tabindex=7',0, sí, no);
produce:
- Código: Seleccionar todo
¿Ud. es de los que han muerto?
<input type="radio" name="pregunta" id="pregunta0" value="0" checked="checked" tabindex=7 />
<label for="pregunta0">no</label>
<input type="radio" name="poronga" id="pregunta1" value="1" />
<label for="pregunta1">yes</label>
Si le damos valor al parámetro id (
- Código: Seleccionar todo
JHTMLSelect::booleanlist('pregunta','tabindex=7',0, sí, no, boniatito);
- Código: Seleccionar todo
<input type="radio" name="pregunta" id="boniatito0" value="0" checked="checked" tabindex=7 />
<label for="boniatito0">no</label>
<input type="radio" name="pregunta" id="boniatito1" value="1" tabindex=7 />
<label for="boniatito1">sí</label>
genericlist
Genera una lista de selección (.string genericlist (array $arr, string $name, [string $attribs = null], [string $key = 'value'], [string $text = 'text'], [mixed $selected = NULL], [ $idtag = false], [ $translate = false])
* array $arr: array de objetos
* string $name: nombre del elemento html
* string $attribs: Atributos adicionales para la etiqueta select.
* string $key: La variable del objeto que se usará para el valor de la opción
* string $text: La variable del objeto que se usará para el texto de la opción.
* mixed $selected: el valor preseleccionado (acepta una cadena o un array)
* $idtag
* $translate
Creemos un par de objetos para usar en esta lista:
- Código: Seleccionar todo
$pepa = new StdClass;
$pepa->perro = 'Napoleón';
$pepa->gato = 'Fosforito';
$pepe = new StdClass;
$pepe->perro = 'Artajerjes';
$pepe->gato = 'Pedorro';
Y ahora vamos a hacer que nos muestre la lista de gatos (pero en realidad el valor que vamos a recibir ser{a el de los perros):
- Código: Seleccionar todo
echo JHTML::_('select.genericlist',array($pepa, $pepe), 'atravesado', null, 'perro', 'gato');
- Código: Seleccionar todo
<select name="atravesado" id="atravesado" ><option value="Napoleón" >Fosforito</option><option value="Artajerjes" >Pedorro</option></select>
En realidad es todo mucho más sangriento, en vez de perritos y gatitos el componente habla de aldeanos descuartizados y de huéfanos, pero lo eufemizo un poco por si hay personas susceptibles leyendo esto.
Pero ahora quiero un select múltiple, para que el usuario pueda mantener apretada la tecla CTRL y de esa manera elegir varios valores a la vez. El código html que se necesita para eso es:
- Código: Seleccionar todo
Causa de la muerte:<select name="causa[]" id="causa" multiple><option value="aplastado" >aplastado</option><option value="despanzurrado" >despanzurrado</option><option value="aniquilado" >aniquilado</option></select>
Nota sobre HTML: son necesarios los corchetes después del nombre del select, si no los ponemos el select se va a ver bien y el usuario podrá marcar varios valores, pero en vez de un array el formulario va a enviar solamente el último valor elegido.
Lo obtenemos así:
- Código: Seleccionar todo
JHTML::_('select.genericlist',$esta, 'causa[]', 'multiple', 'causaDeMuerte', 'causaDeMuerte');
donde $esta es un array de objetos, cada uno de los cuales tiene asignado un valor a la variable 'causaDeMuerte', y esos valores son 'aplastado', 'despanzurrado', etc.
integerlist
Genera una lista de números enteros para elegir.
* return: HTML for the select list
string integerlist (int $start, int $end, int $inc, string $name, [string $attribs = null], [mixed $selected = null], [string $format = ""])
* int $start: El número inicial
* int $end: el número final
* int $inc: El incremento
* string $name: Nombre del elemento html
* string $attribs: atributos adicionales para la etiqueta select
* mixed $selected: Valor elegido por defecto
* string $format: El formato para printf (opcional)
- Código: Seleccionar todo
echo JHTML::_('select.integerlist',7, 21, 2, 'berengena');
produce
- Código: Seleccionar todo
<select name="berengena" id="berengena" ><option value="7" >7</option><option value="9" >9</option><option value="11" >11</option><option value="13" >13</option><option value="15" >15</option><option value="17" >17</option><option value="19" >19</option><option value="21" >21</option></select>
lo que funciona correctamente a pesar de que berenjena se escribe con j.
Bueno, de momento hasta aquí he llegado, de aquí en más es la mera transcripción de api.joomla.org
optgroup (line 47)
object optgroup (string $text, [string $value_name = 'value'], [string $text_name = 'text'])
* string $text: The text for the option
* string $value_name: The returned object property name for the value
* string $text_name: The returned object property name for the text
option (line 32)
object option (string $value, [string $text = ''], [string $value_name = 'value'], [string $text_name = 'text'], [ $disable = false])
* string $value: The value of the option
* string $text: The text for the option
* string $value_name: The returned object property name for the value
* string $text_name: The returned object property name for the text
* $disable
options (line 64)
Generates just the option tags for an HTML select list
* return: HTML for the select list
string options (array $arr, [string $key = 'value'], [string $text = 'text'], [mixed $selected = null], [ $translate = false])
* array $arr: An array of objects
* string $key: The name of the object variable for the option value
* string $text: The name of the object variable for the option text
* mixed $selected: The key that is selected (accepts an array or a string)
* $translate
radiolist (line 217)
Generates an HTML radio list
* return: HTML for the select list
string radiolist (array $arr, string $name, [string $attribs = null], [mixed $key = 'value'], [string $text = 'text'], [string $selected = null], [ $idtag = false], [ $translate = false])
* array $arr: An array of objects
* string $name: The value of the HTML name attribute
* string $attribs: Additional HTML attributes for the <select> tag
* mixed $key: The key that is selected
* string $text: The name of the object variable for the option value
* string $selected: The name of the object variable for the option text
* $idtag
* $translate


