The jQuery UI Swapable plugin makes selected elements swapable by dragging with the mouse.
Introduction
Enable a group of DOM elements to be swapable. Click on and drag an element to a new spot within the list; drag element and drop one will swap and the other items will not affected. By default, swapable items share sortable properties.
DETAILS
Similar to "Sortable", but only two elements of the selected group are affected: dragged and dropped which are swapped. All other elements stay at their current positions. This plugin is built based on existing "Sortable" jQuery plugin and inherits all sortable options except "cursorAt" one which is hard-coded.
Based on jquery.ui.sortable.js
Depends:
- jquery.ui.core.js
- jquery.ui.mouse.js
- jquery.ui.widget.js
- jquery.ui.sortable.js
To add the feature to a group of elements:
Minimal configuration:
$("#swappable").swappable({items: '.itemClass',cursorAt: {top:-10} });
Option's specific:
- Always set option "
cursorAt: {top: -nn}
" as a negative Integer. - Always set option "
items
" with items' class name, not element or filter.
<ul id="foo"> <li class="bar"><li> <li class="bar"><li> <li class="bar"><li> </ul>
$("#foo").swappable({
items:'.bar', // Mandatory option, class only.
cursorAt: {top:-20}, // MUST be set to negative. Default doesn't work!
great plugin, this is what i needed :)
ReplyDeleteThanks,
ReplyDeleteI'm glad to hear it :))
This looks like what I need but I cannot figure out how to get it all to work. There are a lot of dependencies, which don't seem to download, and other options. Thanks anyway.
ReplyDeleteThis plugin is built on jquery.ui.sortable.js and has the same dependencies. Try sortable first. If it works in your environment, add link to this plugin. ( must be set after all dependencies)
ReplyDeletejquery.ui.core.js
jquery.ui.mouse.js
jquery.ui.widget.js
jquery.ui.sortable.js
jquery.ui.swappable.js <- last one
please let me know if you have any questions.
regards,
-Vadim
Great gadgets. This collective info will me greatly in my Air Conditioning Companies in Qatar project.
DeleteThats exactly what I was looking for. Very helpful. Thanks a lot.
ReplyDeleteSomething must have changed. Demo not working in either Chrome or FF in both Windows and OSX.
ReplyDeleteprobable it was server maintenance.
ReplyDeleteВадим, онлайн-примеры не работают.
ReplyDeleteСсылка на http://jqueryui.com/jquery-1.4.2.js нерабочая, возможно, стоит её заменить на http://jqueryui.com/jquery-1.4.3.js
Локально, с правильно подсунутыми .js, всё ок.
thanks Previden :)
ReplyDeleteI'm glad to know that it was environment problem, not plugin's one.
hello
ReplyDeletenice work
i wonder if is it possible to send the order of grids to php??
for sure it's possible. It's a regular DOM structure and can be processed as a regular DOM. There is nothing plugin-specific rules to send the order to php. What's the problem???
ReplyDeleteThis is a really nicely done plugin, thanks!
ReplyDeleteJust wandering, is there any way to add animation to the swap?
I've had a look through the plugin but i can't figure out how to do it
Sorry for delay, Sam.
ReplyDeleteI'm afraid it's not easy. I don't plan to implement animation in the near feature :(
This doesn't seem to work with jQuery.noConflict(true);
ReplyDeleteI apologize this does work with jQuery.noConflict(true). However it is a bit buggy when using it in a grid layout. Thank you.
ReplyDeleteThank you for your comment.
ReplyDeleteCould you please give me any details of the bug? Is it reproducible?
thanks,
-Vadim
You're welcome.
ReplyDeleteI took a look at it deeper and it looks like you have to be exact on where you drag the item to. If you are semi-touching and still not at the right spot, it will revert. So probably this is not a bug. But rather the jQuery needs a way to inform the users that it has registered where you are swapping to. Thank you.
Sorry for the double post. Here is the steps to illustrate what I mean.
ReplyDeletehttp://www.eslinstructor.net/demo/swappable/swappable_grid.html
On the grid layout, if you move Grid 8 to the right of Grid 9 and not have Grid 8 touch the center of Grid 9, Grid 8 will revert back to its original position. I hope this explains what I saw. Thank you.
Well, it's not a bug.
ReplyDeletePointer must be inside a target block (not necessary in the center however, just inside). Frankly, I don't see any problem with such behavior.
And thank you for using this plugin :)
I understand. Great plugin! Thank you very much!
ReplyDeleteGreat!
ReplyDeleteI have a question.
I made a table and each cell has an id. I used this plugin and did the swap of the contents of cells. Turns out the id has not changed and I needed to change the id of the cells with the images. Is it possible?
I'm not sure I completely understand your request, but technically speaking this plugin is built on "Sortable" and inherits all "sortable" functions. So, you can add function Stop() and implement whatever you want there.
ReplyDelete$("#foo").swappable({
...
stop: function(event, ui) {
//modify id here
}
...
});
hope this helps.
Very good plugin! Just what i need.
ReplyDeleteAnd thanks for the last reply, it helped me a lot.
Hello Vadimk, Thanks a ton man. Very appreciated solution..... It's wonderful plugin and made my work so easy.
ReplyDeletethanks again...
I have few Question,
1. Can't we Swap with animation (slow move of elements)
2. "cursorAt: {top: -nn} ?what exactly "nn" does?
1) It's not easy to implement animation as internally it's not a simple position exchange between 2 elements. Elements (not only these two) make several relocations before they set their final position.
ReplyDelete2) negative cursorAt{} set cursor above the element you are drugging (the more nn, the more distance between cursor and top border of the element), so when you drop, event object can refer to the target element.
and thank you for using this plagin :)
Hi,
ReplyDeleteSwappable "cancel" -method is not working like it should. One of swappable elements gets reverted back to place it was before, but other element won't.
Is it bug?
Sorry, I don't quite understand: are you talking about my demo or your application? I've just checked demo and it works in all major browsers under Windows/Linux.
ReplyDeletePlease check how you implemented this plugin in your application. Thanks.
@vadimk, Thanks for answering my previous question.
ReplyDeleteHow to swap the ClassName including Elements if it's not similar (Drag_Element_Class & Target_Element_Class)
In your given example, There is only one class used for both DragElement and TargetElement.
Example : http://jsfiddle.net/bibhaw/vtBVR/47/
http://stackoverflow.com/questions/5659917/to-swap-elemntswith-classname-too-when-draged-dropped
Bibhaw,
ReplyDeletein your example elements behave exactly as they should behave based on the css rules you apply to them. You can add or remove classes or recalculate height/width with javascript on stop event:
$("#foo").swappable({
...
stop: function(event, ui) {
//modify elements here
}
...
});
Vadimk, that's correct but at least i need to know the className of both elements. I only want to know which function stores the Object's className/ID in your plugin
ReplyDelete$("#foo, #zoo").swappable({
items:('#foo,#zoo'),
cursorAt:-10,
stop: function(event, ui) {
Here I need to know the Drag_ID and Target_ID of elements.
if(dargElement.ID == TargetElement.ID)
{ Here No need to swap the className.
}
if (dragElement.ID != TargetElement.ID)
{
var current_ID = dragElement.ID;
dargElement.ID = TargetElement.ID;
TargetElement.ID = current_ID;
OR
can use toggle
}
}
Vadimk, I only want to know which function stores the Object's className/ID in your plugin
ReplyDelete$("#foo, #zoo").swappable({
......
stop: function(event, ui) {
if (dragElement.ID != TargetElement.ID)
{
var current_ID = dragElement.ID;
dargElement.ID = TargetElement.ID;
TargetElement.ID = current_ID;
OR can use toggle
}
}
=================================
ReplyDeletestop: function(event, ui) {
alert("drug: "+ ui.item[0].id + ", drop: "+ event.originalEvent.target.id);
}
===================================
In your demo add ids to elements and copy/paste this fragment.
ah thanks man. resolved now.
ReplyDeletereally appreciated. Thanks a ton
You are welcome,Bibhaw.
ReplyDeleteAnd thank you for using my plugin.
Hi vadmik, Once again i need your help. Please take a look
ReplyDeletehttp://stackoverflow.com/questions/5932379/does-anybody-know-about-vadims-jquery-plugin-swappable
Bibhow, I'm not sure I completely understand your question and your goal. It looks like you assign the same set of elements to the 3 different parent elements (foo,zoo,soo), right? And what do you need to compare? You already have drug_id/drop_id. What exactly do you want to have as a result?
ReplyDeletePlease see the question again, i have been updated it.
ReplyDeleteYOU?? It looks like you assign the same set of elements to the 3 different parent elements (foo,zoo,soo), right?
#foo {width: 100px, height:200px etc..}
#zoo {width: 150px, height:250px etc..}
#soo {width: 125px, height:225px etc..}
NO, There are different set of elements (width, height etc...), when i drag (element of #foo) over #zoo's element so therefor i need to change the height & width of both the elements.
in case of #soo is not swappable : $("#foo,#zoo").swappable({ ...}); #soo elements can't be drag but anyone can drop the #foo OR #zoo elements over #soo elements so in that case, we need to protect.
Bibhaw,
ReplyDeleteI don't see anything specific to this plagin in your question. From plugin you already have drug and drop ids; I don't think you need help to find parent elements, right?
This comment has been removed by the author.
ReplyDeleteoops, it seems you didn't understand my question. BTW thanks for your time.
ReplyDeleteID : #foo, #zoo, #soo
("#foo, #zoo").swappable( { ....
......
stop : ......
.............
How to authenticate...
drag_id,drop_id is elements of #foo, #zoo ???
OR
drag_id_parentId, drop_id_parentId is swappable and exist in [ ("#foo, #zoo")] ???
hi, Vadimk. thank you for your plugin. very nice!
ReplyDeletebut i've found a bug in it. reproduced in IE8.
see swappable list.
swap 1st and 3d elements such way that 1st element will be between 3d and 4th.
see screenshot:
http://xmages.net/storage/10/1/0/3/1/upload/086a9fe2.png
than when you drop the item, the items 1 and 3 will be swapped. But you will see no margins between 2, 1 and 4.
see screenshot:
http://xmages.net/storage/10/1/0/6/5/upload/7a61e2d9.png
it is not very critical when you have margin-bottom: 3px (as in your online example).
but when you have 10px or even more it could be look not very good.
could you please provide solution for this issue?
Thank you!
Vasily
Hi Vasily,
ReplyDeletethank you for using this plagin.
I see your point, but I'm afraid I wan't be able to look into the problem in the near future: I'm too busy with my current project. But if I find time I'll try to help you. Sorry, can't help you right now.
Again, thank you,
-Vadim
Hi,
ReplyDeleteI've found a solution for defect with disappearing margins.
It is hack, not very nice, but it works: we need to call any event before calling this._clear(event, noPropagation) function inside _mouseStop function:
see new code inside the comments:
} else {
/***************************************/
if ($.browser.msie) {
target.click();
}
/***************************************/
this._clear(event, noPropagation);
}
--
Vasily
Hi Vasiliy.
ReplyDeleteThank you for sharing your solution. I really appreciate it. Thank you.
Hi, great plugin by the way, just what I needed!
ReplyDeleteHow can I style the target element during dragging, e.g. when the pointer is over the target element?
Hi Richard.
ReplyDeleteThank you for using my plugin.
The simplest way to style target element is to use CSS :hover selector. Something like this:
li:hover
{
border:1px solid red;
color:blue;
}
I am trying to use swappable plugin to develop a web based iphone app. It doesnt work on iphone browser, though sortable works perfectly on it.
ReplyDeletecan you suggest any changes
Never tested it on iphone. No idea, sorry.
ReplyDeleteEvery time an item is moved, I would like to trigger a call to a function. How can I implement that using Swappable? I tried adding the "change: " detection found for "sortable" but no luck. Any advice/suggestions?
ReplyDeleteNM.... I got it.
ReplyDeleteI should have used "UPDATE" not "CHANGE".. As follows...
$("#swappable").swappable({
items: '.ui-state-default',
cursorAt: {top:-5},
update: function(sorted){
...(Run your functions here)
}
});
$("#swappable").disableSelection();
Hi,
ReplyDeleteHave you tried to get this to work on a tablet or mobile? cant seem to drag and drop this on touch enabled devices.
Hello.
ReplyDeleteI have been using this great plugin for a while but now trying it on iPad it did not work. Do you know if I am doing something wrong or if this plugin just doesn't work with iPad?
Many thanks,
Starki
Sorry, didn't check this branch for a while.
ReplyDeleteFrankly, I've never tested this plugin on mobiles or tablets. So I simply don't know if it works on these platforms or not.
Sorry.
Hi Vadim,
DeleteWould you be interested in getting this to work with tablets? It could be a job i'm willing to pay you for.
Hi Moiz,
DeleteThank you for your request. I'm afraid I don't have time to work on this plugin now as I am busy with other projects. Sorry.
Hello.
ReplyDeleteis connectWith possible?
I've never tested this feature. I think the best way to discover is to try it. Please let us know the result.
ReplyDeleteThank you,
-Vadim.
Hi, how can I get the plugin to work with touch events. I need it to work on tablets and mobile devices. Can you help ?
ReplyDeleteHi Moiz,
Deletethis plugin works with mouse events only. If you look into the code you see _mouseStart() and _mouseStop() functions, plus _clear() function to cleanup. All 3 functions are taken from "Sortable" plugin, customized and than added to the original one so that they overwrite original functions. If jQuery-mobile UI has "Sortable" plugin, you can customize it the same way.
But I'm afraid I can't help you with moving this plugin to mobile platform as I'm busy with other projects and don't have time for this development. Sorry.
Vadim, great plugin thanks! I've fixed the requirement to set the cursorAt property so now it has the same L&F as the sortable plugin. I posted my version to GitHub and gave you attribution for authorship. https://github.com/agoodno/jquery.swappable.js. I committed the original and then made my modification. I was hoping to get your approval on the change so you could assign it an official new version. Once again, thanks for the plugin, it saved me a lot of time.
ReplyDeleteNice job, Andy!
ReplyDeleteI created github repo
https://github.com/vkiryukhin/swappable.
You can fork the source, modify it and then (if you want) pull request to merge your version.
1. Add your name in the comment area at the top.
2. Get rid of "cursorAt" attribute in the examples and other pages.
3. I would include source of "jQuery Nearest plugin v1.2.0" directly into the swappable.js file to avoid one more dependency link.
Please test your changes in major browsers.
And thank you for making you code public!
Best regards,
-Vadim
This is great!
ReplyDeleteQ: How to update this to use with mysql? (in case that we want to save our ordering)
Aleksandar,
DeleteI already answered this question here, in this branch. Please take a look at my reply from April 21, 2011 (http://vadimkir.blogspot.com/2010/08/swappable-jquery-plugin.html?showComment=1303372364279#c4715000216053849224)
Simply use "Stop()" member function, where you can get elements' order and save it in your DB.
Best regards,
-Vadim
Vadim,
Deletethanks, is working :)
This comment has been removed by the author.
Deletelooks like if we use it like:
ReplyDelete$(".sortable").swappable({
placeholder: "ui-state-highlight",
items: '.ui-state-default',
cursorAt: {top:-1},
revert: true
});
it has some kind of animation but it doesn.t work properly...do you have any ideea why?
Sorry, no idea.
DeleteVadim, great work !
ReplyDeleteworks perfectly and the database!
But, when i want to put betwen li tag, div or table html tags, code stop to work..
How to fix this problem?
http://ebchost.info/drag/swappable_list.php
DeleteAt this link you will see what I mean, but also, on the right side of example, which shows the formations, you can see the new problem that i met when modifying the code (try to drag right side player and the GK) and for that reason more experienced please help ?
"ui-state-highlightt" css code is very difficult to menage for me.
Thanks forward
Hi vadimk,
ReplyDeleteYour plugin works really awesome. I just need to know how to make the ul swappable on a click and make it disable on another click. What I did here is:
/***********************************************************************************/
function swap_elements()
{
if($("#swappable > li").hasClass("ui-state-default"))
{
alert("Remove");
$("#swappable").removeClass("ui-sortable");
$("#swappable > li").removeClass("ui-state-default");
$("#swappable > li").css({"border":"none"});
$("#plan_list_id > ul").attr("id", "unswap");
}
else
{
alert('Add');
$("#plan_list_id > ul").attr("id", "swappable");
$("#swappable").addClass("ui-sortable");
$("#swappable > li").addClass("ui-state-default");
$("#swappable > li").css({"border":"1px solid #780030"});
$("#swappable").swappable({
items: '.ui-state-default',
cursorAt: {top:-5}
});
$("#swappable").disableSelection();
}
}
/***********************************************************************************/
This code doesn't let the li to be swappable after removing the id(swappable) and class(ui-sortable) from ul and removing class(ui-state-default) from all li elements. But all the li elements are still draggable. How to make them un-draggable too after removing the class? Any help regarding this would be really appreciated.
Thanks for the plugin though. Its really great. :)
Hi Anonymous,
DeleteI don't think you need to manipulate with classes. Enable / Disable functionality can be done this way:
--------------------------------------------------------
function toggleIt(){
var isDisabled = $( "#swappable" ).swappable( "option", "disabled" );
if( isDisabled ) {
$( "#swappable" ).swappable( "enable" );
} else {
$( "#swappable" ).swappable( "disable" );
}
}
-------------------------------------------------------------------
Best regards,
--Vadim
Just want to say thank you for this. This is exactly what I was looking for so I could implement a drag and drop combat button interface similar to those seen in many MMOs. I am implementing this in my web game at nevergrind.com. Thanks a ton!
ReplyDeleteYou are very welcome!
DeleteHi Vadimk,
ReplyDeleteI need to know the new location of the moved item, can I?
For example: a list of 10 elements as follows (100,500,200,040,999,22,88,11,10,5) I move the first element to the seventh position, how do I know that it was moved to the seventh position?
Thank you
Hi Vadimk. Thanks for the plugin! I was faced with some unexpected behaviour when trying to implement it. Everytime I swap two list items, an extra list item is appended to my unordered list. What am I missing?
ReplyDeleteHi Vadimk, Thanks for the plugin :), I have one issue - i am creating tables dynamically and applying the swappable to the td's it is working fine, but when i am creating multiple tables the swapping is happening between the two table (or more) td's. I need to happen the swapping only in one table and not across tables. Any help would be appreciated.
ReplyDeleteWeb design companies in Qatar
ReplyDeleteNice blog, more Find out Useful Free JQuery Plugins at amohits.
ReplyDeleteThis is such an interesting and informative article to share with others. Keep up the good work and more. SEO Agency In Dubai
ReplyDeleteThanks for sharing this great information. keep posting.
ReplyDeleteMedia Relation company in Dubai
Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
ReplyDeleteSelenium Training in Chennai Tamil Nadu | Selenium Training Institute in Chennai anna nagar | selenium training in chennai velachery
Selenium Training in Bangalore with placements | Best Selenium Training in Bangalore marathahalli
java training in chennai | java training in chennai Velachery |java training in chennai anna nagar
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteData Science Training in Chennai
Data Science training in kalyan nagar
Data science training in Bangalore
Data Science training in marathahalli
Data Science interview questions and answers
Data science training in jaya nagar
I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating for us.
ReplyDeletelg mobile repair
lg mobile service center near me
lg mobile service center in velachery
Thanks For Sharing The InFormation The Information Shared Is Very Valuable Please Keeep updating Us Time Just Went On reading the article Python Online Course Data Science Online Course Data Science Online Course Hadoop Online Course Awsw Online Course
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.MSBI Training in Bangalore
ReplyDeleteThanks For Sharing The Information The information shared Is Very Valuable Please Keep Updating Us Time just went On reading devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery
ReplyDeleteReally very informative post you shared here. Kindly keep blogging. It is really helpful for a newbie like me. this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners.
ReplyDeletepython training in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python flask training
python flask online training
python training in coimbatore
SitGulf is one of the best leading web designing company in qatar. We specialized in website designing, digital marketing, app development & SEO services.
ReplyDeletebest web design company in qatar!
continuously i used to read smaller articles that as well clear their motive, and that is also happening with this piece of writing which I am reading at this time.
ReplyDeleteIt's great that you are getting ideas from this piece of writing as well as from our dialogue made at this time.
ReplyDeleteinformative article.thank you
ReplyDeletebest-angular-training in chennai |
Great with detailed information. It is really very helpful for us.
ReplyDeleteVillage Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
As a best video production company in Bangalore, we produce quality and creative videos to our clients.
A dedicated group of imaginative thinkers makes up the team at
ReplyDeleteDigital Sea,a results-driven digital marketing business in Dubai.
We consider that progress cannot be fueled by facts alone.
A dedicated group of imaginative thinkers makes up the team at
ReplyDeleteAnata.digital,a results-driven digital marketing business in Australia
We consider that progress cannot be fueled by facts alone.
A dedicated group of imaginative thinkers makes up the team at
ReplyDeleteSolutions sector,a results-driven digital marketing business in Pakistan
We consider that progress cannot be fueled by facts alone.
Good content. You write beautiful things.
ReplyDeletekorsan taksi
hacklink
taksi
sportsbet
hacklink
vbet
mrbahis
mrbahis
vbet
This post is on your page i will follow your new content.
ReplyDeletecasino siteleri
sportsbet giriş
mrbahis
mrbahis giriş
sportsbet
sportsbetgiris.net
mrbahis.co
betgaranti.online
sportsbet
amasya
ReplyDeleteantakya
edirne
elazığ
kayseri
LS7PKT
salt likit
ReplyDeletesalt likit
RSHD
gebze
ReplyDeletebolu
bağcılar
başakşehir
kartal
JİXUA
muğla
ReplyDeleteümraniye
manavgat
karşıyaka
didim
GJTD
muğla
ReplyDeleteümraniye
manavgat
karşıyaka
didim
2ECHV
شركة انشاء مسابح
ReplyDeleteشركة انشاء مسابح بجدة
Great post! I learned a lot from reading this.
ReplyDeleteSEO, PPC, SMM, Web Design & Development Agency Dubai
Hire a Expert Advocate to Solve Your Family Suit in Pakistan. Our Main Family Services are Given Below:
ReplyDeleteKhula Case in Pakistan
Divorce Procedure
Talaq Procedure
Hire a Quick Process Server Onlinefor Attested and server your legal Papers. We are one of the Best and Top Lawyer for quick Process Server Services.
ReplyDeleteشركة صيانة وتنظيف افران
ReplyDeleteصيانة وتنظيف افران
شركة فك وتركيب مطابخ
ReplyDeleteفك وتركيب مطابخ
شركة كشف تسربات المياه بالرياض
ReplyDeleteشركة تسليك مجارى بالرياض
شركة مقاولات كهرباء بالرياض
شركة مقاولات سباكه بالرياض
شركة تسليك مجاري في خورفكان 2cY4yi77tF
ReplyDelete