Search code examples
jqueryselectablejquery-ui-selectable

jquery selectable - disable corresponding value


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.


Solution

  • 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();
    
    });​