|
Combining JQuery Datatable with drag and drop functions |
The datatables JQuery plugin ( http://www.datatables.net/ ) is an incredibly powerful way of displaying tabular data and allowing the user to manipulate it without leaving the current view.
The plugin already features paging and sorting, but what if you want to extend the plugin with some other non default behaviours like drag and drop? Using a previous example ( http://www.mccran.co.uk/index.cfm/2010/4/29/JQuery-Datatables-plugin-example-using-a-server-side-data-request-coldfusion ) as a starting point I thought I'd try and integrate drag and drop with the datatable object.
There is an example of the finished JQuery datatable with added drag and drop here.
I won't go into how the datatable gets the data through an AJAX request as this is handled in the article above.
Firstly add the JQuery UI JavaScript file, as this will be handling the interactions.
Next we will amend the existing datatable code and add in the block below. Using the 'sortable' UI function we can specify that each row will be a draggable object. (Any DOM elements specified as 'sortable' automatically inherit the 'draggable' functions properties.)
Using the 'start' and 'update' events we can calculate the starting and ending position of our dragged object, correcting the value for zero based JavaScript Arrays. In the 'update' event I am also grabbing the table cell value as it is the record id in the example code. In this way I can pass this through to a remote CFC and amend the record in the database.
Lastly there is a simple ajax request to a data handler object, and a function to handle the response back from the object.
2var end;
3
4 $("#displayData tbody").sortable({
5 cursor: "move",
6 start:function(event, ui){
7 // 0 based array, add one
8 start = ui.item.prevAll().length + 1;
9 },
10
11 update: function(event, ui) {
12 // 0 based array, add one
13 end = ui.item.prevAll().length + 1;
14 alert('Start: ' + start + ' End: ' + end);
15 var id = ui.item.context.children[0].innerHTML;
16 alert(id);
17
18 $.getJSON('dao.cfc', {
19 method:'methodName',
20 returnFormat:'JSON',
21 name:value
22 },
23
24 // handle the response
25 function(data){
26 if(data.intSuccess == 1){
27 // success
28 } else {
29 // error
30 }
31 });
32 }
33 // end of drag
34 });
In the following example I have also created two areas on the page that will accept a dragged object. One is labelled 'duplicate' the other 'delete', these have events attached to them so as to demonstrate that you could easily add graphical tools to duplicate and delete record through dragging them into these elements. Just imagine the 'delete' box as a bin.
Example JQuery datatable with added drag and drop.
Thanks for the comments. I can't see how it isn't working though? Can you elaborate on the issue?
I tried dragging the rows onto the two drop boxes (Duplicate and Delete) but it's quite hard to target them. Perhaps having the two boxes presented vertically would be better?
I managed to hit the target after countless tries and although I saw the notification (delete or duplicate), it doesn't seem to work.
Perhaps you could also reduce the number of entries, make it say just 10 rows so any changes made would be noticeable more easily.
One last thing, say if there Location can be sort based on popularity. For example:
Location Popularity
Abingdon 1
Airdrie 2
Bangor 3
Belfast 4
Canterbury 5
Would it be possible to update the Rank upon dragging them up and down? eg. Dragging Airdrie to the first row will update the rank to 1 and change Abingdon's to 2.
Thank you!
I agree, in my example the difference in size and shape make the elements awkward to drag over each other.
Also the 'delete' and 'duplicate' alerts simply show the fact that a function has been triggered. I tend not to actually delete records in demo's, you never know when someone will just delete all your data!
Changing the sort on poularity would be a good use for this type of functioanlity. You could submit the dropped value to a server side handler and detect the position change and adjust the records around it. Good spot.
Yes it is possible to have sub category levels, you just need to layer in the Div elements in a hierarchical way. That way the JQuery selector will just pick them up.
Yes, I am available for hire, if you have a specific project you want to get done let me know.
Buck,
Yes, in my example where the pop up message is triggered you can insert an AJAX request to a CFC that submits the position of the moved elements. That way the CFC can run a query and simply update an index value in the DB to change the order. If that doesn't make sense let me know and I'll put together an article on it.
http://www.albela.in/movers-and-packers-delhi-to-d...
<a href="http://www.lechalo.in/packers-and-movers-chennai/&... And Movers In Chennai</a><br>
http://www.lechalo.in/about-us.html
http://www.lechalo.in/contact.html
http://blog.lechalo.in/
http://www.lechalo.in/packers-and-movers-mumbai/
http://www.lechalo.in/packers-and-movers-pune/
http://www.lechalo.in/packers-and-movers-hyderabad...
http://www.lechalo.in/packers-and-movers-bangalore...
Packers and Movers Bangalore @
http://www.expert5th.in/packers-and-movers-bangalo...
Packers and Movers Mumbai @
http://www.expert5th.in/packers-and-movers-mumbai/...
Packers and Movers Chennai @
http://www.expert5th.in/packers-and-movers-chennai...
Packers and Movers Hyderabad @
http://www.expert5th.in/packers-and-movers-hyderab...
Packers and Movers Delhi @
http://www.expert5th.in/packers-and-movers-delhi/
Packers and Movers Gurgaon @
http://www.expert5th.in/packers-and-movers-gurgaon...
Packers and Movers Noida @
http://www.expert5th.in/packers-and-movers-noida/
Packers and Movers Pune @
http://www.expert5th.in/packers-and-movers-pune/
Packers and Movers Thane @
http://www.expert5th.in/packers-and-movers-thane/
Packers and Movers Navi Mumbai @
http://www.expert5th.in/packers-and-movers-navimum...
Packers and Movers Ghaziabad @
http://www.expert5th.in/packers-and-movers-ghaziab...
Packers and Movers Faridabad @
http://www.expert5th.in/packers-and-movers-faridab...
instagram likes
http://socialgrand.com/buy-instagram-likes/
[
Yes, I am available for hire, if you have a specific project you want to get done let me know.