views:

384

answers:

1

Hi,

I am using jquery 1.4.2 and trying to achieve the following:

1 - function call that sends a value to a php page to add/remove an item

2 - returns html list of the items

3 - list should still be sortable

4 - save (serialise list) onclick

My full WIP is located here [http://www.chittak.co.uk/test4/index_nw3.php][1]

I tried to delegate from the level above the UL but I could not get this to work

$("#construnctionstage").delegate('ul li', 'click', function(){

The initial list is sortable, when you click add/remove the ajax function returns a new list with the a number of items, BUT I am doing something wrong as the alert message continues to work while the list is no longer sortable.

$(document).ready(function(){

  $('ul').delegate('li', 'click', function(){

      alert('You clicked on an li element!');
    /*$("#test-list").sortable({
      handle : '.handle',
      update : function () {
          var order = $('#test-list').sortable('serialize');
        $("#info").load("process-sortable.php?"+order);
      }
    });*/
}).sortable({
      handle : '.handle',
      update : function () {
          var order = $('#test-list').sortable('serialize');
        $("#info").load("process-sortable.php?"+order);
      }
   });

});

<div id="construnctionstage"> <ul id="test-list"> <li id="listItem_1">

+1  A: 

You'll need to call .sortable() again after adding/removing new elements. Do this on the "success" handler in jQuery.ajax.

Shiki
I added to the success as suggested see below which 99% works function PlanBuilder( id, name, planfunction ) { jQuery.ajax({ type: "POST", url: "ajax2.php", data: "id=" + id + " $("#info").load("process-sortable.php?"+order); } }); } }); }
Trevor
In IE and Firefox with Firebug on it does what I want but gives an error I can't work out: Message: Object expected Line: 29 Char: 59 Code: 0 URI: http://chittak.co.uk/test4/jquery-ui-1.8.1.custom.min.js this.options is undefined Line 29 But with firebug turned off the page works! ???? I changed the ajax2.php to only return the just li html rather than the ul->li whole section as this seemed to work. And when I returned the whole ul with li it seemed to not make the li elements sortable no matter what combination I tried. Thanks for you help so far.
Trevor
What happens if you try to: $('div#construnctionstage ul#test-list').sortable('destroy') before calling sortable() again?
Shiki
I added that line and the error in IE and Firebug disappeared. Strangely I also found before your suggestion that if I changed the ui version to 1.7.1 then the error disappeared.I'm new to Jquery so thanks for the help, I've struggled to find any tutorials which go as far as what I'm trying to create.
Trevor
Now I'm going to try and add 'edit in place' feature to the content within the <li> any suggestions on a suitable example gratefully received, so far I'm trying http://papermashup.com/jquery-and-php-inline-editing/ but I need to work out either binding or understand how to rewrite it with delegate( so that it works once the ajax request occurs
Trevor