登录  /  注册
首页 > web前端 > js教程 > 正文

easyUI的拖动操作中droppable,draggable用法实例

小云云
发布: 2018-01-10 10:42:15
原创
1564人浏览过

本文主要介绍了有关easyui的拖动操作中droppable,draggable用法例子,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。

这个demo展示的效果为:从上面可以拖动到下面相应的框中(原有的不能再拖动),拖动框中的东西到外面可以取消

-----------以下为HTML


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <link rel="stylesheet" type="text/css" href="css/bootstrap-3.3.5/css/bootstrap.min.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="css/easyui.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="css/icon.css" rel="external nofollow" >
  <link rel="stylesheet" href="css/demo.css" rel="external nofollow" >
</head>
<body>
  <p class="container">
   <ul class="items">
     <li class="category">
      <i></i>
      <span>品类</span>
     </li>
     <li class="factory">
      <i></i>
      <span>工厂</span>
     </li>
     <li class="with-high">
      <i></i>
      <span>跟高</span>
     </li>
     <li class="with-type">
      <i></i>
      <span>跟型</span>
     </li>
     <li class="price">
      <i></i>
      <span>单价</span>
     </li>
   </ul>
   <p class="target">
     <p class="target-cascade">
      <span>级联统计指标</span>
      <ul>
      </ul>
     </p>
     <p class="target-column">
      <span>列指标</span>
      <ul>
      </ul>
     </p>
   </p>
  </p>
  <script src="js/jquery.js"></script>
  <script src="js/jquery.easyui.min.js"></script>
  <script src="js/demo.js"></script>
</body>
</html>
登录后复制

-------------以下为js代码


var tab = [];
$(&#39;.items li&#39;).draggable({
  proxy: &#39;clone&#39;,
  revert: true
});
// 级联统计指标放置区
$(&#39;.target-cascade&#39;).droppable({
  onDragEnter: function(e,source){
   $(this).css(&#39;border&#39;,&#39;1px solid red&#39;);
  },
  onDragLeave: function(e,source){
   $(this).css(&#39;border&#39;,&#39;1px solid black&#39;);
  },
  onDrop: function(e,source){
   // 判断拖动的元素是否存在于放置区内
   if($(source).draggable(&#39;options&#39;).proxy === &#39;clone&#39;){
     // 禁用拖动
     NotDrag(source);
     // 将拖入元素的原位置记录下来
     var buttonName = $(source).find(&#39;span&#39;).html();
     console.log("--------"+$(source).index());
     tab[buttonName] = $(source).index();//返回指定元素相对于其他元素的位置(0,1等),如果没有找到,则返回-1
     var Ele = $(&#39;<li class=&#39;+ $(source)[0].className +&#39;><button>&#39;+ buttonName +&#39;</button></li>&#39;);
     Ele.appendTo(&#39;.target-cascade ul&#39;);
   }
   $(this).css(&#39;border&#39;,&#39;1px solid black&#39;);

   // 拖动放置区内的元素
   $(this).find(&#39;button&#39;).draggable({
     revert: true,
     onStopDrag: function(e){
      var _index = tab[$(this).html()];
      $(this).parent().remove();
      $(&#39;.items li:eq(&#39;+_index+&#39;)&#39;).draggable(&#39;enable&#39;);
      $(&#39;.items li:eq(&#39;+_index+&#39;)&#39;).find(&#39;i&#39;).css(&#39;backgroundColor&#39;,&#39;red&#39;);
     }
   });
  }
});
// 列指标放置区
$(&#39;.target-column&#39;).droppable({
  onDragEnter: function(e,source){
   $(this).css(&#39;border&#39;,&#39;1px solid red&#39;); 
  },
  onDragLeave: function(e,source){
   $(this).css(&#39;border&#39;,&#39;1px solid black&#39;);
  },
  onDrop: function(e,source){
   // 判断拖动的元素是否存在于放置区内
   if($(source).draggable(&#39;options&#39;).proxy === &#39;clone&#39;){
     // 禁用拖动
     NotDrag(source);
     var buttonName = $(source).find(&#39;span&#39;).html();
     tab[buttonName] = $(source).index();
     var Ele = $(&#39;<li class=&#39;+ $(source)[0].className +&#39;><button>&#39;+ buttonName +&#39;</button><select><option value ="show">显示</option>&#39;+
      &#39;<option value ="sum">求和</option><option value ="count">计数</option></select></li>&#39;);
     Ele.appendTo(&#39;.target-column ul&#39;);
   }
   $(this).css(&#39;border&#39;,&#39;1px solid black&#39;);
   // 拖动放置区内的元素
   $(this).find(&#39;button&#39;).draggable({
     revert: true,
     onDrag: function(e){
      $(e.data.parent).find(&#39;select&#39;).hide();
     },
     onStopDrag: function(e){
      var _index = tab[$(this).html()];
      $(this).parent().remove();
      $(&#39;.items li:eq(&#39;+_index+&#39;)&#39;).draggable(&#39;enable&#39;);
      $(&#39;.items li:eq(&#39;+_index+&#39;)&#39;).find(&#39;i&#39;).css(&#39;backgroundColor&#39;,&#39;red&#39;);

      $(e.target).siblings(&#39;select&#39;).show();
     }
   });
  }
});
//禁止拖动
function NotDrag(source){
  $(source).draggable(&#39;disable&#39;);//禁用拖动动作
  $(source).find(&#39;i&#39;).css(&#39;backgroundColor&#39;,&#39;grey&#39;);
}
登录后复制

相关推荐:

jQuery EasyUI 教程-Droppable(放置)

droppable讲解

如何使用jQuery Draggable和Droppable实现拖拽功能_jquery

以上就是easyUI的拖动操作中droppable,draggable用法实例的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号