5

I want to disable a specific element of a selectable list. I am able to disable the whole list, but when I try with specific element, it doesn't work.

$('#disableButton').click(function(){  

    $('#selectable li#b ').selectable('disable');
});

http://jsfiddle.net/Komlan/RYWaZ/1/

Here is my code

    //reset seats
function resetSelect(){
    var options = { filter: "li.selectable" };
$( "#selectable").selectable(options);               

}
//Ajax get taken seats
$('input[name="choice"]').change(function(){
    resetSelect();
    var sc_id = $('input:radio[name=choice]:checked').val();

    $.ajax({
        url: 'seatings.php',
        data:{ sc_id:sc_id},
        type: "POST",
        dataType:'text',
        success: function(data){
             var id = data.split(",");
             for (var i=0;i<id.length -1;i++){
             alert(id[i]);
                var string = "#"+id[i];
                $(string).css("color","red");
                $('#selectable li'+string).removeClass("selectable ui-selected"); 
             }
                var options = { filter: "li.selectable" };
            $( "#selectable" ).selectable('destroy').selectable(options);                
        }

    }); 
});

In summary, I'm getting a series of id and disable them one after the other, every time there is a change on my radio button group.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Komlan
  • 102
  • 4
  • 12

2 Answers2

7

There is no straight way to do this (AFAIK), but here's a little hack you can use (and remember, it's just a hack and maybe not the best):

Add the css class "selectable" (or whatever you want):

<ol id="selectable">
  <li class="ui-widget-content selectable" id="ok"> 1</li>
  <li class="ui-widget-content selectable"> 2</li>
  <li class="ui-widget-content selectable"> 3</li>
  <li class="ui-widget-content selectable"> 4</li>
  <li class="ui-widget-content selectable"> 5</li>
  <li class="ui-widget-content selectable"> 6</li>
  <li class="ui-widget-content selectable"> 7</li>
</ol>

And then use a filter on that css class:

// Create a filter to only make <li> with the specified css class, selectable.
var options = { filter: "li.selectable" };
$( "#selectable" ).selectable(options);

$('#lol').click(function(){
    console.log('dsfds');

    // Remove/add (toggle) the class used in the filter on the <li> you want to remove the selectable.
    // (Also remove the ui-selected in case it's selected.)
    $('#selectable li#ok').toggleClass("selectable").removeClass("ui-selected");

    // Now destroy the selectable and re-create it with the filter again.
    // We removed the css class from a <li> used in the filter, so it won't be selectable again.
    $( "#selectable" ).selectable('destroy').selectable(options);
});

Updated: http://jsfiddle.net/RYWaZ/7/

References:

Mario S
  • 11,715
  • 24
  • 39
  • 47
  • @user2040101 Can you explain a little bit more what your trying to disable and how? – Mario S Feb 08 '13 at 06:43
  • I want to be able to disable and enable any element in the list. Right when i disable some element, i cant re-enable that without refreshing the page. Because im collecting data when i click on those element, and if they are disable i cant select them anymore – Komlan Feb 12 '13 at 00:14
  • @user2040101 I have updated the answer to show you an example on how to toggle the enabled/disabled state of the selectable item. Is this what you meant? – Mario S Feb 12 '13 at 08:23
  • Thanks but its not the way Im looking for. Because im disabling a some li in the selectable, everytime theres a change in a radio button. And when theres another change i want everything to be selectable again and disable those who need to be. So now my problem is that i cant re-enable the li – Komlan Feb 17 '13 at 23:04
  • @user2040101 I think you need to provide more code then, it's easier to help you if you show what you have. The example above shows you how to disable and re-enable selectable on `
  • `-tags. You should be able to work that out (it's just a matter of adding or removing the *selectable* class from the `
  • `-tags).
  • – Mario S Feb 18 '13 at 07:35