Joe
Joe

Reputation: 4633

Multiple sortable lists with jquery

I am building a todo list application for one of my projects and I am creating multiple todo lists dynamically from the database and each todo list will have multiple todo items. The items will be constrained to their own list and will not be able to be connected to another list.

I've done a single sortable list in jquery before, but I am having a hard time wrapping my head around how to write DRY code, so I am not repeating myself for each new todo list a user creates. Since the jquery code will have to figure out how many todo lists there are, and then manage each one.

Anybody know how to go about doing this or could you talk through it with some pseudocode or an example would be great.

Thanks

Upvotes: 1

Views: 4269

Answers (3)

jeristotle
jeristotle

Reputation: 5

two other options (in general):

  1. create the sortable group by id:

    $("#list1, #list2, #list3").sortable({ connectWith: "#list1, #list2, #list3" });
    
  2. create the sortable group by class:

    $("#list1, #list2, #list3").sortable({ connectWith: ".transfer" });
    

    (.transfer is the class, just assign this to all your lists in your html.)

Upvotes: 1

Steerpike
Steerpike

Reputation: 17554

What's wrong with using the jQuery sortable plugin and just doing something like

$(document).ready(){
    $('ol, ul').sortable(); //make every ordered and unordered list sortable
});

Upvotes: 2

jonstjohn
jonstjohn

Reputation: 60276

To be DRY in jQuery, you want to write plugins, or use an existing plugin such as Tablesorter.

Upvotes: 0

Related Questions