Home > Web Front-end > JS Tutorial > body text

Solution to save state after sortable sorting in jquery_jquery

WBOY
Release: 2016-05-16 18:35:48
Original
1954 people have browsed it

At that time, I copied a few sentences from the official website of jquery to get started, but later I found that it was not very useful and it was quite slow. Sweat~~~ I didn’t figure out how to save the sorted results until this morning. It just so happened that the company was going to build such a function, so I learned it all over again.
First of all, I found similar code on jquery’s official demo (http://jqueryui.com/demos/). What I want is called portlets (http://jqueryui.com/demos/sortable/portlets.html). Just like the igoogle homepage. It seems to be very simple. After adding all the js to be quoted, then a few lines of code are done.

Copy code The code is as follows:



html code omitted... Please view the demo for details

After writing these, you can try dragging. Do you feel a sense of accomplishment? Not bad, young man, there is a future. sortable has many parameters, go to the official website for details! Let's just talk about what connectWith:'.column' means here. It means that any portlet with class column can drag the portlet of one column to another column. Just try it and you will know. Of course, the focus today is not how to drag it, but that refreshing after dragging still preserves the current order.

You have encountered a little difficulty, but you have to do it, otherwise no girl will marry you in the future! ^_^ So I started to Google Baidu. Some people say that you can get an ID array using the serialize method of sortable, but unfortunately, I didn't get it. If you do it, please tell me; some people say that you can also get the ID array using the toArray method. I did get it this time. But something very nasty happened.

$('.column').sortable('toArray');

In this way, you can only get the ID array whose first class is column. Use each()? I tried , but not work; Maybe you can do it, please tell me! So I can only turn to other methods. Maybe you will say, isn't this simple? Wouldn't it be OK to just save the layout of the entire web page? Haha, I also think so! Through the iedeveloper debugging tool, I found that after dragging, they found changes. It was not the style that changed, but the order of the divs. If I save the entire content, it seems to work, but the amount is a bit large and not suitable for dynamic content. What should I do, so I thought, wouldn’t it be O if I just save their IDs in order? So I gave each of them an ID.

Everything is difficult at the beginning. After you have this idea, have you opened the toilet? I'll go take a rest first. Thank you for opening the door. I'll be right back!

Let’s follow this idea step by step. The first is to get all the columns.

$.each($(".column"), function(m) {}

Then find the portlet under each column;

$.each($(".column"), function(m) {
$.each($(this).children(".portlet"), function(d) {
}

Then save them in your own way
Copy the code The code is as follows:

function saveLayout(){
var list = "";
$.each($(".column"), function(m) {
list = $(this).attr( 'id') ":";
$.each($(this).children(".portlet"), function(d) {
list = $(this).attr('id') " @";
})
list = "|";
})
$.cookie("list", list)}

Another method is also used here One component jquery.cookie

Change the starting
Copy the code The code is as follows:

$(".column").sortable({
connectWith: '.column',
stop: saveLayout
});

stop refers to dragging Events triggered after the end.

Finally, it is read into the container in order. I won’t say much here, I can only understand it, not explain it in words:
Copy code The code is as follows:

var list = $.cookie("list"); //Get the list in the cookie Value
//alert(list)
var arrColumn = list.split('|');
$.each(arrColumn, function(m, n) {
var elemId = n.split (':')[0]; //Container ID
var arrRow = n.split(':')[1] ? n.split(':')[1].split('@') : ""; //Single sequence ID
$.each(arrRow, function(m, n) {
if (n) {//Exclude null values
$("#" elemId).append( $("#sb" n).attr('id', n))//Add the sequence into the container
}
});
})

Okay, that’s it for today. It took me an hour to type and typeset, and the company gave me 16 RMB per hour. So BYE!
If you have any questions, please don’t ask me, I’m very busy. Go to QQ group 5678537 to find other people to discuss!

Demo codehttp://demo.jb51.net/js/Sortable/Sortable.htm
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template