Skip to content
本页导读

Drag

组件类型:UxDragComponentPublicInstance

支持列表式、宫格式拖动排序,支持设置列数,列表式支持边界滚动,支持动态新增删除项目

平台兼容性

UniApp X

AndroidiOSweb鸿蒙 Next小程序
x

UniApp Vue Nvue

AndroidiOSweb鸿蒙 Next小程序
xxxx

Props

属性名类型默认值说明
nameStringid唯一索引名称
listArray数据列表
colNumber1列数
heightAny45单项高度
borderBooleanfalse显示下边框
longtouchBooleantrue是否开启长按拖动
touchtimeNumber250长按拖动触发时间开启长按拖动才有效
speedNumber1列表边界滚动速度
disabledBooleanfalse是否禁用

Events

事件名说明参数
click点击项目时触发
change拖拽完成时触发
start拖拽开始时触发
end拖拽结束时触发

示例代码

html
<template>
	<ux-page :stack="showDoc">
		<ux-navbar :title="title" :bold="true">
			<template v-slot:right>
				<!-- #ifndef MP -->
				<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
				<!-- #endif -->
			</template>
		</ux-navbar>
		
		<ux-scroll :show-scrollbar="false" :disabled="disabled">
			<ux-card direction="column" icon="flag-filled" title="拖拽排序" :bold="true">
				<ux-text text="支持列表式、宫格式拖动排序,支持设置列数,列表式支持边界滚动,支持动态新增删除项目"></ux-text>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="列表式" :bold="true">
				<template #subtitle>
					<ux-row>
						<ux-button theme="primary" :ml="10" text="+1" @click="add()"></ux-button>
						<ux-button theme="primary" :ml="10" text="-1" @click="del()"></ux-button>
						<ux-button theme="primary" :ml="10" text="重置" @click="reset()"></ux-button>
					</ux-row>
				</template>
				<ux-row align="center" :mb="15">
					<ux-text text="支持列表式拖动排序,支持边界自动滚动"></ux-text>
				</ux-row>
				
				<view style="max-height: 250px;">
					<ux-drag ref="uxDragRef" :list="list" :col="1" :border="true" @start="disabled = true" @end="disabled = false" @click="click" @change="change">
						<!-- #ifdef MP -->
						<template v-for="(item, index) in list" :key="index">
							<template v-slot:[`${item['id']}`]>
								<view class="row">
									<view class="content">
										<image class="icon" :src="item['url']"></image>
										<text class="text">{{item['id']}}</text>
									</view>
									<view class="drag">
										<ux-icon type="drag"></ux-icon>
									</view>
								</view>
							</template>
						</template>
						<template v-slot:drag="{data}">
							<view class="row">
								<view class="content">
									<image class="icon" :src="data['url']"></image>
									<text class="text">{{data['id']}}</text>
								</view>
								<view class="drag">
									<ux-icon type="drag"></ux-icon>
								</view>
							</view>
						</template>
						<!-- #endif -->
						<!-- #ifndef MP -->
						<template v-slot="{data}">
							<view class="row">
								<view class="content">
									<image class="icon" :src="data['url']"></image>
									<text class="text">{{data['id']}}</text>
								</view>
								<view class="drag">
									<ux-icon type="drag"></ux-icon>
								</view>
							</view>
						</template>
						<!-- #endif -->
					</ux-drag>
				</view>
			</ux-card>
			
			<!-- #ifdef APP-ANDROID -->
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="soapbubble-filled" text="系统问题 待修复"></ux-text>
				</ux-row>
			</ux-placeholder>
			<!-- #endif -->
			
			<!-- #ifndef APP-ANDROID -->
			<ux-card direction="column" icon="arrowright" title="宫格式" :bold="true">
				<template #subtitle>
					<ux-row>
						<ux-button theme="primary" :ml="10" text="+1" @click="add()"></ux-button>
						<ux-button theme="primary" :ml="10" text="-1" @click="del()"></ux-button>
						<ux-button theme="primary" :ml="10" text="重置" @click="reset()"></ux-button>
					</ux-row>
				</template>
				<ux-row align="center" :mb="15">
					<ux-text text="支持宫格式拖动排序,支持长按拖拽"></ux-text>
				</ux-row>
				<ux-drag ref="uxDragGridRef" name="cid" :list="list" :col="3" @start="disabled = true" @end="disabled = false" @click="click" @change="change">
					<!-- #ifdef MP -->
					<template v-for="(item, index) in list" :key="index">
						<template v-slot:[`${item['cid']}`]>
							<view class="grid">
								<ux-text :bold="true" :size="15" color="black" :text="item['cid']"></ux-text>
							</view>
						</template>
					</template>
					<template v-slot:drag="{data}">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="data['cid']"></ux-text>
						</view>
					</template>
					<!-- #endif -->
					<!-- #ifndef MP -->
					<template v-slot="{data}">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="data['cid']"></ux-text>
						</view>
					</template>
					<!-- #endif -->
				</ux-drag>
			</ux-card>
			
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
				</ux-row>
			</ux-placeholder>
			<!-- #endif -->
			
		</ux-scroll>
	</ux-page>
</template>
<template>
	<ux-page :stack="showDoc">
		<ux-navbar :title="title" :bold="true">
			<template v-slot:right>
				<!-- #ifndef MP -->
				<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
				<!-- #endif -->
			</template>
		</ux-navbar>
		
		<ux-scroll :show-scrollbar="false" :disabled="disabled">
			<ux-card direction="column" icon="flag-filled" title="拖拽排序" :bold="true">
				<ux-text text="支持列表式、宫格式拖动排序,支持设置列数,列表式支持边界滚动,支持动态新增删除项目"></ux-text>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="列表式" :bold="true">
				<template #subtitle>
					<ux-row>
						<ux-button theme="primary" :ml="10" text="+1" @click="add()"></ux-button>
						<ux-button theme="primary" :ml="10" text="-1" @click="del()"></ux-button>
						<ux-button theme="primary" :ml="10" text="重置" @click="reset()"></ux-button>
					</ux-row>
				</template>
				<ux-row align="center" :mb="15">
					<ux-text text="支持列表式拖动排序,支持边界自动滚动"></ux-text>
				</ux-row>
				
				<view style="max-height: 250px;">
					<ux-drag ref="uxDragRef" :list="list" :col="1" :border="true" @start="disabled = true" @end="disabled = false" @click="click" @change="change">
						<!-- #ifdef MP -->
						<template v-for="(item, index) in list" :key="index">
							<template v-slot:[`${item['id']}`]>
								<view class="row">
									<view class="content">
										<image class="icon" :src="item['url']"></image>
										<text class="text">{{item['id']}}</text>
									</view>
									<view class="drag">
										<ux-icon type="drag"></ux-icon>
									</view>
								</view>
							</template>
						</template>
						<template v-slot:drag="{data}">
							<view class="row">
								<view class="content">
									<image class="icon" :src="data['url']"></image>
									<text class="text">{{data['id']}}</text>
								</view>
								<view class="drag">
									<ux-icon type="drag"></ux-icon>
								</view>
							</view>
						</template>
						<!-- #endif -->
						<!-- #ifndef MP -->
						<template v-slot="{data}">
							<view class="row">
								<view class="content">
									<image class="icon" :src="data['url']"></image>
									<text class="text">{{data['id']}}</text>
								</view>
								<view class="drag">
									<ux-icon type="drag"></ux-icon>
								</view>
							</view>
						</template>
						<!-- #endif -->
					</ux-drag>
				</view>
			</ux-card>
			
			<!-- #ifdef APP-ANDROID -->
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="soapbubble-filled" text="系统问题 待修复"></ux-text>
				</ux-row>
			</ux-placeholder>
			<!-- #endif -->
			
			<!-- #ifndef APP-ANDROID -->
			<ux-card direction="column" icon="arrowright" title="宫格式" :bold="true">
				<template #subtitle>
					<ux-row>
						<ux-button theme="primary" :ml="10" text="+1" @click="add()"></ux-button>
						<ux-button theme="primary" :ml="10" text="-1" @click="del()"></ux-button>
						<ux-button theme="primary" :ml="10" text="重置" @click="reset()"></ux-button>
					</ux-row>
				</template>
				<ux-row align="center" :mb="15">
					<ux-text text="支持宫格式拖动排序,支持长按拖拽"></ux-text>
				</ux-row>
				<ux-drag ref="uxDragGridRef" name="cid" :list="list" :col="3" @start="disabled = true" @end="disabled = false" @click="click" @change="change">
					<!-- #ifdef MP -->
					<template v-for="(item, index) in list" :key="index">
						<template v-slot:[`${item['cid']}`]>
							<view class="grid">
								<ux-text :bold="true" :size="15" color="black" :text="item['cid']"></ux-text>
							</view>
						</template>
					</template>
					<template v-slot:drag="{data}">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="data['cid']"></ux-text>
						</view>
					</template>
					<!-- #endif -->
					<!-- #ifndef MP -->
					<template v-slot="{data}">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="data['cid']"></ux-text>
						</view>
					</template>
					<!-- #endif -->
				</ux-drag>
			</ux-card>
			
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
				</ux-row>
			</ux-placeholder>
			<!-- #endif -->
			
		</ux-scroll>
	</ux-page>
</template>
ts
<script setup>
	
	import * as plus from '@/uni_modules/ux-plus'
	
	const title = ref('')
	
	let id = 0
	const disabled = ref(false)
	const list = ref<UTSJSONObject[]>([])
	const uxDragRef = ref(null)
	const uxDragGridRef = ref(null)
	
	function reset() {
		list.value = [
			{id: '0', cid: '0', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '1', cid: '1', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '2', cid: '2', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '3', cid: '3', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '4', cid: '4', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '5', cid: '5', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '6', cid: '6', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '7', cid: '7', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '8', cid: '8', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		]
		
		// 模拟一个唯一id
		id = list.value.length
	}
	
	function add() {
		list.value.push({
			id: `${id}`,
			cid: `${id}`,
			url: 'https://via.placeholder.com/200x200.png/f0f0f0'
		})
		
		// 模拟一个唯一id
		id++
	}
	
	function del() {
		list.value.splice(0, 1)
		// list.value.splice(list.value.length - 1, 1)
	}
	
	function change(e : UTSJSONObject[]) {
		list.value = e
	}
	
	function click(item: UTSJSONObject) {
		uni.showToast({
			title: `点击了${item['id']}`,
			duration: 300,
			icon: 'none'
		})
	}
	
	onLoad((e) => {
		title.value = e['title'] ?? ''
		
		reset()
	})
	
	const showDoc = ref(false)
	
	function onDoc() {
		plus.openWeb({
			title: '在线文档',
			url: 'https://www.uxframe.cn/component/drag.html',
			// blur: 1,
			success: () => {
				showDoc.value = true
			},
			complete: () => {
				showDoc.value = false
			}
		})
	}
</script>
<script setup>
	
	import * as plus from '@/uni_modules/ux-plus'
	
	const title = ref('')
	
	let id = 0
	const disabled = ref(false)
	const list = ref<UTSJSONObject[]>([])
	const uxDragRef = ref(null)
	const uxDragGridRef = ref(null)
	
	function reset() {
		list.value = [
			{id: '0', cid: '0', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '1', cid: '1', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '2', cid: '2', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '3', cid: '3', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '4', cid: '4', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '5', cid: '5', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '6', cid: '6', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '7', cid: '7', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
			{id: '8', cid: '8', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		]
		
		// 模拟一个唯一id
		id = list.value.length
	}
	
	function add() {
		list.value.push({
			id: `${id}`,
			cid: `${id}`,
			url: 'https://via.placeholder.com/200x200.png/f0f0f0'
		})
		
		// 模拟一个唯一id
		id++
	}
	
	function del() {
		list.value.splice(0, 1)
		// list.value.splice(list.value.length - 1, 1)
	}
	
	function change(e : UTSJSONObject[]) {
		list.value = e
	}
	
	function click(item: UTSJSONObject) {
		uni.showToast({
			title: `点击了${item['id']}`,
			duration: 300,
			icon: 'none'
		})
	}
	
	onLoad((e) => {
		title.value = e['title'] ?? ''
		
		reset()
	})
	
	const showDoc = ref(false)
	
	function onDoc() {
		plus.openWeb({
			title: '在线文档',
			url: 'https://www.uxframe.cn/component/drag.html',
			// blur: 1,
			success: () => {
				showDoc.value = true
			},
			complete: () => {
				showDoc.value = false
			}
		})
	}
</script>
css
<style lang="scss">
	
	.grid {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f0f0f0;
		border: 1px solid white;
		box-sizing: border-box;
	}
	
	.row {
		flex: 1;
		height: 44px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		box-sizing: border-box;
		
		.content {
			flex: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-left: 5px;
			padding-right: 5px;
		
			.icon {
				width: 30px;
				height: 30px;
				border-radius: 4px;
			}
			
			.text {
				font-size: 14px;
				margin-left: 5px;
			}
		}
		
		.drag {
			width: 44px;
			height: 44px;
			display: flex;
			align-items: center;
			justify-content: center;
		
			.drag-text {
				font-size: 14px;
				color: black;
			}
		}
	}
</style>
<style lang="scss">
	
	.grid {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f0f0f0;
		border: 1px solid white;
		box-sizing: border-box;
	}
	
	.row {
		flex: 1;
		height: 44px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		box-sizing: border-box;
		
		.content {
			flex: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-left: 5px;
			padding-right: 5px;
		
			.icon {
				width: 30px;
				height: 30px;
				border-radius: 4px;
			}
			
			.text {
				font-size: 14px;
				margin-left: 5px;
			}
		}
		
		.drag {
			width: 44px;
			height: 44px;
			display: flex;
			align-items: center;
			justify-content: center;
		
			.drag-text {
				font-size: 14px;
				color: black;
			}
		}
	}
</style>