views:

187

answers:

4

I have a range of items that are selectable. I would like to add a button somewhere that activates a preset selection amongst those. Is there a way I can do that?

What I would like is to tell it to "Select these guys" and then have all the events and all fired as normal, so I don't have to call all of those selection events manually.

More info: The events that I talk about are the ones listed in their api and on their demo page:

  • selected
  • selecting
  • start
  • stop
  • unselected
  • unselecting

And also, I think there might be data that is set/cleared as well when selecting things. So it's not just to add those css classes.

A: 

Edit : Sorry for the misunderstanding, I'm editing my answer.

So, yes it is possible the selection of the object corresponds to the class ui-selected, so what you can do is :

$(#button).click(function(){
  $("#element1").addClass("ui-selected");

  .......

});
Michael Lumbroso
This isn't correct either...there are lots of events in the background that this won't fire, not to mention setting it's data correctly.
Nick Craver
Hey, can you be more specific about the basckground events? Apart from the animations, I'm not sure to know about them. Thanks for your contribution, I know mine are uncomplete once in a while, but you keep us "basic users" from saying too much crap ;)
Michael Lumbroso
@Michael: I updated the question with information about those events :)
Svish
A: 

Is it not possible to trigger the selected event manually with .trigger('selected')?

Bruno
Hm, I suppose, but then I would miss the rest of the events of course. Also, would triggering that event do whatever jquery-ui does? I mean, I thought jquery-ui did what was necessary (marking with classes and all that, and then triggering that event itself. I want to tell it to do all that it normally does when a user selects some items, without me having to figure out what exactly all of those steps are.
Svish
I tried this way : `$("#selectable li:first").trigger('selected')` on their demo page and doesn't do anything :(
Ionut Staicu
A: 

Using Ionut code, how about:

 $("#selectable li:first").trigger('start').trigger('selecting').trigger('selected'); 

?

Antonio Louro
Nope, doesn't work. Try to open firebug console in the demo page and paste there this code. :)
Ionut Staicu
+1  A: 

There you go:

,calc: function() { this._mouseStop(); },
custom: function(guys) {
  var self = this;
  self.selectees.removeClass("ui-selected");
  guys.each(function(){
    $(this).addClass("ui-selecting");
    self._trigger("selecting", {}, {
       selecting: this
    });
  });
  this.calc(); // do the selection + trigger selected
} 

Add this after _mouseStop in selectable.js and then you can say:

$("#selectable").selectable("custom", $("#selectable :first-child"));

... or whatever you like.

Have fun! :)

galambalazs