views:

32

answers:

4

I have a web page with 2 listboxes (HTML select control). The first listbox is multi-select and contains a huge number of elements.

When I select one or more elements in the first listbox the content of the second listbox has to change based on the selected values. These values have to be taken form the database. Because the selection has to work instantly I have to do all of this on the client side without callback/postback. This means injecting the content of the database when the page loads and processing it with JQuery.

What approach do you suggest for storing such data in the page? Can you point me to some existing solution?

I don't even know how to Google this.

A: 

You should jQuery to fetch data on click.

Maybe this guide will help you:
http://www.akchauhan.com/select-content-from-one-list-to-another-using-jquery/

Learn about jQuery here: http://docs.jquery.com/Tutorials

Steven
+2  A: 

I would create an object that holds the different items in arrays. Example:

var values = {
    cat1: ["item1", "item2", ...],
    cat2: ["item1", "item2", ...]
}

Whenever an element is selected from the first select, look up this value with values[selectedValue] which gives you the items for the other select box. Then you just have to generate the HTML for it.

Felix Kling
+1  A: 
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
    // Simple plugin that compares two arrays
    jQuery.fn.compare = function(t) {
        if (this.length != t.length) { 
            return false; 
        }
        var a = this.sort(),
            b = t.sort();
        for (var i = 0; t[i]; i++) {
            if (a[i] !== b[i]) { 
                return false;
            }
        }
        return true;
    };


    // Those rules should be fetched from the server
    var rules = [ 
        // select the first value if 1, 2 and 5 are selected
        { value: '1', selectedValues: [ '1', '2', '5' ] }, 
        // select the second value if 2 and 4 are selected
        { value: '2', selectedValues: [ '2', '4' ] }, 
        // select the third value if 3 is selected
        { value: '3', selectedValues: [ '3' ] }
    ];

    $(function() {
        // whenever the selection in the multiselect box changes:
        $('#first').change(function() {
            // get the array of all selected elements
            var selectedValues = $(this).val();

            // verify if this array matches any of the defined rules
            $(rules).each(function(index, rule) {
                if ($(selectedValues).compare(rule.selectedValues)) {
                    // if we have a match select the corresponding value in the second list
                    $('#second').val(rule.value);
                }
            })
        });
    });
    </script>

</head>
<body>

<select multiple="multiple" id="first">
    <option value="1">value1</option>
    <option value="2">value2</option>
    <option value="3">value3</option>
    <option value="4">value4</option>
    <option value="5">value5</option>
</select>

<select id="second">
    <option value="1">value1</option>
    <option value="2">value2</option>
    <option value="3">value3</option>
</select>

</body>
</html>
Darin Dimitrov
A: 

You can try the columnnav jquery plugin.

PJP