2

I have been looking for something similar to what i'm trying to implement but haven't found it on stack or across the net. Using Jquery Selectable, but have more than one set of ul's:

<div>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</div>

​What i want to be able to do is if i select '1' from any of the options, all the other '1's are disabled. The same goes for all the other selection options, so basically you can only select one of the same options at a time. I can do this with radio buttons, using the name and value tags, but not really sure how to implement it within the selectable interface? Thanks ahead for any help or if someone can point me to a similar application.

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
user1648449
  • 97
  • 2
  • 10

1 Answers1

0

You can make only some items selectable using the filter option. After a change is made, you need to go thru all your elements and update the filter according to your needs.

Sample implementation might look like this (see the live demo here):

$(document).ready(function() {

    function enableAll() {
        $('li').addClass('enabled');
    }

    function disableSelected(selected) {

        // iterate thru all list items
        $.each($('li'), function(i, item) {

            // if the text is selected somewhere..
            if ($.inArray($(this).text().toLowerCase(), selected) >= 0) {

                // ..and it's not here
                if (!$(this).hasClass('ui-selected')) {

                    // remove
                    $(this).removeClass('enabled');
                }
            }
        });
    }

    $('ul').selectable({

        // only matching items will be selectable
        filter: '.enabled',

        // change handler
        stop: function(event, ui) {

            // we will collect selected texts in this variable
            var selectedTexts = new Array();

            // go thru all selected items (not only in current list)
            $('.ui-selected').each(function() {

                // extract selected text
                var text = $(this).text().toLowerCase();

                // add to array if not already there
                if ($.inArray(text, selectedTexts) < 0) {
                    selectedTexts.push(text);
                }
            });

            // enable all list items for selectable
            enableAll();

            // disable list items with text that is already selected
            disableSelected(selectedTexts);
        }
    });

    // initialization
    enableAll();

});​
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • thanks dude. I understand what you meant with the filter option only working on some disables. For instance, when i click 4 in the first, 2 in the second, i can still also click 2 in the third. I'm still relatively new to javascript/jquery so i'll study your sample to see the language process. what i'm going to try to do is have an output element, something like a shopping cart i guess, whereby the selections from each list are displayed and there can only be one of each number represented. thanks again. – user1648449 Sep 21 '12 at 16:07