w-drag-sorts.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <scroll-view scroll-y :show-scrollbar="showScrollBar" class="w-drag-sorts-scroll" :style="{height: scrollBoxPxHeight+'px'}">
  3. <view class="w-drag-sorts" :style="{height: boxHeight+'px'}" :class="{'inited':inited}">
  4. <view class="w-drag-sorts-item" v-for="(vo,i) in newList" :key="i"
  5. :style="{'top': vo.__top + 'px', ...finallyItemStyles}"
  6. :class="{'draging':currentIndex == i}" @longpress="onRemove(i)" @click="navTo(i)">
  7. <view class="align-center">
  8. <view class="ctrl"
  9. v-if="showDragCtrl"
  10. @touchmove.stop.prevent
  11. @touchstart="onTouchstart($event, i)"
  12. @touchmove="onTouchmove"
  13. @touchend="onTouchend"
  14. >
  15. <!-- <image class="icon" :src="ctrlIcon" /> -->
  16. <image class="w32 h32" src="@/static/images/user/drag_icon.png"></image>
  17. </view>
  18. <view class="text">
  19. {{vo.indicatorName}}
  20. </view>
  21. </view>
  22. <!-- <view class="content2"> -->
  23. <view v-if="isRemove==true" class="item-icon" @click.stop="onDisabledClick(i,vo)" >
  24. <!-- <image class="icon" :src="vo.icon"/> -->
  25. <image class="w40 h40" src="@/static/images/user/remove_icon.png"></image>
  26. </view>
  27. <view v-if="isAdd==true" class="item-icon" @click.stop="onAddClick(i,vo)">
  28. <!-- <image class="icon" :src="vo.icon"/> -->
  29. <image class="w40 h40" src="@/static/images/user/remove_add_icon.png"></image>
  30. </view>
  31. <!-- </view> -->
  32. </view>
  33. </view>
  34. </scroll-view>
  35. </template>
  36. <script>
  37. export default {
  38. name: "wDragSorts",
  39. props: {
  40. //列表数据
  41. listData: {
  42. type: Array,
  43. default: () => []
  44. },
  45. //列表项高度
  46. itemHeight: {
  47. type: Number,
  48. default: 80
  49. },
  50. //列表内边距,
  51. itemStyles: {
  52. type: Object,
  53. default: () => {
  54. return {};
  55. }
  56. },
  57. //控制图标
  58. ctrlIcon:{
  59. type:String,
  60. default:'/static/images/user/drag_icon.png'
  61. },
  62. //是否显示拖动控制器
  63. showDragCtrl:{
  64. type:Boolean,
  65. default: true,
  66. },
  67. //滚动去高度,如果单位rpx,如果不设置,内容容器高度一致
  68. scrollBoxHeight:{
  69. type: Number,
  70. default:-1
  71. },
  72. //是否显示删除图标
  73. isRemove:{
  74. type:Boolean,
  75. default: true,
  76. },
  77. //是否显示新增图标
  78. isAdd:{
  79. type:Boolean,
  80. default: true,
  81. }
  82. },
  83. data() {
  84. return {
  85. newList: [],
  86. //当前列表项的下标
  87. currentIndex: -1,
  88. //记录拖动位置
  89. moveY: 0,
  90. //拖动状态
  91. draging: false,
  92. //初始化完成
  93. inited:false,
  94. }
  95. },
  96. computed:{
  97. itemPxHeight(){
  98. return uni.rpx2px(this.itemHeight);
  99. },
  100. boxHeight(){
  101. return this.itemPxHeight * this.listData.length;
  102. },
  103. finallyItemStyles(){
  104. return { ...(this.itemStyles || {}), height: this.itemHeight+'rpx'}
  105. },
  106. scrollBoxPxHeight(){
  107. if( this.scrollBoxHeight < 0 ){
  108. return this.boxHeight
  109. }else{
  110. let _height = uni.rpx2px(this.scrollBoxHeight);
  111. if( _height > this.boxHeight ){
  112. _height = this.boxHeight
  113. }
  114. return _height;
  115. }
  116. },
  117. showScrollBar(){
  118. return (this.boxHeight - this.scrollBoxPxHeight) > 10;
  119. }
  120. },
  121. watch:{
  122. showScrollBar:{
  123. handler(v){
  124. console.log(v)
  125. },
  126. immediate:true
  127. },
  128. listData:{
  129. handler(v){
  130. this.listData=v
  131. this.init()
  132. },
  133. immediate:true
  134. }
  135. },
  136. created() {
  137. },
  138. mounted() {
  139. },
  140. methods: {
  141. init() {
  142. this.inited = false;
  143. this.newList = this.listData.map(vo => {
  144. return {
  145. ...vo,
  146. __top: 0,
  147. __height: 0,
  148. __otop: 0
  149. }
  150. });
  151. // console.log(this.listData,'888')
  152. // #ifdef MP-WEIXIN
  153. const selector = uni.createSelectorQuery().in(this);
  154. // #endif
  155. // #ifndef MP-WEIXIN
  156. const selector = uni.createSelectorQuery();
  157. // #endif
  158. this.$nextTick(() => {
  159. selector.selectAll('.w-drag-sorts-item').fields({
  160. rect: true,
  161. size: true,
  162. }, nodeItem => {
  163. nodeItem.forEach((item, index) => {
  164. const top = item.height * index;
  165. this.$set(this.newList, index, {
  166. ...this.newList[index],
  167. __height: item.height,
  168. __top: top,
  169. __otop: top
  170. });
  171. });
  172. this.inited = true;
  173. }).exec();
  174. });
  175. },
  176. onTouchstart(e, i) {
  177. const pageY = e.touches[0]?.pageY || 0;
  178. // 记录当前拖动元素的下标
  179. this.currentIndex = i;
  180. // 记录拖动前的位置
  181. this.moveY = pageY
  182. },
  183. onTouchmove(e) {
  184. const pageY = e.touches[0]?.pageY || 0;
  185. const index = this.currentIndex;
  186. //列表项替换的阈值,如果移动的位置大于上下项的一半就执行位置占位操作
  187. const currentItem = this.newList[index];
  188. const changeVar = currentItem?.__height / 2;
  189. //判断上下移动的边界
  190. let newTop = this.newList[index].__top + (pageY - this.moveY);
  191. const max = currentItem?.__height * (this.newList.length - 1);
  192. if (newTop < 0) {
  193. newTop = 0;
  194. }
  195. if (newTop > max) {
  196. newTop = max;
  197. }
  198. // 设置被拖动项最新的位置
  199. this.newList[index].__top = newTop;
  200. // 记录位置
  201. this.moveY = pageY;
  202. // 向下拖动
  203. if (currentItem.__top >= this.newList[index + 1]?.__top - changeVar) {
  204. this.moveChange(1);
  205. }
  206. // 向上拖动
  207. if (currentItem.__top <= this.newList[index - 1]?.__top + changeVar) {
  208. this.moveChange(-1);
  209. }
  210. },
  211. moveChange(addValue) {
  212. const index = this.currentIndex;
  213. if (this.draging) {
  214. return
  215. }
  216. this.draging = true
  217. let currentItem = this.newList[index];
  218. const newIndex = index + addValue;
  219. //取出被替换项的位置,等交换完位置之后,给当前列表项
  220. const changeItemOTop = this.newList[newIndex].__otop;
  221. //交换位置
  222. this.newList[index] = this.newList[newIndex];
  223. this.newList[newIndex] = currentItem;
  224. //把当前项的位置给被替换的列表项
  225. this.newList[index].__top = currentItem.__otop;
  226. this.newList[index].__otop = currentItem.__otop;
  227. //由于当前列表项的top一直在改变,所以这里只需要把被替换列表项的原有位置给当前列表项
  228. //等到停止拖动再把这个被替换项的位置赋给当前项的top
  229. this.newList[newIndex].__otop = changeItemOTop;
  230. this.currentIndex = newIndex;
  231. this.draging = false;
  232. },
  233. onTouchend(e) {
  234. const index = this.currentIndex;
  235. this.newList[index].__top = this.newList[index].__otop;
  236. this.currentIndex = -1;
  237. const returnData = this.getReturnData();
  238. this.$emit('draged', [...returnData]);
  239. },
  240. //返回移除操作附加属性的列表数据
  241. getReturnData(){
  242. const tmp = JSON.parse(JSON.stringify(this.newList));
  243. tmp.map(vo => {
  244. for (let key in vo) {
  245. if (key.indexOf('__') == 0) {
  246. delete vo[key];
  247. }
  248. }
  249. })
  250. return tmp;
  251. },
  252. navTo(i){
  253. const item = this.getReturnData()[i]
  254. this.$emit("navClick",i, item);
  255. },
  256. //长按删除
  257. onRemove( i ){
  258. console.log(i,'1')
  259. const item = this.getReturnData()[i]
  260. this.$emit("itemRemoveClick",i, item);
  261. },
  262. //禁用
  263. onDisabledClick( i ){
  264. const item = this.getReturnData()[i]
  265. this.$emit("itemDisabledClick",i, item);
  266. },
  267. //启用
  268. onAddClick( i ){
  269. const item = this.getReturnData()[i]
  270. this.$emit("itemAddClick",i, item);
  271. },
  272. //列表项图标点击事件点击
  273. onItemIconClick( i ){
  274. const item = this.getReturnData()[i]
  275. this.$emit("itemIconClick",i, item);
  276. },
  277. }
  278. }
  279. </script>
  280. <style scoped lang="scss">
  281. @mixin iconSize {
  282. width: 48rpx;
  283. height: 48rpx;
  284. }
  285. .w-drag-sorts {
  286. width: 100%;
  287. position: relative;
  288. z-index: 1;
  289. height: auto;
  290. .icon {
  291. @include iconSize();
  292. }
  293. &.inited>&-item{
  294. position: absolute;
  295. }
  296. &-item {
  297. width: 100%;
  298. display: flex;
  299. justify-content: space-between;
  300. align-items: center;
  301. box-sizing: border-box;
  302. background-color: #fff;
  303. padding: 0 30rpx;
  304. border-bottom: 0 !important;
  305. &.draging {
  306. box-shadow: 0 0px 30rpx #ddd;
  307. z-index: 1;
  308. }
  309. .text{
  310. margin-left: 20rpx;
  311. font-family: PingFang SC, PingFang SC;
  312. font-weight: 400;
  313. font-size: 32rpx;
  314. color: #222426;
  315. text-align: left;
  316. }
  317. .item-icon{
  318. @include iconSize();
  319. }
  320. // .content2 {
  321. // flex:1;
  322. // height: 100%;
  323. // display: flex;
  324. // align-items: center;
  325. // }
  326. .ctrl {
  327. width: 32rpx;
  328. height: 32rpx;
  329. }
  330. }
  331. }
  332. </style>