天天看點

雙向二級關聯的實作

 Suppose we will implement the following two <b>select</b>:

<b>Primary &lt;select&gt;</b>     {A,B,C}

<b>Secondary&lt;select&gt;</b>  { 1,2,3,4,5,6,7,8,9}

<b>Primary-&gt;Secondary rule:</b>

<b></b>

A-&gt;{1,2,3}

B-&gt;{4,5,6}

C-&gt;{7,8,9}

<b>Secondary-&gt;Primary rule:</b>

1-&gt;A

2-&gt;A

3-&gt;A

4-&gt;B

5-&gt;B

6-&gt;B

7-&gt;C

8-&gt;C

9-&gt;C

<b>The following definitions:</b>

你正在操作的&lt;select&gt; ::= <b>working select</b>

被動的,即将基于你的操作所産生的&lt;select&gt; ::= <b>desired select</b>

<b> In fact, the implementation is according the following considerations: (</b><b>實作思路)</b>

<b> </b>

1.       Let both &lt;select&gt; can accept <b>DOM event</b> ,and bind the related event handle function.

2.       Configure the <b>mapping rule</b> in the event handle function.

3.       In the event function ,create an <b>Array</b> to store the values of the  &lt;options&gt; of <b>desired select</b> based on the <b>index </b>you have chosen from the <b>working select</b>

4.       <b>Please Note: in event function ,you must set the other &lt;select&gt; ‘s event handle method to be empty in order to prevent it to process its event recursively.</b>

5.       Clear all the &lt;options&gt; of <b>desired select</b>.

6.       Create the <b>desired select</b> and filling the &lt;options&gt; by the <b>Array</b> created in Step 3.

    (Details please see the comments in the <b>javascript</b> file)

<b>Screen Shots:</b>

<b>Primary-&gt;Secondary</b>

<b>Initial State:</b>

<a href="http://blog.51cto.com/attachment/201204/104948511.png" target="_blank"></a>

<b>Step 1: (Choose one from the left, for example B, then the right can only have three choice {4,5,6})</b>

<a href="http://blog.51cto.com/attachment/201204/104959383.png" target="_blank"></a>

<b>Step 2 (Choose one from the right ,for example “5” and it shows the result)</b>

<a href="http://blog.51cto.com/attachment/201204/105012713.png" target="_blank"></a>

<b>Secondary-&gt;Primary</b>

<a href="http://blog.51cto.com/attachment/201204/105022958.png" target="_blank"></a>

<b>Step1: (Choose one from the right ,for example “7”)</b>

<a href="http://blog.51cto.com/attachment/201204/105031339.png" target="_blank"></a>

<b>Step 2: (then the left &lt;select&gt; will display “C” because of the rule “7-&gt;C”)</b>

<a href="http://blog.51cto.com/attachment/201204/105041213.png" target="_blank"></a>

Implementation:

js:

/** 

 * This function will render the secondary select according to the primart select 

 */ 

function createNumbers(){ 

    //create an alphabetIndex which stands for the selected index from the primary &lt;select&gt; 

    var alphabetIndex =document.testform.alphabets.selectedIndex; 

    //if the first index (the prompt is selected) then ignore it 

    if ((alphabetIndex == null) || (alphabetIndex == 0)) return; 

  //binding the &lt;options&gt; based on the index you have chosen from the primary &lt;select&gt; 

  if (alphabetIndex == 1) { 

  //create an array which can hold all the &lt;options&gt; of the secondary select 

  var Numbers = new Array(); 

  Numbers[0] = new Option("1"); 

  Numbers[1] = new Option("2"); 

  Numbers[2] = new Option("3"); 

  } 

  if (alphabetIndex ==2) { 

    var Numbers = new Array(); 

    Numbers[0] = new Option("4"); 

    Numbers[1] = new Option("5"); 

    Numbers[2] = new Option("6"); 

  if (alphabetIndex ==3) { 

    Numbers[0] = new Option("7"); 

    Numbers[1] = new Option("8"); 

    Numbers[2] = new Option("9"); 

  //empty all the existing options from the secondary &lt;select&gt; 

  for (i=document.testform.numbers.options.length; i&gt;0; i--) {  

  document.testform.numbers.options[i] = null; 

  //set all the &lt;options&gt; of the secondary &lt;select&gt; to be from the created Array  

  for(i=0; i&lt;Numbers.length; i++) { 

  document.testform.numbers.options[i] = Numbers[i]; 

  //set the first &lt;option&gt; of the secondary &lt;select&gt; to be "selected" status 

  document.testform.numbers.options[0].selected = true; 

  //This line is very important 

  //to prevent the event from the secondary&lt;select&gt; that may cause the primary&lt;select&gt; to execute createAlphabets() 

  document.testform.numbers.onchange=function(){}; 

 * This function will render the primary &lt;select&gt; according to the &lt;secondary&gt; select 

function createAlphabets(){ 

    var numberIndex =document.testform.numbers.selectedIndex; 

    if ((numberIndex == null) || (numberIndex == 0)) return; 

  if (numberIndex == 1) { 

  var Alphabets = new Array(); 

  Alphabets[0] = new Option("A");  

  if (numberIndex == 2) { 

  if (numberIndex == 3) { 

  if (numberIndex == 4) { 

  Alphabets[0] = new Option("B");  

  if (numberIndex == 5) { 

  if (numberIndex == 6) { 

  if (numberIndex == 7) { 

  Alphabets[0] = new Option("C");  

  if (numberIndex == 8) { 

  if (numberIndex == 9) { 

  for (i=document.testform.alphabets.options.length; i&gt;0; i--) {  

  document.testform.alphabets.options[i] = null; 

  for(i=0; i&lt;Alphabets.length; i++) { 

  document.testform.alphabets.options[i] = Alphabets[i]; 

  document.testform.alphabets.options[0].selected = true; 

The html to render the selection:

&lt;html&gt; 

&lt;head&gt; 

&lt;title&gt;Test Bi-Directional Linked Select&lt;/title&gt; 

&lt;script  

    type="text/javascript"  

    src="js/linkedcombobox.js"  

    &gt; 

&lt;/script&gt; 

&lt;/head&gt; 

&lt;!--  

Suppose A -&gt;{1,2,3} 

        B -&gt;{4,5,6} 

        C -&gt;{7,8,9} 

        1-&gt;A 

        2-&gt;A 

        3-&gt;A 

        4-&gt;B 

        5-&gt;B 

        6-&gt;B 

        7-&gt;B 

        8-&gt;B 

        9-&gt;B 

 --&gt; 

&lt;form name="testform"&gt; 

Test Bi-Directional Select : 

&lt;select name="alphabets" onchange="createNumbers()"&gt; 

  &lt;option value=" "&gt;Choose an Alphabet&lt;/option&gt; 

  &lt;option value="A"&gt;A&lt;/option&gt; 

  &lt;option value="B"&gt;B&lt;/option&gt; 

  &lt;option value="C"&gt;C&lt;/option&gt; 

&lt;/select&gt; 

&lt;select name="numbers" onchange="createAlphabets()"&gt; 

  &lt;option value=""&gt;Choose a Number&lt;/option&gt; 

  &lt;option value="1"&gt;1&lt;/option&gt; 

  &lt;option value="2"&gt;2&lt;/option&gt; 

  &lt;option value="3"&gt;3&lt;/option&gt; 

  &lt;option value="4"&gt;4&lt;/option&gt; 

  &lt;option value="5"&gt;5&lt;/option&gt; 

  &lt;option value="6"&gt;6&lt;/option&gt; 

  &lt;option value="7"&gt;7&lt;/option&gt; 

  &lt;option value="8"&gt;8&lt;/option&gt; 

  &lt;option value="9"&gt;9&lt;/option&gt; 

&lt;/form&gt; 

&lt;/body&gt; 

&lt;/html&gt; 

本文轉自 charles_wang888 51CTO部落格,原文連結:http://blog.51cto.com/supercharles888/840268,如需轉載請自行聯系原作者

繼續閱讀