[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/rsc/js/ -> dynamic_select.js (source)

   1  /**

   2   *    Dynamic select list options

   3   *

   4   *     Exemple:

   5   * 

   6   *     LIST 1 Values 'a','b','c'

   7   *

   8   *    LIST 2 Values 'a-1','a-2','b-3','c-2','c-4' 

   9   *    ==> After Refresh LIST 2 values '1','2' if SL1 = 'a' -- OR -- '3' if SL1 = 'b'  ..........

  10   *

  11   *     CloneOptions always Values 'a-1','a-2','b-3','c-2','c-4'

  12   *

  13   * This file is part of the evoCore framework - {@link http://evocore.net/}

  14   * See also {@link http://sourceforge.net/projects/evocms/}.

  15   *

  16   * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}

  17   * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.

  18   *

  19   * {@internal License choice

  20   * - If you have received this file as part of a package, please find the license.txt file in

  21   *   the same folder or the closest folder above for complete license terms.

  22   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  23   *   then you must choose one of the following licenses before using the file:

  24   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  25   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  26   * }}

  27   *

  28   * {@internal Open Source relicensing agreement:

  29   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license

  30   * PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project

  31   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  32   * }}

  33   *

  34   * @package admin

  35   *

  36   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  37   * @author fplanque: Francois PLANQUE.

  38   * @author mbruneau: Marc BRUNEAU / PROGIDISTRI

  39   */
  40   
  41  /**

  42  * Called by window.onload event

  43  * Initialize all parent_child selects lists of the form

  44  */
  45  function init_dynamicSelect()
  46  {
  47      for( var i = 0; i < nb_dynamicSelects ; i++)
  48      {
  49          dynamicSelect( tab_dynamicSelects[i]['parent'], tab_dynamicSelects[i]['child'] ); 
  50      }
  51  }
  52   
  53  /**

  54   * Initialize select lists

  55   *

  56   * You need to use it to handle parent_child select lists

  57   * This will clone all select options in order to reuse a filtered subset later, depending on the parent selection.

  58   * This will also add the onchange handler on the parent

  59   *

  60   * @param parent select list

  61   * @param child select list

  62   */
  63  function dynamicSelect( id1, id2 ) 
  64  {
  65      // Feature test to see if there is enough W3C DOM support

  66      if (document.getElementById && document.getElementsByTagName) 
  67      {
  68          // Obtain references to both select boxes

  69          var sel1 = document.getElementById(id1);
  70          var sel2 = document.getElementById(id2);
  71          // Clone the dynamic select box

  72          var clone = sel2.cloneNode(true);
  73          // Obtain references to all cloned options 

  74          var clonedOptions = clone.getElementsByTagName("option");
  75          // Onload init: call a generic function to display the related options in the dynamic select box

  76          refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
  77          // Onchange of the main select box: call a generic function to display the related options in the dynamic select box

  78          sel1.onchange = function() 
  79          {
  80              refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
  81          }
  82      }
  83  }
  84  
  85  
  86  /**

  87   * Refresh the child select list when the parent changes.

  88   *

  89   * @param parent select list

  90   * @param child select list

  91   * @param clone of the child select list initialized  

  92   */
  93  function refreshDynamicSelectOptions( sel1, sel2, clonedOptions ) 
  94  {
  95      // Delete all options of the dynamic select box

  96      while( sel2.options.length )
  97      {
  98          sel2.remove(0);
  99      }
 100      // Regular expression to test if the value of a cloned option begins with the value of the selected option of the main select box

 101      var pattern1 = new RegExp( "^" + sel1.options[sel1.selectedIndex].value + "-.*$" ); 
 102  
 103      // Regular expression to keep only the second part(X2) of the value "X1-X2"

 104      //var pattern2 = new RegExp( "^.*-(.*)$" ); 

 105      
 106      // Iterate through all cloned options

 107      for( var i = 0, j = 0; i < clonedOptions.length; i++ )
 108      {
 109          // If the value of a cloned option begins with the value of the selected option of the main select box

 110          if ( clonedOptions[i].value.match( pattern1 ) || clonedOptions[i].value == '' ) 
 111          { 
 112              // Clone the option from the hidden option pool and append it to the dynamic select box

 113              sel2.appendChild( clonedOptions[i].cloneNode( true ) );
 114              
 115              // keep only the second part of value if exist (pattern 2)

 116              //    val = sel2.options[j].value.match( pattern2 );

 117              //if (val) sel2.options[j].value = val[1];

 118              //j++; //indice options in the new list SL2

 119          }
 120      }
 121  }


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics