this looks very over complicated .... If all you are trying to do is update a table on a droppable to remeber the order then its so simple you can do it in about 3 lines .....
I missed the original post .. if you re-post it i will write it for you Alex ----- Original Message ----- From: "Walter Lee Davis" <[email protected]> To: <[email protected]> Sent: Thursday, May 28, 2009 3:50 PM Subject: [Proto-Scripty] Re: MySQL - order ID > > I think we are missing one another here. The order of things on the > screen is not entirely driven by the database in a Sortable. Until you > fire Sortable.serialize and send the new order back to the server, the > elements on screen may be a different order than in the database. Once > you fire that callback (using onUpdate()) you can synch the two back > up, but there's no requirement that you reload the list in its > entirety in order to interact with it further. > > Add a column to your database called position (Rails convention) or > whatever else you want to call it. Make it an int or a smaller form of > int. Set it to default to 0. > > Each time your callback fires with the new order, update that column > like this: > > foreach($_POST['sort_list'] as $k=>$v){ > if($item = ActiveRecord::FindById($model,$v)) { > $item->position = $k+1; > $item->save(); > } > } > > This is using MyActiveRecord syntax, your implementation will vary, > but this line: > > if($item = ActiveRecord::FindById($model,$v)) { > > is roughly the same as: > > $id = intval($v); > $result = mysql_query('SELECT * FROM `table` WHERE id = ' . $id); > if (is_resource($result){ > > and this part: > > $item->position = $k+1; > $item->save(); > } > > is roughly the same as > > $position = intval($k); > $update = mysql_query('UPDATE `table` SET `position` = ' . > $position . ' WHERE id = ' . $id); > } > > Your list on screen should be generated using ORDER BY position ASC, > and then the Sortable and the database can remain in synch, no matter > how you drag things around. > > The key to getting your insert to drop where you want it to is going > to be to use the onChange() function in the Sortable class (which > fires each time anything happens in the sortable, not just when it > finishes re-ordering) to get Sortable to tell you what object is > directly before your drop point. It's been a few weeks since I read > through the source, but I am pretty sure that's something it can tell > you, since it depends on that information in its every move to allow > these sorts of drag-sort and drop-sort to happen in a "vanilla" two > lists with containment setting. Please do let me know if you work this > out, I wasn't able to, but that doesn't mean new eyes won't see what I > overlooked. > > Walter > > On May 28, 2009, at 10:08 AM, WLQ wrote: > >> >> An because it clones also the Id of a row it will be placed based on >> the Id, but not where it was dropped. I can easily tell it to append >> the biggest or the lowest id number, because I'm doing a MySQL query, >> but I need to paste an item not in the beginning, neither at the end, >> but there where it was dropped. >> >> On May 28, 4:03 pm, WLQ <[email protected]> wrote: >>> Ok this onDrop function does the following: >>> >>> $result = $this->mMysqli->query('INSERT INTO #1sortable SELECT * FROM >>> #2sortable WHERE id="' . >>> $content . '"'); >>> $updatedList = $this->Build#1(); >>> return $updatedList; >>> >>> onDrop it's cloning a row from one table to another. It's doing a >>> MySQl query: >>> >>> INSERT INTO #1sortable (inserts a row of the dropped item (my >>> sortable >>> is driven by MySLQ database, so each sortable item has it's own "row" >>> in the "table") inside of a #1 sortable "table"). >>> >>> SELECT * FROM #2sortable (it selects the row from #2 "table" (the #2 >>> "table" is the second sortable list which is driven by MySQL >>> database)). >>> >>> WHERE id="' . $content . (it's what it's copying). >>> >>> $updatedList = $this->Build#1(); >>> return $updatedList; After cloning is done, the sortable list >>> updates (rebuilding the list). >>> >>> On May 28, 3:40 pm, Walter Lee Davis <[email protected]> wrote: >>> >>>> I have not been able to get this to work the way you describe. >>>> What I >>>> had to do was set up my "factory" elements as Draggable (with >>>> revert) >>>> and then set a separate Droppable container around my sortable. >>>> When I >>>> drop, the onDrop function fires, which does the Ajax call to >>>> create a >>>> new element, then that element is appended to the end of the >>>> sortable >>>> and the sortable is re-initialized so that it recognizes the new >>>> member. >>> >>>> I understand what you want to do, but I wasn't able to make it work >>>> precisely the way you describe it here. >>> >>>> One thing for you to think over as you work on this is to do less. >>>> For >>>> example: >>> >>>>> $result = $this->mMysqli->query('INSERT INTO tasks SELECT * >>>>> FROM channels WHERE id="' . >>>>> $content . '"'); >>>>> $updatedList = $this->BuildPlaylist(); >>>>> return $updatedList; >>> >>>> I can't see what's going on in here, but if you are returning (and >>>> re- >>>> populating) the entire list, maybe you can simply return the one new >>>> list element (with its newly generated ID) and insert that into your >>>> Sortable where you are dropping (which is the part I couldn't figure >>>> out myself, hence my punt). You do know the new ID after your insert >>>> -- just get the value from your $result: >>> >>>> $row = mysql_fetch_object($result); >>>> $id = $row->id; >>> >>>> Build up your element as HTML, and insert it into the outer list >>>> with >>>> Element.insert('theElementYouDroppedAfter',{after:yourHTML}); >>> >>>> then just re-run Sortable.create with the same arguments as >>>> previous, >>>> and it will tear down the old Sortable (in memory) and replace it >>>> with >>>> a new one that "knows" what order these things are currently in. >>> >>>> Walter >>> >>>> On May 28, 2009, at 7:13 AM, WLQ wrote: >>> >>>>> After it clones a row from one table to another, the list >>>>> updates. But >>>>> I want not only to clone an item, but also give it a corret Id, so >>>>> it's not somewhere inside of a sortable but exactly where you've >>>>> dropped it. >> > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
