jQuery实现拖动布局 排序结果可以保存到数据库 刷新后为更新后的排序
本站已经发布过垂直拖动排序的文章《jquery添加和删除数据 无刷新,拖动改变排序,jquery无刷新编辑内容并提交》
这个是可以多行多列任意位置拖动
SQL Code
- CREATE TABLE IF NOT EXISTS `sortlist` (
- `id` int(11) NOT NULL auto_increment,
- `sort` varchar(100) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
- --
- INSERT INTO `sortlist` (`id`, `sort`) VALUES
- (1, '1,2,6,5,3,4,7,8,9,10,11,12');
index.php
PHP Code
- <?php
- $query=mysql_query("select * from sortlist where id=1");
- if($rs=mysql_fetch_array($query)){
- $sort=$rs['sort'];
- }
- $sort_arr=explode(",",$sort);
- $len=count($sort_arr);
- ?>
- <!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实现拖动布局 排序结果可以保存到数据库 刷新后为更新后的排序</title>
- <link rel="stylesheet" type="text/css" href="../css/main.css" />
- <style type="text/css">
- #module_list{margin-left:4px}
- .modules{float:left; width:200px; height:140px; margin:10px; overflow:hidden; border:1px solid #acc6e9; background:#e8f5fe}
- .m_title{height:24px; line-height:24px; background:#afc6e9}
- #loader{height:24px; text-align:center}
- .clear{clear:both}
- </style>
- <script type="text/javascript" src="../../js/jquery-1.4.3.js"></script>
- <script type='text/javascript' src='jquery-ui.min.js'></script>
- <script type="text/javascript" src="idrag.js"></script>
- <link rel="stylesheet" type="text/css" href="../demo.css">
- </head>
- <body>
- <div id="main">
- <div id="loader"></div>
- <div id="module_list">
- <input type="hidden" id="orderlist" value="<?php echo $sort;?>" />
- <?php
- for($i=0;$i<$len;$i++){
- ?>
- <div class="modules" title="<?php echo $sort_arr[$i]; ?>">
- <h3 class="m_title">Module:<?php echo $sort_arr[$i]; ?></h3>
- <p><?php echo $sort_arr[$i]; ?></p>
- </div>
- <?php } ?>
- <div class="clear"></div>
- </div>
- </div>
- </div>
- </body>
- </html>
update.php
PHP Code
- $order = $_POST['order'];
- //echo $order;
- $itemid = trim($_POST['id']);
- if (!emptyempty ($itemid)) {
- if ($order != $itemid) {
- $query = mysql_query("update sortlist set sort='$itemid' where id=1");
- if ($query) {
- echo $itemid;
- } else {
- echo "none";
- }
- }
- }
JavaScript Code
- // JavaScript Document
- $(function(){
- $(".m_title").bind('mouseover',function(){
- $(this).css("cursor","move")
- });
- var $show = $("#loader"); //进度条
- var $orderlist = $("#orderlist");
- var $list = $("#module_list");
- $list.sortable({
- opacity: 0.6,
- revert: true,
- cursor: 'move',
- handle: '.m_title',
- update: function(){
- var new_order = [];
- $list.children(".modules").each(function() {
- new_order.push(this.title);
- });
- var newid = new_order.join(',');
- var oldid = $orderlist.val();
- $.ajax({
- type: "post",
- url: "update.php",
- data: { id: newid, order: oldid }, //id:新的排列对应的ID,order:原排列顺序
- beforeSend: function() {
- $show.html("<img src='load.gif' /> 正在更新");
- },
- success: function(msg) {
- //alert(msg);
- $show.html("");
- }
- });
- }
- });
- });
原文地址:http://www.freejs.net/article_jquerywenzi_441.html