Custom 9

Overview

This example will display either a dual or single multi-select QuickForm element, with options and fancy attributes.
Since version 1.5.0 the fancy attributes applied on options are now kept even on transfert or move inside selection.
This example will also introduce how to handle the persistant options at run-time.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains

Step 1

The dual multi-select is created with headers (lines 39-42), and will used the data defined from $fruit_array (lines 23 thru 34). Data is loaded at line 82, where we also set default selected values.

As you can see, we've defined options "pear", and "tangerine" (line 84) as persistant options: that means we could not move them unless we remove later the disabled attribute at run-time (lines 159-169).

Step 2

Two layouts need two different templates:
- Alternative template is set on lines 66-80, for a dual multi-select element shape.
- Main template is set on lines 55-63, for a single checkboxes multi-select element shape.

Final template is set (lines 86-90) accordingly to user-choice (see checkbox near buttons lines 94-98).

Depending of template used (single or dual multi-select), we will set (line 136) or not the additional CSS rules.

Step 3

On dual multi-select, both list are 200 pixels width (line 40) and display 10 items (default attribute). Styles are managed by CSS rules and class pool (lines 131-133).

Property Value Default
size   10
width 200 100
class pool  

All buttons used default values, and an additional CSS class rule inputCommand (lines 44-52;131-133).

Property Value Default
name   add
value    >> 
type   button
class inputCommand  

Property Value Default
name   remove
value    << 
type   button
class inputCommand  

Property Value Default
name   all
value    Select All 
type   button
class inputCommand  

Property Value Default
name   none
value    Select None 
type   button
class inputCommand  

Property Value Default
name   toggle
value    Toggle Selection 
type   button
class inputCommand  

Property Value Default
name   up
value    Up 
type   button
class inputCommand  

Property Value Default
name   down
value    Down 
type   button
class inputCommand  

Property Value Default
name   top
value    Top 
type   button
class inputCommand  

Property Value Default
name   bottom
value    Bottom 
type   button
class inputCommand  

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Custom advMultiSelect HTML_QuickForm element in single or dual shape
  4.  * with fancy attributes items and all buttons
  5.  *
  6.  * @version    $Id: qfams_custom_9.php,v 1.4 2009/02/08 21:51:20 farell Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @package    HTML_QuickForm_advmultiselect
  9.  * @subpackage Examples
  10.  * @access     public
  11.  * @example    examples/qfams_custom_9.php
  12.  *             qfams_custom_9 source code
  13.  * @link       http://www.laurent-laville.org/img/qfams/screenshot/custom9.png
  14.  *             screenshot (Image PNG, 640x620 pixels) 33.3 Kb
  15.  */
  16.  
  17. require_once 'HTML/QuickForm.php';
  18. require_once 'HTML/QuickForm/advmultiselect.php';
  19.  
  20. $form = new HTML_QuickForm('amsCustom9');
  21. $form->removeAttribute('name');        // XHTML compliance
  22.  
  23. $fruit_array = array(
  24.     'apple'     =>  'Apple',
  25.     'orange'    =>  'Orange',
  26.     'pear'      =>  array('Pear', array('disabled' => 'disabled')),
  27.     'banana'    =>  'Banana',
  28.     'cherry'    =>  'Cherry',
  29.     'kiwi'      =>  array('Kiwi', array('style' => 'color:green;background-color:lightblue')),
  30.     'lemon'     =>  array('Lemon', array('style' => 'color:yellow;background-color:black')),
  31.     'lime'      =>  array('Lime', array('style' => 'color:blue')),
  32.     'tangerine' =>  array('Tangerine', array('disabled' => 'disabled',
  33.                                              'style' => 'color:red;'))
  34. );
  35.  
  36. // rendering with QF renderer engine and template system
  37. $form->addElement('header', null, 'Advanced Multiple Select: custom layout ');
  38.  
  39. $ams =& $form->createElement('advmultiselect', 'fruit', null, null,
  40.                              array('class' => 'pool', 'style' => 'width:200px;')
  41. );
  42. $ams->setLabel(array('Fruit:', 'Available', 'Selected'));
  43.  
  44. $ams->setButtonAttributes('add'       , 'class=inputCommand');
  45. $ams->setButtonAttributes('remove'    , 'class=inputCommand');
  46. $ams->setButtonAttributes('all'       , 'class=inputCommand');
  47. $ams->setButtonAttributes('none'      , 'class=inputCommand');
  48. $ams->setButtonAttributes('toggle'    , 'class=inputCommand');
  49. $ams->setButtonAttributes('moveup'    , 'class=inputCommand');
  50. $ams->setButtonAttributes('movedown'  , 'class=inputCommand');
  51. $ams->setButtonAttributes('movetop'   , 'class=inputCommand');
  52. $ams->setButtonAttributes('movebottom', 'class=inputCommand');
  53.  
  54. // template for a single checkboxes multi-select element shape
  55. $template1 = '
  56. <table{class}>
  57. <!-- BEGIN label_3 --><tr><th>{label_3}</th><th>&nbsp;</th></tr><!-- END label_3 -->
  58. <tr>
  59.  <td>{selected}</td>
  60.  <td>{all}<br />{none}<br />{toggle}</td>
  61. </tr>
  62. </table>
  63. ';
  64.  
  65. // template for a dual multi-select element shape
  66. $template2 = '
  67. <table{class}>
  68. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  69. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  70. <tr>
  71.  <td>{unselected}</td>
  72.  <td align="center">
  73.    {add}<br />{remove}<br /><br />
  74.    {all}<br />{none}<br />{toggle}<br /><br />
  75.    {moveup}<br />{movedown}<br />{movetop}<br />{movebottom}
  76.  </td>
  77.  <td>{selected}</td>
  78. </tr>
  79. </table>
  80. ';
  81.  
  82. $ams->load($fruit_array, 'pear,kiwi,lime');
  83.  
  84. $ams->setPersistantOptions(array('pear', 'tangerine'));
  85.  
  86. if (isset($_POST['multiselect']) || $_SERVER['REQUEST_METHOD'] == 'GET') {
  87.     $ams->setElementTemplate($template2);
  88. } else {
  89.     $ams->setElementTemplate($template1);
  90. }
  91.  
  92. $form->addElement($ams);
  93.  
  94. $buttons[] =& $form->createElement('submit', null, 'Submit');
  95. $buttons[] =& $form->createElement('reset',  null, 'Reset');
  96. $buttons[] =& $form->createElement('checkbox', 'multiselect', null,
  97.                                    'use dual select boxes layout');
  98. $form->addGroup($buttons, null, '&nbsp;');
  99. ?>
  100. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  101.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  102. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  103. <head>
  104. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  105. <title>HTML_QuickForm::advMultiSelect custom example 9</title>
  106. <style type="text/css">
  107. <!--
  108. body {
  109.   background-color: #FFF;
  110.   font-family: Verdana, Arial, helvetica;
  111.   font-size: 10pt;
  112. }
  113.  
  114. table.pool {
  115.   border: 0;
  116.   background-color: #787878;
  117. }
  118. table.pool td {
  119.   padding-left: 1em;
  120. }
  121. table.pool th {
  122.   font-size: 80%;
  123.   font-style: italic;
  124.   text-align: center;
  125. }
  126. table.pool select {
  127.   color: #242424;
  128.   background-color: #eee;
  129. }
  130.  
  131. .inputCommand {
  132.   width: 120px;
  133. }
  134. <?php
  135. if (!isset($_POST['multiselect'])) {
  136.     echo $ams->getElementCss();
  137. }
  138. ?>
  139.  -->
  140. </style>
  141. <script type="text/javascript">
  142. //<![CDATA[
  143. var QFAMS = {};
  144. QFAMS.env = {persistantSelection: false, persistantMove: false};
  145.  
  146. <?php echo $ams->getElementJs(true); ?>
  147. //]]>
  148. </script>
  149. </head>
  150. <body>
  151. <?php
  152. if ($form->validate()) {
  153.     $clean = $form->getSubmitValues();
  154.  
  155.     echo '<pre>';
  156.     print_r($clean);
  157.     echo '</pre>';
  158.  
  159.     // if apple fruit is selected, then pear may be remove from next selection
  160.     if (in_array('apple', $clean['fruit'])) {
  161.         $ams->setPersistantOptions('pear', false);
  162.     } else {
  163.         $ams->setPersistantOptions('pear', true);
  164.         $selection = $ams->getSelected();
  165.         if (!in_array('pear', $selection)) {
  166.             array_push($selection, 'pear');
  167.             $ams->setSelected($selection);
  168.         }
  169.     }
  170. }
  171. $form->display();
  172. ?>
  173. </body>
  174. </html>
generated by Generic Syntax Highlighter - GeSHi