Home Web Front-end JS Tutorial jquery implements drag sorting of elements (code attached)

jquery implements drag sorting of elements (code attached)

Apr 26, 2018 am 09:28 AM
jquery drag sort

This time I will bring you jquery to implement element drag sorting (with code). What are the precautions for jquery to implement element drag sorting? . The following is a practical case, let's take a look.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>jquery学习-jquery对元素拖动排序</title> 
<style type="text/css"> 
#show 
{ 
color: Red; 
} 
#list 
{ 
cursor: move; 
width: 300px; 
} 
#list li 
{ 
border: solid 1px yellow; 
float: left; 
list-style-type
: none; 
margin-top: 10px;
Copy after login


Below, we will implement this function step by step.

<span id="show">
<p>
  <input id="check" type="checkbox" />
</p>
<p>
  <input type="hidden" id="orderlist" />
  <ul id="list">
    <asp:Repeater ID="rptOrder" runat="server">
    <ItemTemplate>
      <li id="<%#Eval("ID") %>" title="<%#Eval("OrderID") %>">
        <img alt="img" src="<%#Eval("Link") %>" />
      </li>
    </ItemTemplate>
    </asp:Repeater>   
  </ul>
</p>
Copy after login

There is a radio button. When the user selects it, the sorting of the data in the database is changed when the picture is dragged. The hidden field saves the original order of pictures. ul displays the picture list.

In order to make it easier to see, I added a little style:

var show = jQuery("#show"); //输出提示 
var orderlist = jQuery("#orderlist"); //原顺序 
var check = jQuery("#check"); //是否更新到数据库
Copy after login

First save the commonly used selectors so that subsequent calls will become simpler. Everyone will definitely have no problem with this one. ^_^

//保存原来的排列顺序 
var order = []; 
list.children("li").each(function() { 
  order.push(this.title); //原排列顺序保存在title,得到后更改title 
  jQuery(this).attr("title", "你可以拖动进行排序"); 
}); 
orderlist.val(order.join(&#39;,&#39;));
Copy after login

Save the original sort order to the hidden field. The push() method of the array is used here, which is to add the title (original arrangement order) in each li of ul to the array. Finally, use the join() method to get the original arrangement order and return a string. The sort order format is now 1,2,3.

//ajax更新 
var Update = function(itemid, itemorder) { 
  jQuery.ajax({ 
    type: "post", 
    url: "update.aspx", 
    //id:新的排列对应的ID,order:原排列顺序
    data: { id: itemid, order: orderlist.val() },  
    beforeSend: function() { 
      show.html("正在更新"); 
    }, 
    success: function() { 
      show.html("更新成功"); 
    } 
  }); 
};
Copy after login

Next, separate the ajax update block separately. This way the program becomes cleaner and there is nothing new in this area.

//调用ajax更新方法 
var Submit = function(update) { 
  var order = []; 
  list.children("li").each(function() { 
    order.push(this.id); 
  }); 
  var itemid = order.join(&#39;,&#39;); 
  //如果单选框选中,则更新表中排列顺序 
  if (update) { 
    Update(itemid); 
  } 
  else { 
    show.html(""); 
  } 
};
Copy after login

Similar to getting the sort order, the ID is formed into a string and passed to the Update() method. The parameter update in the function is whether the checkbox is selected.

//执行排列操作 
list.sortable({ 
  opacity: 0.7, 
  update: function() { 
    Submit(check.attr("checked")); 
  } 
});
} 
</style> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.js"></script> 
<script type="text/javascript"> 
$(document).ready(function () { 
//保存常用选择器 
var list = $("#list"); //ul 
var show = $("#show"); //输出提示 
var orderlist = $("#orderlist"); //原顺序 
var check = $("#check"); //是否更新到数据库 
//保存原来的排列顺序 
var order = []; 
list.children("li").each(function () { 
order.push(this.title); //原排列顺序保存在title,得到后更改title 
$(this).attr("title", "你可以拖动进行排序"); 
}); 
orderlist.val(order.join()); 
//执行排列操作 
list.sortable({ 
axis: &#39;y&#39;,//只能横向拖动 
opacity: 0.7,// 移动时的透明度 
update: function () {//当排序动作结束时且元素坐标已经发生改变时触发此事件。 
Submit(check.attr("checked")); 
} 
}); 
//ajax更新 
var Update = function (itemid, itemorder) { 
$.ajax({ 
type: "post", 
url: "update.aspx", 
data: { id: itemid, order: orderlist.val() }, //id:新的排列对应的ID,order:原排列顺序 
beforeSend: function () { 
show.html("正在更新"); 
}, 
success: function (req) { 
if (req == "100") { 
show.html("更新成功"); 
} 
else if (req == "001") { 
show.html("失败,请稍后再试"); 
} 
else { 
show.html("参数不全"); 
} 
} 
}); 
}; 
//调用ajax更新方法 
var Submit = function (update) { 
var order = []; 
list.children("li").each(function () { 
order.push(this.id); 
}); 
var itemid = order.join(&#39;,&#39;); 
//如果单选框选中,则更新表中排列顺序 
if (update) { 
Update(itemid); 
} 
else { 
show.html(""); 
} 
}; 
}); 
</script> 
</head> 
<body> 
<form method="post" action="jquery-drag-order-sort.aspx" id="form1"> 
<p class="aspNetHidden"> 
<input type="hidden" name="VIEWSTATE" id="VIEWSTATE" value="/wEPDwUJNDc3MzMwNjM4D2QWAgIBD2QWAgIBDxYCHgtfIUl0ZW1Db3VudAIDFgZmD2QWAmYPFQMCMTQBMSdodHRwOi8vd3d3LmJhaWR1LmNvbS9pbWcvYmFpZHVfbG9nby5naWZkAgEPZBYCZg8VAwIxMwEyL2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS5oay9pbWFnZXMvc3Jwci9sb2dvM3cucG5nZAICD2QWAmYPFQMCMTYBMyxodHRwOi8vaW1nMy5jbi5tc24uY29tL2ltYWdlcy8wODA5L2xvZ28xLnBuZ2RkDx67fZ2swhZiUjvFaE+ziATRZTct5b77PuWvqXLCUlg=" /> 
</p> 
<span id="show"></span> 
<h1>jQuery对元素拖动排序</h1> 
<p>拖动时同时更新数据库数据:<input type="checkbox" id="check" /></p> 
<p> 
<input type="hidden" id="orderlist" /> 
<ul id="list"> 
<li id="14" title="1"> 
<img alt="img" src="http://www.baidu.com/img/baidu_logo.gif" /></li> 
<li id="13" title="2"> 
<img alt="img" src="http://www.google.com.hk/images/srpr/logo3w.png" /></li> 
<li id="16" title="3"> 
<img alt="img" src="http://img3.cn.msn.com/images/0809/logo1.png" /></li> 
</ul> 
</p> 
</form> 
</body> 
</html>
Copy after login

Finally, perform the arrangement operation. The background part is the update of the current ID corresponding to the original arrangement order. I believe everyone is familiar with it.

It can be seen that if database operation is not performed, the plug-in only needs to call sorttable to complete the dragging of elements.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

detailed explanation of the steps to implement table sorting using sortElements

jquery implements table sorting real-time search function

The above is the detailed content of jquery implements drag sorting of elements (code attached). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sort WPS scores How to sort WPS scores Mar 20, 2024 am 11:28 AM

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

How to sort in excel How to sort in excel Mar 05, 2024 pm 04:12 PM

Sorting methods in excel: 1. Single column sorting; 2. Multiple column sorting; 3. Custom sorting. Detailed introduction: 1. Single-column sorting is the most common sorting method. It sorts according to a selected column; 2. Multi-column sorting refers to sorting data in multiple columns, usually sorting according to a certain column first. On the basis of, sort by another column; 3. Custom sorting, allowing users to define the sort order according to their own needs.

How to sort WPS tables to facilitate data statistics How to sort WPS tables to facilitate data statistics Mar 20, 2024 pm 04:31 PM

WPS is a very complete office software, including text editing, data tables, PPT presentations, PDF formats, flow charts and other functions. Among them, the ones we use most are text, tables, and demonstrations, and they are also the ones we are most familiar with. In our study work, we sometimes use WPS tables to make some data statistics. For example, the school will count the scores of each student. If we have to manually sort the scores of so many students, it will be really a headache. In fact, we don’t have to worry, because our WPS table has a sorting function to solve this problem for us. Next, let’s learn how to sort WPS together. Method steps: Step 1: First we need to open the WPS table that needs to be sorted

How to reorder multiple columns in Power Query via drag and drop How to reorder multiple columns in Power Query via drag and drop Mar 14, 2024 am 10:55 AM

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to sort values ​​in an array by size in PHP How to sort values ​​in an array by size in PHP Mar 22, 2024 pm 05:24 PM

PHP is a commonly used server-side scripting language widely used in website development and data processing fields. In PHP, it is a very common requirement to sort the values ​​in an array by size. By using the built-in sort function, you can easily sort arrays. The following will introduce how to use PHP to sort the values ​​in an array by size, with specific code examples: 1. Sort the values ​​in the array in ascending order:

See all articles