Skip to content
本页导读

Drag 拖拽排序

组件类型:UxDragComponentPublicInstance、UxDragItemComponentPublicInstance

支持列表式、宫格式拖动排序。仅支持 ux-drag-item 作为子项

平台兼容性

AndroidiOSweb鸿蒙 Next小程序
xx

UxDragComponentPublicInstance

属性名类型默认值说明
nameStringid唯一索引名称 (默认 id)
listUTSJSONObject[]-数据列表
colNumber1列数 >2则宫格排序
heightNumber45单项高度

UxDragItemComponentPublicInstance

属性名类型默认值说明
idString-唯一索引
orderNumber-排序下标

Events

事件名称说明回调参数
change拖拽完成时触发Function(event: UTSJSONObject[])
start拖拽开始时触发Function()
end拖拽结束时触发Function()

示例代码

html
<template>
	<ux-page>
		<ux-navbar :title="title" :border="false"></ux-navbar>
		
		<ux-scroll :disabled="disabled">
			<ux-card direction="column" icon="flag" title="拖拽排序" :bold="true">
				<ux-text text="支持列表式、宫格式拖动排序"></ux-text>
				<ux-row :flex="true" align="top" :mt="5" style="width: 100%;">
					<ux-text text="详细文档:"></ux-text>
					<ux-text style="flex: 1" name="拖拽排序" text="https://www.uxframe.cn/component/func/drag.html" mode="link"></ux-text>
				</ux-row>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="列表式" :bold="true">
				<ux-text text="支持单列拖动排序" :mb="15"></ux-text>
				<ux-drag name="id" :list="list1" @start="disabled = true" @end="disabled = false" @change="change1">
					<ux-drag-item :id="item['id']" :order="index" v-for="(item,index) in list1" :key="index">
						<ux-cell style="width: 100%;flex:1;">
							<template v-slot:left>
								<ux-image :width="30" :height="30" :radius="5" :src="item['url']"></ux-image>
								<ux-text :ml="10" :bold="true" :size="15" :text="item['id']"></ux-text>
							</template>
							<template v-slot:right>
								<ux-icon type="more"></ux-icon>
							</template>
						</ux-cell>
					</ux-drag-item>
				</ux-drag>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="宫格式" :bold="true">
				<ux-text text="支持宫格式拖动排序" :mb="15"></ux-text>
				<ux-drag name="sort" :list="list2" :col="3" @start="disabled = true" @end="disabled = false" @change="change2">
					<ux-drag-item :id="item['sort']" :order="index" v-for="(item,index) in list2" :key="index">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="item['sort']"></ux-text>
						</view>
					</ux-drag-item>
				</ux-drag>
			</ux-card>
			
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="wink" text="真的没有了~"></ux-text>
				</ux-row>
			</ux-placeholder>
		</ux-scroll>
	</ux-page>
</template>
<template>
	<ux-page>
		<ux-navbar :title="title" :border="false"></ux-navbar>
		
		<ux-scroll :disabled="disabled">
			<ux-card direction="column" icon="flag" title="拖拽排序" :bold="true">
				<ux-text text="支持列表式、宫格式拖动排序"></ux-text>
				<ux-row :flex="true" align="top" :mt="5" style="width: 100%;">
					<ux-text text="详细文档:"></ux-text>
					<ux-text style="flex: 1" name="拖拽排序" text="https://www.uxframe.cn/component/func/drag.html" mode="link"></ux-text>
				</ux-row>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="列表式" :bold="true">
				<ux-text text="支持单列拖动排序" :mb="15"></ux-text>
				<ux-drag name="id" :list="list1" @start="disabled = true" @end="disabled = false" @change="change1">
					<ux-drag-item :id="item['id']" :order="index" v-for="(item,index) in list1" :key="index">
						<ux-cell style="width: 100%;flex:1;">
							<template v-slot:left>
								<ux-image :width="30" :height="30" :radius="5" :src="item['url']"></ux-image>
								<ux-text :ml="10" :bold="true" :size="15" :text="item['id']"></ux-text>
							</template>
							<template v-slot:right>
								<ux-icon type="more"></ux-icon>
							</template>
						</ux-cell>
					</ux-drag-item>
				</ux-drag>
			</ux-card>
			
			<ux-card direction="column" icon="arrowright" title="宫格式" :bold="true">
				<ux-text text="支持宫格式拖动排序" :mb="15"></ux-text>
				<ux-drag name="sort" :list="list2" :col="3" @start="disabled = true" @end="disabled = false" @change="change2">
					<ux-drag-item :id="item['sort']" :order="index" v-for="(item,index) in list2" :key="index">
						<view class="grid">
							<ux-text :bold="true" :size="15" color="black" :text="item['sort']"></ux-text>
						</view>
					</ux-drag-item>
				</ux-drag>
			</ux-card>
			
			<ux-placeholder :height="200">
				<ux-row justify="center" align="center" style="height: 100%;">
					<ux-text prefix-icon="wink" text="真的没有了~"></ux-text>
				</ux-row>
			</ux-placeholder>
		</ux-scroll>
	</ux-page>
</template>
ts
<script setup>
	let title = ''
	
	const disabled = ref(false)
	
	const list1 = ref<UTSJSONObject[]>([
		{id: '0', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '1', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '2', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '3', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '4', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
	] as UTSJSONObject[])
	
	const list2 = ref<UTSJSONObject[]>([
		{sort: '0'},
		{sort: '1'},
		{sort: '2'},
		{sort: '3'},
		{sort: '4'},
		{sort: '5'},
		{sort: '6'},
		{sort: '7'},
		{sort: '8'},
	] as UTSJSONObject[])
	
	function change1(e : UTSJSONObject[]) {
		console.log(e);
	}
	
	function change2(e : UTSJSONObject[]) {
		console.log(e);
	}
	
	onLoad((e: OnLoadOptions) => {
		title = e['title'] ?? ''
	})
</script>
<script setup>
	let title = ''
	
	const disabled = ref(false)
	
	const list1 = ref<UTSJSONObject[]>([
		{id: '0', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '1', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '2', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '3', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
		{id: '4', url: 'https://via.placeholder.com/200x200.png/f0f0f0' },
	] as UTSJSONObject[])
	
	const list2 = ref<UTSJSONObject[]>([
		{sort: '0'},
		{sort: '1'},
		{sort: '2'},
		{sort: '3'},
		{sort: '4'},
		{sort: '5'},
		{sort: '6'},
		{sort: '7'},
		{sort: '8'},
	] as UTSJSONObject[])
	
	function change1(e : UTSJSONObject[]) {
		console.log(e);
	}
	
	function change2(e : UTSJSONObject[]) {
		console.log(e);
	}
	
	onLoad((e: OnLoadOptions) => {
		title = e['title'] ?? ''
	})
</script>
css
<style lang="scss">
	.title {
		width: 100%;
		padding: 10px;
		font-size: 28rpx;
	}
	
	.grid {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f0f0f0;
		margin: 2px;
	}
	
</style>
<style lang="scss">
	.title {
		width: 100%;
		padding: 10px;
		font-size: 28rpx;
	}
	
	.grid {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #f0f0f0;
		margin: 2px;
	}
	
</style>