jquery sortable update database using coldfusion
There are a ton of ways to do this in PHP but I have not found any posts that do this in Coldfusion using jQuery. If you have a link, please share that. Otherwise, here is what I'm doing:
I have four sections (first, second, third, fourth for lack of better term) and within each section is a list of items (0-15 items in each section). The client needs to be able to开发者_如何学运维 manually sort the items (I'm using jQuery sortable to do this - only within a section, not between each section). I don't want to give the user an extra step by having a "save" button. I'd like to store the updated order in the database when the list item is dropped. I have the sorting part working and now I need to update my mssql database with the new order and I'm having trouble working out the loop.
Using FF firebug, this is what is being passed via post and the recordsArray[] is what I can't seem to figure out how to loop over:
recordsArray[]=1&recordsArray[]=3&recordsArray[]=2&action=updateRecordsListings
Your help would be appreciated.
I've written two blog entries that match this to varying degrees.
This first uses datatables and drag and drop:
http://www.mccran.co.uk/index.cfm/2011/5/15/Combining-JQuery-Datatable-with-drag-and-drop-functions
The second which I think is closer to what you want is a drag and drop server side app:
http://www.mccran.co.uk/index.cfm/2011/6/17/JQuery-Sortable-Drag-and-Drop-lists-and-a-server-side-AJAX-save
It shows how you can drag and drop elements and use an ajax request to save the data.
Thought I'd go ahead and mark this as the correct answer: 4 years later and it's still working like a champ: http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/285
I pretty much took the solution in the blog post and renamed the variables to that of what I was using on our project. I'm not a part of that project any more so I don't have access to the source code but I can see the web page on the intranet and see the solution is still there. That is about the best I can offer for this at this time.
Summary: In case the link breaks, the blog post shows how to use ajax to pass a sorted list of id's ie id_5,id_1,id_3,id_2...
to a CFC. The CFC loops through the list, extracts the id values and uses the current list position as the "sort order". The individual records are then updated, and any removed id's are deleted. See the blog entry for the full example.
<cftransaction>
<cfloop list="#arguments.orderedList#" index="ndx">
<cfset id = Val( ListLast( ndx, "_" ) )>
<cfset position = position+1>
<cfset updatedids = ListAppend( updatedids, id )>
<cfquery datasource="mydsn">
update myTable
set order = <cfqueryparam value="#position#" cfsqltype="cf_sql_integer">
where id = <cfqueryparam value="#id#" cfsqltype="cf_sql_integer">
</cfquery>
</cfloop>
<!--- delete any items not in the list --->
<cfquery datasource="mydsn">
delete from myTable
where id not in ( <cfqueryparam value="#updatedids#" cfsqltype="cf_sql_integer" list="true"> )
</cfquery>
</cftransaction>
精彩评论