Scriptaculous is one of many great new JavaScript libraries created to answer the call for well written ‘Web 2.0′ JavaScript libraries. Written by Thomas Fuchs, scriptaculous has many features that can be used in AJAX-ified applications, drag-and-drop effects, and a whole slew of visual effects. The drag-and-drop effects, most notably the sortables, caught my eye because the look great, they are so easy to implement, and they’re just so much nicer than the standard listbox with up/down arrows that we see in most of today’s applications and administration tools.
SLLists - a PHP wrapper around scriptaculous sortables
So scriptaculous lists are really easy to use and implement…so why put together a PHP script to wrap around it all? I did this for a couple reasons (no the following list isn’t sortable!):
- It isn’t very obvious from scriptaculous (or other similar drag-and-drop sorting libraries) how sortables can actually be used in a real application
to perform a useful function. - I wanted a simple way to serialize the result of all this snazzy ordering into a simple PHP array with which I could update a database
- Many people are just plain scared of JavaScript and don’t know what to do with it - I’m one of these folks too!
- PHP is fun.
The resulting PHP class is a very simple class that makes it easy - with a couple lines of code to get a sortable list (or sortable just about anything) onto a page and to translate the result into a PHP array. I’ll be the first to admit that there’s no real innovation here - just trying to make an already easy to use JavaScript library easier to use for the JavaScript-scared people out there. I’ve got no documentation except for a sample page…so here’s a brief rundown of the class functions:
- SLLists - constructor that basically sets the path to the JavaScript files
- addList - adds a list or other element as a new sortable entity
- printTopJS - prints the JavaScript into the head of a PHP file
- printForm - prints an HTML form that contains the hidden inputs needed. Alternatively users can create their own forms or use the printHiddenInputs functions to put these hidden inputs in existing forms
- printBottomJS - prints the JavaScript that should go right before the closing body tag
- getOrderArray - returns an array with items and their order after being passed an input with the serialzed scriptaculous list
Download Version 0.01 of the class (and a sample page) to get started - no JavaScript needed!
For the full info, head to the SLLists page. The page has examples and shows what scriptaculous can do. And leave some comments here if you like it or have any comments…
August 26th, 2005 at 4:49 am
This is Great! Thanks for posting this.
-M
September 5th, 2005 at 8:03 am
Nice Page! Anyway to use this and have an image dragged and dropped then show up in a database?
September 7th, 2005 at 1:11 pm
Love it! Question though, I’m trying to get the passed to the db, rather than the ‘old location’. Like UPDATE blah SET order=$item[’order’] WHERE blah_id = $item[’element’], and $item[’element’] is the . I’ve tried replacing the listname from ‘categories’ to ‘cat’ (where my , but nothing seems to happen. Any Help?
September 7th, 2005 at 1:15 pm
sorry, i didn’t realize the tags would be taken out. So here it is without them:
Love it! Question though, I’m trying to get the list values (with the id’s of cat[]) passed to the db, rather than the ‘old location’. Like UPDATE blah SET order=$item[’order’] WHERE blah_id = $item[’element’], and $item[’element’] is the value of the list id cat[]. I’ve tried replacing the listname from ‘categories’ to ‘cat’ (where my list is an array called cat, but nothing seems to happen. Any Help?
September 7th, 2005 at 1:45 pm
Nevermind, I figured it out!
September 7th, 2005 at 5:45 pm
even though i put this little wrapper together, i never actually put together a working example of using this with a DB. the other day i put together a simple generic script that does this (with mysql). i’ll post this up as soon as i get a chance…
September 8th, 2005 at 8:19 pm
great work, thanks a ton!
October 28th, 2005 at 4:03 am
Firefox has flaws in processing this stuff. Form inputs on sortable divs cannot be edited properly.
October 28th, 2005 at 6:51 am
Spajus: Hmmm…I didn’t notice that - I guess I never actually tried to edit the form. I wonder if some additional JavaScript could help here. The only issue I saw with the form in my example is that the textbox doesn’t get focus when clicked…i was able, however, to ‘tab’ into the field and change a value….
November 2nd, 2005 at 6:25 am
Nice Script Greg!
I haven’t had a single problem with Firefox and this script.
On another note, i’m wondering how you could use this in a heirarchal manner.
My use for this is a menu builder, where the user can drag and drop the display order of the menu items. I’m thinking about the case where a menu item would have a parent, maybe up to 3 levels deep. Something like:
link one
sub-link one
sub-link two
sub-link one
link two
link three
I’ve been playing with the code but not enough to put together a prototype. If you haven’t done it yet and i get the code working, i’ll send it over to you.
Thanks again Greg!
November 2nd, 2005 at 6:26 am
The comments don’t like indents, here’s a rework of the sample idea:
link one
—–sub-link one
—–sub-link two
———sub-link one
link two
link thre
November 8th, 2005 at 7:01 pm
Hi ,
I thought your application was rad,
I’ve made a similar application using a combination of
PHP5
JavaScript
MySQL
Example::
http://slit.no-ip.org/sortable_lists/
You can download a copy at
http://slit.no-ip.org/sortable_lists/sortable_lists.tar.gz
November 12th, 2005 at 2:25 pm
Very nice BUT: it would be even nicer if I could remove a single element with a click from the list.
Thanks for the script!
November 16th, 2005 at 9:55 am
Excellent job, Greg.
How do I set this up to automatically update the database without clicking a button? I’d like to update it as soon as an item is moved.
November 18th, 2005 at 6:24 am
That’s okay, I figured it out.
January 18th, 2006 at 7:36 am
Thanks alot for sharing this wonderful code!
Could anyone who has solved automatic db updating please post some pointers?
Groeten uit Nederland! (Greetings from Holland!)
/ Marten
January 24th, 2006 at 11:51 am
Just wanted to let you know your floating sort example key’d me into that ability of scriptaculous. Thanks for taking the time and creating that example page.
February 11th, 2006 at 2:28 pm
I really like the benefits offered by the sortable object and the serialized function.
I’m wondering, is there way to just get the ID of the LI when the object is dropped?
I just want to take that ID and do a DB update server-side with PHP.
I don’t want to deal with the entire serialed array, just the ID of the individual LI.
Thanks in advance!
Ben
February 14th, 2006 at 11:08 am
How can i change the order. i want the order desc but when i change the query it starts to go funky when i change the order and save
March 1st, 2006 at 5:58 am
Hi Greg, nice implementation! I’m having a problem in that when I reorder items, the form elements get reset to their default. I notice yours don’t, how did you get around this?
March 2nd, 2006 at 1:40 pm
hey greg great work. one question. i am trying to make something where there are 2 lists and basically you can drag images from one list over to the other and then sort both. any idea if this is possible with what you’ve done here?
March 13th, 2006 at 6:02 pm
Hi,
Nice implemenation and very good use of Scriptaculous to customize it as per requirement. I am trying to solve the following.
/*
I have a div inside which there will be list of times. I want to create this div itself a drag and drop item and also i want all the list items inside this div to be dragabbal and droppable. There are other div items like this which will have other list items. So i should be able to drag any list item from any div item and should be able to drop it in any div item.
It should preserve the order of divs and list within it upon leaving from the page.
*/
Can you please tell me how can i implement this drag and drop feature using scriptaculous or your classes? Is there any example of this nested type of drag and drop feature?
Thanks!
March 13th, 2006 at 7:57 pm
Sorting Sortables - dragging and dropping items between groups, then sorting the groups…
April 7th, 2006 at 6:13 pm
Great article. I am just sad I dont know how to reply properly, though, since I want to show my appreciation like many other.
May 20th, 2006 at 6:43 am
http://www.gregphoto.net/sortable/
Is there a way to turn “on” and “off” the drag & drop ability in sortables?
i.e. you can’t “drag” to copy text on the items. If the user could click a button to turn the drag/drop on/off it would be great. This way I could load the page with drag OFF and when they wanted to reorder, they could click a button to turn it on.
Thanks!
May 22nd, 2006 at 8:23 am
clap clap clap !
nice job !
could you find a solution for the bug mentionned by Spajus (where inputs can’t be edited) ?
In IE, inputs works fine, but radio don’t work …
I will tried to resolve this problem, but i am a noob in js
Regards.
May 24th, 2006 at 5:18 am
me again … about the inputs bug …
I found that if you use the handle option, inputs are accessibles … so this is a short and nice solution ;).
An other solution (if you don’t want to use handle) is to init all the inputs with this simple javascript function :
// init ( -> window.onload)
function iniInputOverEdit() {
var inputs = document.getElementsByTagName(”input”);
for (var i =0; i
May 24th, 2006 at 5:21 am
Damn … code isn’t accepted on your comments
Note : Replace > by the character inferior
// initialisation (window.onload)
function iniInputOverEdit() {
var inputs = document.getElementsByTagName(”input”);
for (var i =0; i >inputs.length; i++){
var inp = inputs[i];
iniInput(inp);
}
}
// initialisation de chaque item input
function iniInput(element){
var view = element;
// Events functions
function edit(){
this.focus();
}
function select(){
this.select();
}
// Events declarations
view.ondblclick = select
view.onclick = edit
}
July 10th, 2006 at 7:09 am
Has anyone been able to figure out how to sort elements in a nested list?
July 10th, 2006 at 2:44 pm
Hi, Thanks for posting this! It’s really cool. Any idea how I might modify this to drag the div when you click on the img instead? I only ask because in your example for the div draggables you can’t type in the input box because of the drag that overtakes it. So i was hoping to add an element like an image inside the div and use that for the drag point instead of the whole div. Any insight would be great. I’m diggin thru dragdrop.js right now…. good place to start?
July 10th, 2006 at 4:23 pm
@B-Rich: check out my later post http://www.gregphoto.net/index.php/2006/05/30/updated-scriplaculous-sortable-demo/ where it shows dragging something by a handle. You can just change the simple javascript to point to a different handle which can be an image…
July 11th, 2006 at 4:53 pm
Greg! Thanks a bunch…got it! Very cool. If you got a running wishlist from everyone.. lol The option to remove an item from the list would be sweet. I’ve already asked enough from you though. =P
Thanks again! Really appriciate it.
October 4th, 2006 at 2:35 pm
thanks, got it set up properly now. works great.
I really wish you had a bit more documentation though.
Without documentation, the only thing one can do is literally read all the code but since that whole id=”ITEM” part kept slipping past me (especially thres no JS or PHP function that obviously strips off the ITEM part and uses the remaining number) I was lost for a long time.
I’m gonna have to go read through the scriptaculous and prototype files now, still looking for where the item_number gets split for the number to be used. It’s driving me mad.
October 5th, 2006 at 12:36 pm
Does anybody know how to do a descending list instead of a ascending ?
October 5th, 2006 at 2:14 pm
@thinsoldier - as my things are are purely demos, I don’t have much to document. The scriptaculous site has pretty decent documentation on all of these issues. I think the id has to have some sort of text - that’s more of an html limitation…I don’t think you can have an element with a purely numeric id.
@chris - I’m not sure what you’re looking for, you’re pulling something from the database and you want to get it in descending order, use a order clause…if you want to get the order in reverse order from what is shown on the screen, use it like you normally would and then use a php function to reverse the array.
@B-Rich - removing an item can be as simple as doing (in Javascript) Element.remove(’myelementid’); where myelementid is the id of the element you want to remove. Element.remove is a function of the prototype library. Then you’d have to figure out how to deal with knowing that it had been removed on the php side. Alternatively you could use ajax to send a query to the DB on clicking a remove button and if that request is successful, do the whole element.remove thing.
November 22nd, 2006 at 6:45 pm
very nice!
how about implementing this with rico.
http://openrico.org
actually, i need his live grid..
its very usefull yah know.. :))
tnx!
December 22nd, 2006 at 11:24 am
Just a word to those who have updated PHP versions, you may need to edit the PHP files and change all short opening PHP tags (
December 22nd, 2006 at 11:25 am
OK LETS TRY THIS AGAIN BUT WITHOUT THROWING STUFF THAT GETS CHOPPED BECAUSE OF CODE TAGS…
Just a word to those who have updated PHP versions, you may need to edit the PHP files and change all short opening PHP tags (<?) to long tags (<?php) as well as using the long opening tag in conjunction with echo and varname instead of the short tag = varname.
My system puked on that part of the script because I have short tags off.
Otherwise, this little app rocks. Good job and thanks.
January 17th, 2007 at 9:02 am
Noticed someone mentioned sorting nested lists. I’ve seen the scriptaculous version of nested list sorting and found it wasn’t developed enough yet and was quite jumpy and buggy. So I went and wrote my own based on the lovely mootools http://mootools.net
It’ll sort nested lists, allow optional sorting restrictions such as same depth only or same parent only, optional collapsible lists to make it easier to sort huge lists and will even expand collapsed items when dragged over with “shift” pressed. Thought it might be useful as I’m forever faced with tree structures in my web applications
http://www.clanccc.co.uk/moo/sortlist.html (mine)
vs.
http://script.aculo.us/playground/test/functional/sortable_tree_test.html (scriptaculous)
February 14th, 2007 at 9:36 am
Greg. I have a very urgent question.
In the simple sortable list, I have all the items coming from database and getting listted nicely. I want to have a small cross on the right of each div(array) so that once pressed can delete the div which will result in deleting that item from the database table.
can you please help me where to put the delete function and how to put the delete gif or word in writting right next to each item?
thanks
April 1st, 2007 at 7:10 am
[…] to The fantastic Scriptaculous library and Greg Neustaetter who’s precious time saving PHP class is used by Prioritask. Comments Feed | […]
May 8th, 2007 at 1:53 am
Hey there Greg,
I am trying to make a “loading screen” using (ajax, javascript) to show while my lists are updating.
Any idea how I will go about doing this, I’m new to the whole ajax game, so a little help would be appreciated! :))))
thx
May 9th, 2007 at 5:42 am
Hi Greg,
did you find a solution for the textbox focus problem?
thnx
July 27th, 2007 at 6:49 am
Greg: thank you for the examples. I am using the “Sorting a regular list” for a project I am working on.
My question is: has anyone added ‘edit in place’ functionality to this example? I am very interested in adding that to my system.
Thanks!
February 8th, 2008 at 8:43 am
Hi, I looked at your example at http://www.gregphoto.net/sortable/advanced
In all the browsers that I’ve tested it in, the dragging of the items is “erratic”. The wrong items that are left behind (i.e. not being dragged) are jumping around to accommodate for the item being dragged.
This is also exactly the problem I’m facing with a site I’m developing. I’ve sat for hours debugging my CSS but couldn’t find the problem.
Can you replicate the problem with Scriptaculous v1.8 ? Any ideas on the solution?
thanks,
Francois