I really was not sure what to title this post so that's what I came up with. I have a friend who I do work for that has a pretty simple admin screen where he can update properties of an e commerce application I built for him. Recently he asked for an option to change the status of an order. This is fairly common in this type of application but what we did not want to do is present an order list, click on an order to view the details, update the order and then return back to the order screen. We decided it would be best if he could easily change the status right from the orders listing. This article will walk you through a couple things I found while creating this feature, hopefully it helps someone.
First let's look at our new admin screen. Here we have a list of orders and instead of displaying a status we are going to display the status as a pick list with the current status selected, nothing big going on here.

The thing to pay attention to here is what I am doing with the id of the select box. All status drop downs will have a class of status but the id will be unique for each one. During the output loop we will insert the order id so our status will be status-orderid. This will allow us to know what order is being changed.
Now we are ready to rock and roll. The first thing we need to do is to add a change handler for all status drop down box. This reason we are using the class is so we can register a change listener on all status drop downs. It would not make much sense to create a listener for every single one. To get the order id we are using this which represents the item that was changed and using the split method to break the id into an array. From there we know the id will always be the 2nd part of the array (remember arrays in javascript are 0 based. Now that we have the id we can get the status the admin selected. I made a mistake here that you might come across so be on the look out for it. If you don't put a space before option:selected the selelctor appears as "#status-orderidoption:selected" and as you can imagine that just will not work so be careful.
To update that order status on the server side we need the orderId and the new status, both of which we now have. To accomplish our Ajax call to our back end we are going to use the post method. The post method will do all the magic for us all we need to do is provide some details. The first argument is the URL we are calling and it happens to be our order component. The next item is a object of key/value pairs to be sent to the server. I am sending the method I want to call, the order id and the new status. Finally I am setting a callback method, which is the method to call when this round trip is done. It's important to note that this will happen on success or failure and will send the response along with it.
Before we look at handling the response we will take a quick peek at the server side. This is pretty bacsic update method that will set the status of an order based on it's order id. Remember like all ajax methods we need to set the access to remote. If the update was a success I am returning the string "success" if not I am returning the error message.
We are in the home stretch. Now that the call has been made to our component and the status has either been updated or failed, we just need to react to it. In our post method we defined afterUpdateStatus as our callback. Remember that this method will accept the response as an argument. In ColdFusion8 or higher you can return plain text or json but if you just return a string in 7 it will turn it into wddx. I did a quick search and this little regex seems to work good for stripping out the xml and just giving you the response. You can probably do more with the response but I am just outputting it in an alert.
If there are better ways to do things please speak up! I have no problem admitting I don't know everything. This little gem works though and it's added some nice functionality for our orders admin screen. I hope this helps someone through a problem and as always feel free to fire away questions.

#1 by todd sharp on 6/8/09 - 4:34 PM
I believe there is something up on RIAForge that will serialize/deserialize JSON for older versions of CF.
#2 by Jiel on 6/9/09 - 12:47 AM
#3 by Dan on 6/9/09 - 7:20 AM
@Jiel - Please let me know if you have any questions.
#4 by Dave Phipps on 6/25/09 - 10:39 AM
$("._status").change(updateStatus)
I recently read here:
http://net.tutsplus.com/tutorials/javascript-ajax/...
that using the class selector is a lot slower than an id selector so I would change it to this:
$("select[id^='status-']").change(updateStatus);
If your container has an id you can also help jquery out by giving your selector context, e.g if your container id is statuscontainer then you would use:
$("select[id^='status-']", "statuscontainer").change(updateStatus);
May just be another way of doing it!
#5 by Dan Vega on 6/25/09 - 10:44 AM