`
haoningabc
  • 浏览: 1465622 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jquery table拖拽排序

 
阅读更多
参考http://dragsort.codeplex.com/

<html>
<head>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
	<script type="text/javascript" src="jquery.dragsort-0.4.min.js"></script>
</head>
<body>
	<table id="list1" border="1">
		<thead id="gridThead">
				<tr style="cursor: pointer;">
					<td class="listHeadCell">编号</td>
					<td class="listHeadCell">名称</td>
				</tr>
		</thead>
		<tbody id="gridtbody">
	        <tr>
		        <td name="biaozhi">1</td>
		        <td>名称1</td>
	        </tr>   
	        <tr>    
		        <td name="biaozhi">2</td>
		        <td>名称2</td>
	        </tr>   
	        <tr>    
		        <td name="biaozhi">3</td>
		        <td>名称3</td>
	        </tr>   
		    <tr>    
		        <td name="biaozhi">4</td>
		        <td>名称4</td>
	        </tr>
	    </tbody>
    </table>
	<br/>
	<textarea name="listhaoSortOrder" id="listhaoSortOrder" rows=10 cols=100></textarea>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#gridtbody").dragsort({ itemSelector: "tr", dragSelector: "tr", dragBetween: true,dragEnd: saveOrder1, placeHolderTemplate: "<tr></tr>" });
		});
		function saveOrder1() {
		//	var data = $("#gridtbody").map(function() { return $(this).children().html(); }).get();
			var data = $("[name='biaozhi']").map(function() { return $(this).html(); }).get();
			$("#listhaoSortOrder").val(data.join("|"));
		};
	</script>
</body>
</html>



jquery.dragsort-0.4.min.js:
// jQuery List DragSort v0.4
// Website: http://dragsort.codeplex.com/
// License: http://dragsort.codeplex.com/license

(function($) {

	$.fn.dragsort = function(options) {
		var opts = $.extend({}, $.fn.dragsort.defaults, options);
		var lists = [];
		var list = null, lastPos = null;
		if (this.selector)
			$("head").append("<style type='text/css'>" + (this.selector.split(",").join(" " + opts.dragSelector + ",") + " " + opts.dragSelector) + " { cursor: pointer; }</style>");

		this.each(function(i, cont) {

			if ($(cont).is("table") && $(cont).children().size() == 1 && $(cont).children().is("tbody"))
				cont = $(cont).children().get(0);

			var newList = {
				draggedItem: null,
				placeHolderItem: null,
				pos: null,
				offset: null,
				offsetLimit: null,
				scroll: null,
				container: cont,

				init: function() {
					$(this.container).attr("data-listIdx", i).mousedown(this.grabItem).find(opts.dragSelector).css("cursor", "pointer");
					$(this.container).children(opts.itemSelector).each(function(j) { $(this).attr("data-itemIdx", j); });
				},

				grabItem: function(e) {
					if (e.which != 1 || $(e.target).is(opts.dragSelectorExclude))
						return;

					var elm = e.target;
					while (!$(elm).is("[data-listIdx='" + $(this).attr("data-listIdx") + "'] " + opts.dragSelector)) {
						if (elm == this) return;
						elm = elm.parentNode;
					}

					if (list != null && list.draggedItem != null)
						list.dropItem();

					$(e.target).css("cursor", "move");

					list = lists[$(this).attr("data-listIdx")];
					list.draggedItem = $(elm).closest(opts.itemSelector);
					var mt = parseInt(list.draggedItem.css("marginTop"));
					var ml = parseInt(list.draggedItem.css("marginLeft"));
					list.offset = list.draggedItem.offset();
					list.offset.top = e.pageY - list.offset.top + (isNaN(mt) ? 0 : mt) - 1;
					list.offset.left = e.pageX - list.offset.left + (isNaN(ml) ? 0 : ml) - 1;

					if (!opts.dragBetween) {
						var containerHeight = $(list.container).outerHeight() == 0 ? Math.max(1, Math.round(0.5 + $(list.container).children(opts.itemSelector).size() * list.draggedItem.outerWidth() / $(list.container).outerWidth())) * list.draggedItem.outerHeight() : $(list.container).outerHeight();
						list.offsetLimit = $(list.container).offset();
						list.offsetLimit.right = list.offsetLimit.left + $(list.container).outerWidth() - list.draggedItem.outerWidth();
						list.offsetLimit.bottom = list.offsetLimit.top + containerHeight - list.draggedItem.outerHeight();
					}

					var h = list.draggedItem.height();
					var w = list.draggedItem.width();
					var orig = list.draggedItem.attr("style");
					list.draggedItem.attr("data-origStyle", orig ? orig : "");
					if (opts.itemSelector == "tr") {
						list.draggedItem.children().each(function() { $(this).width($(this).width()); });
						list.placeHolderItem = list.draggedItem.clone().attr("data-placeHolder", true);
						list.draggedItem.after(list.placeHolderItem);
						list.placeHolderItem.children().each(function() { $(this).css({ borderWidth:0, width: $(this).width() + 1, height: $(this).height() + 1 }).html("&nbsp;"); });
					} else {
						list.draggedItem.after(opts.placeHolderTemplate);
						list.placeHolderItem = list.draggedItem.next().css({ height: h, width: w }).attr("data-placeHolder", true);
					}
					list.draggedItem.css({ position: "absolute", opacity: 0.8, "z-index": 999, height: h, width: w });

					$(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); });

					list.scroll = { moveX: 0, moveY: 0, maxX: $(document).width() - $(window).width(), maxY: $(document).height() - $(window).height() };
					list.scroll.scrollY = window.setInterval(function() {
						if (opts.scrollContainer != window) {
							$(opts.scrollContainer).scrollTop($(opts.scrollContainer).scrollTop() + list.scroll.moveY);
							return;
						}
						var t = $(opts.scrollContainer).scrollTop();
						if (list.scroll.moveY > 0 && t < list.scroll.maxY || list.scroll.moveY < 0 && t > 0) {
							$(opts.scrollContainer).scrollTop(t + list.scroll.moveY);
							list.draggedItem.css("top", list.draggedItem.offset().top + list.scroll.moveY + 1);
						}
					}, 10);
					list.scroll.scrollX = window.setInterval(function() {
						if (opts.scrollContainer != window) {
							$(opts.scrollContainer).scrollLeft($(opts.scrollContainer).scrollLeft() + list.scroll.moveX);
							return;
						}
						var l = $(opts.scrollContainer).scrollLeft();
						if (list.scroll.moveX > 0 && l < list.scroll.maxX || list.scroll.moveX < 0 && l > 0) {
							$(opts.scrollContainer).scrollLeft(l + list.scroll.moveX);
							list.draggedItem.css("left", list.draggedItem.offset().left + list.scroll.moveX + 1);
						}
					}, 10);

					list.setPos(e.pageX, e.pageY);
					$(document).bind("selectstart", list.stopBubble); //stop ie text selection
					$(document).bind("mousemove", list.swapItems);
					$(document).bind("mouseup", list.dropItem);
					if (opts.scrollContainer != window)
						$(window).bind("DOMMouseScroll mousewheel", list.wheel);
					return false; //stop moz text selection
				},

				setPos: function(x, y) {
					var top = y - this.offset.top;
					var left = x - this.offset.left;

					if (!opts.dragBetween) {
						top = Math.min(this.offsetLimit.bottom, Math.max(top, this.offsetLimit.top));
						left = Math.min(this.offsetLimit.right, Math.max(left, this.offsetLimit.left));
					}

					this.draggedItem.parents().each(function() {
						if ($(this).css("position") != "static" && (!$.browser.mozilla || $(this).css("display") != "table")) {
							var offset = $(this).offset();
							top -= offset.top;
							left -= offset.left;
							return false;
						}
					});

					if (opts.scrollContainer == window) {
						y -= $(window).scrollTop();
						x -= $(window).scrollLeft();
						y = Math.max(0, y - $(window).height() + 5) + Math.min(0, y - 5);
						x = Math.max(0, x - $(window).width() + 5) + Math.min(0, x - 5);
					} else {
						var cont = $(opts.scrollContainer);
						var offset = cont.offset();
						y = Math.max(0, y - cont.height() - offset.top) + Math.min(0, y - offset.top);
						x = Math.max(0, x - cont.width() - offset.left) + Math.min(0, x - offset.left);
					}
					
					list.scroll.moveX = x == 0 ? 0 : x * opts.scrollSpeed / Math.abs(x);
					list.scroll.moveY = y == 0 ? 0 : y * opts.scrollSpeed / Math.abs(y);

					this.draggedItem.css({ top: top, left: left });
				},
				
				wheel: function(e) {
					if (($.browser.safari || $.browser.mozilla) && list && opts.scrollContainer != window) {
						var cont = $(opts.scrollContainer);
						var offset = cont.offset();
						if (e.pageX > offset.left && e.pageX < offset.left + cont.width() && e.pageY > offset.top && e.pageY < offset.top + cont.height()) {
							var delta = e.detail ? e.detail * 5 : e.wheelDelta / -2;
							cont.scrollTop(cont.scrollTop() + delta);
							e.preventDefault();
						}
					}
				},

				buildPositionTable: function() {
					var item = this.draggedItem == null ? null : this.draggedItem.get(0);
					var pos = [];
					$(this.container).children(opts.itemSelector).each(function(i, elm) {
						if (elm != item) {
							var loc = $(elm).offset();
							loc.right = loc.left + $(elm).width();
							loc.bottom = loc.top + $(elm).height();
							loc.elm = elm;
							pos.push(loc);
						}
					});
					this.pos = pos;
				},

				dropItem: function() {
					if (list.draggedItem == null)
						return;

					$(list.container).find(opts.dragSelector).css("cursor", "pointer");
					list.placeHolderItem.before(list.draggedItem);

					list.draggedItem.attr("style", list.draggedItem.attr("data-origStyle")).removeAttr("data-origStyle");
					list.placeHolderItem.remove();

					$("[data-dropTarget]").remove();

					window.clearInterval(list.scroll.scrollY);
					window.clearInterval(list.scroll.scrollX);

					var changed = false;
					$(lists).each(function() {
						$(this.container).children(opts.itemSelector).each(function(j) {
							if (parseInt($(this).attr("data-itemIdx")) != j) {
								changed = true;
								$(this).attr("data-itemIdx", j);
							}
						});
					});
					if (changed)
						opts.dragEnd.apply(list.draggedItem);
					list.draggedItem = null;
					$(document).unbind("selectstart", list.stopBubble);
					$(document).unbind("mousemove", list.swapItems);
					$(document).unbind("mouseup", list.dropItem);
					if (opts.scrollContainer != window)
						$(window).unbind("DOMMouseScroll mousewheel", list.wheel);
					return false;
				},

				stopBubble: function() { return false; },

				swapItems: function(e) {
					if (list.draggedItem == null)
						return false;

					list.setPos(e.pageX, e.pageY);

					var ei = list.findPos(e.pageX, e.pageY);
					var nlist = list;
					for (var i = 0; ei == -1 && opts.dragBetween && i < lists.length; i++) {
						ei = lists[i].findPos(e.pageX, e.pageY);
						nlist = lists[i];
					}

					if (ei == -1 || $(nlist.pos[ei].elm).attr("data-placeHolder"))
						return false;

					if (lastPos == null || lastPos.top > list.draggedItem.offset().top || lastPos.left > list.draggedItem.offset().left)
						$(nlist.pos[ei].elm).before(list.placeHolderItem);
					else
						$(nlist.pos[ei].elm).after(list.placeHolderItem);

					$(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); });
					lastPos = list.draggedItem.offset();
					return false;
				},

				findPos: function(x, y) {
					for (var i = 0; i < this.pos.length; i++) {
						if (this.pos[i].left < x && this.pos[i].right > x && this.pos[i].top < y && this.pos[i].bottom > y)
							return i;
					}
					return -1;
				},

				createDropTargets: function() {
					if (!opts.dragBetween)
						return;

					$(lists).each(function() {
						var ph = $(this.container).find("[data-placeHolder]");
						var dt = $(this.container).find("[data-dropTarget]");
						if (ph.size() > 0 && dt.size() > 0)
							dt.remove();
						else if (ph.size() == 0 && dt.size() == 0)
							$(this.container).append(list.placeHolderItem.clone().removeAttr("data-placeHolder").attr("data-dropTarget", true));
					});
				}
			};

			newList.init();
			lists.push(newList);
		});

		return this;
	};

	$.fn.dragsort.defaults = {
		itemSelector: "li",
		dragSelector: "li",
		dragSelectorExclude: "input, textarea, a[href]",
		dragEnd: function() { },
		dragBetween: false,
		placeHolderTemplate: "<li>&nbsp;</li>",
		scrollContainer: window,
		scrollSpeed: 5
	};

})(jQuery);
分享到:
评论

相关推荐

    jQuery实现拖动布局并将排序结果保存到数据库

    总结来说,这个项目通过`jQuery`实现了用户友好的拖动排序功能,利用`idrag.js`插件简化了拖放操作的实现。当用户完成排序后,通过AJAX将新的布局信息发送到服务器,并使用`PHP`更新数据库,确保布局的更改能够持久...

    bootstrap-table.css 表格拖拽排序

    在这个特定的场景中,`bootstrap-table.css`可能包含了拖拽排序的样式规则,如拖动行时的高亮效果、悬浮指示器等,使得交互过程更加直观。 接着,`jquery.tablednd.js`是基于jQuery的TableDnD库,实现了表格行的...

    经过修改,适合bootstrap table拖动排序的jQuery-UI sortable

    经过修改,适合bootstrap table拖动排序的jQuery-UI sortable

    jQuery表格列自由拖动排序.zip

    &lt;h1 class="datouwang"&gt;jQuery表格列自由拖动排序 &lt;table id="tableSort"&gt; 点击排序"&gt; 序列 点击排序"&gt; 名称 点击排序"&gt; 数量 点击排序"&gt; 单价(Q点) 点击排序"&gt; 总计(Q点) &lt;td&gt;1 农场话费A ...

    实现table表格可按行拖拽,按列排序,并可以保存排序后的结果

    "实现table表格可按行拖拽,按列排序,并可以保存排序后的结果"是一个常见的需求,尤其适用于数据展示和管理的场景。这里我们将详细探讨如何利用jQuery、jQuery.tablesorter和jQuery.tablednd_0_5这三个JavaScript库...

    bootstraptable-reorder-columns表格拖拽排序列

    "bootstraptable-reorder-columns" 是这个库的一个扩展插件,专门用于实现表格列的拖拽排序功能。用户可以通过简单的拖放操作来调整表格列的顺序,极大地提高了用户在查看和管理大量数据时的灵活性和便利性。 在Web...

    针对后台列表table拖拽比较实用的jquery拖动排序

    一个用于实现拖拽排序的表格通常包含一个 `&lt;table&gt;` 元素,其中 `&lt;thead&gt;` 部分定义表头,而 `&lt;tbody&gt;` 部分则包含可排序的行 `&lt;tr&gt;`。为了能够拖动整个行,表头和行内每个单元格通常会设置指针样式(`cursor:...

    jQuery UI拖拽信息到Table表格特效

    **jQuery UI 拖拽信息到Table表格特效详解** 在网页开发中,用户交互体验是至关重要的,而jQuery UI库提供了丰富的组件和效果,其中拖放功能(Drag and Drop)可以显著提升用户的操作便捷性。本篇文章将详细介绍...

    jQuery表格列表拖动排序代码.zip

    《jQuery表格列表拖动排序详解》 在网页开发中,数据展示往往以表格或列表的形式进行,为了提高用户体验,让用户能根据需求自定义排列顺序,实现表格列表的拖动排序功能变得至关重要。jQuery,作为一款强大的...

    jQuery UI拖拽到Table表格特效.zip

    在“jQuery UI拖拽到Table表格特效”中,我们可以将页面上的任意元素(如文本、图片或者自定义对象)拖动到表格的特定单元格内,实现动态添加数据或调整数据布局的效果。这个特效的核心在于jQuery UI的`draggable`和...

    表格拖拽排序插件 Table Drag and Drop JQuery plugin v0.7

    表格拖拽排序插件 Table Drag and Drop JQuery plugin v0.7 最新0.7版本

    jQuery实现table中的tr上下移动并保持序号不变

    本示例主要关注如何利用jQuery实现在HTML表格(table)中移动行(tr)的同时保持行内的序号不变,这对于数据排序或用户交互有着重要的应用。我们将探讨以下关键知识点: 1. jQuery选择器与DOM操作: jQuery提供了...

    table列排序、列拖动、列宽度变化、行交换

    "table列排序、列拖动、列宽度变化、行交换"这些功能是网页数据展示中常见的需求,能够极大地提升用户操作的便捷性和数据管理的有效性。下面我们将详细探讨这些知识点。 首先,**表头列排序**是一种常见于数据表格...

    可拖动、排序的表格非常灵活,样式也很好看

    `sort_table.html`、`SortTable.js` 和 `DragTable.js` 这三个文件分别对应了页面结构、排序功能和拖动功能的实现。通过这些技术,我们可以创建出既美观又实用的数据管理工具,提升用户的使用体验。

    JQuery-tableDnD 拖拽的基本使用 Table Drag and Drop JQuery plugin

    `JQuery-tableDnD` 是一个用于实现表格元素拖放功能的 jQuery 插件,它使得用户可以通过鼠标拖拽来重新排序表格行,从而增强交互性和用户体验。在这个插件的帮助下,开发人员可以轻松地创建具有动态排序功能的数据...

    基于jQuery 开发的几个组件 (Table, Grid ,Menu ,Layout)

    本文将深入探讨如何使用jQuery开发四个关键的前端组件:Table、Grid、Menu和Layout,以提高Web应用的用户体验和交互性。 1. jQuery Table组件 jQuery Table组件用于展示结构化的数据,例如报表或数据库查询结果。...

    bootstrap-table拖拽表格改变列宽

    在“bootstrap-table拖拽表格改变列宽”这个主题中,我们将深入探讨如何实现用户可以动态调整表格列宽的功能,同时确保兼容主流浏览器,并保持表格的body部分与header部分对齐。 首先,我们需要引入必要的CSS和JS库...

    jQuery表格自定义拖到列表排序代码

    本教程将详细讲解如何利用jQuery实现表格中的自定义拖动排序功能,即“jQuery表格自定义拖到列表排序代码”。 首先,理解基本概念: 1. **jQuery库**:jQuery是由John Resig于2006年创建的,它通过提供简洁易用的...

    仿Excel样式的jquery表格排序插件

    这是一款仿Excel样式的jquery表格排序插件。该jquery表格排序插件基于bootstrap表格,它表格列支持按英文字母或数字的升序和降序排列,支持在表格列中按关键字进行搜索,支持过滤不需要的表格列。

Global site tag (gtag.js) - Google Analytics