uniapp App表格拖动排序以及全屏切换显示

<template>
	<view class="box" :style="{marginLeft: isFullscreen? 0 : '44px'}">
		<view class="navBar" v-if="isFullscreen">
			表格
		</view>
		<view class="button" @click="fullscreen">
			点击全屏
		</view>
		<view class="uni-container" :data="tableData" :change:data="Sortable.queryMenuList">
			<uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据"
				@selection-change="selectionChange">
				<uni-tr>
					<uni-th width="150" align="center">日期</uni-th>
					<uni-th width="150" align="center">姓名</uni-th>
					<uni-th align="center">地址</uni-th>
					<uni-th width="204" align="center">设置</uni-th>
				</uni-tr>
				<tbody id="drag">
					<uni-tr v-for="(item, index) in tableData" :key="index" :data-id="JSON.stringify(item)">
						<uni-td sortable>{{ item.date }}</uni-td>
						<uni-td>
							<view class="name">{{ item.name }}</view>
						</uni-td>
						<uni-td align="center">{{ item.address }}</uni-td>
						<uni-td>
							<view class="uni-group">
								<button class="uni-button" size="mini" type="primary">修改</button>
								<button class="uni-button" size="mini" type="warn">删除</button>
							</view>
						</uni-td>
					</uni-tr>
				</tbody>
			</uni-table>
			<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
					:total="total" @change="change" /></view>
		</view>
	</view>
</template>

<script>
	import tableData from './table.js'
	export default {
		data() {
			return {
				searchVal: '',
				tableData: [],
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				isFullscreen: true
			}
		},
		onLoad() {
			this.selectedIndexs = []
			this.getData(1)
		},
		methods: {
			fullscreen() {
				// 获取当前屏幕内容是横向(90)还是纵向(0)
				const orientation = plus.navigator.getOrientation();
				// 切换横竖屏
				plus.screen.lockOrientation(orientation ? 'portrait-primary' : 'landscape-primary');
				// 切换屏幕是否全屏  plus.navigator.isFullscreen() 返回当前屏幕是否全屏  false 不是全屏
				plus.navigator.setFullscreen(!plus.navigator.isFullscreen());
				this.isFullscreen = !plus.navigator.isFullscreen();
				console.log(this.isFullscreen);
			},
			// 多选处理
			selectedItems() {
				return this.selectedIndexs.map(i => this.tableData[i])
			},
			// 多选
			selectionChange(e) {
				console.log(e.detail.index)
				this.selectedIndexs = e.detail.index
			},
			//批量删除
			delTable() {
				console.log(this.selectedItems())
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.selectedIndexs.length = 0
				this.getData(e.current)
			},
			// 搜索
			search() {
				this.getData(1, this.searchVal)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.loading = true
				this.pageCurrent = pageCurrent
				this.request({
					pageSize: this.pageSize,
					pageCurrent: pageCurrent,
					value: value,
					success: res => {
						// console.log('data', res);
						this.tableData = res.data
						this.total = res.total
						this.loading = false
					}
				})
			},
			// 伪request请求
			request(options) {
				const {
					pageSize,
					pageCurrent,
					success,
					value
				} = options
				let total = tableData.length
				let data = tableData.filter((item, index) => {
					const idx = index - (pageCurrent - 1) * pageSize
					return idx < pageSize && idx >= 0
				})
				if (value) {
					data = []
					tableData.forEach(item => {
						if (item.name.indexOf(value) !== -1) {
							data.push(item)
						}
					})
					total = data.length
				}

				setTimeout(() => {
					typeof success === 'function' &&
						success({
							data: data,
							total: total
						})
				}, 500)
			}
		}
	}
</script>

<script module="Sortable" lang="renderjs">
	import Sortable from 'sortablejs';
	export default {
		data() {
			return {
				list: [],
				userId: ''
			}
		},
		mounted(){
			this.init();
		},
		methods: {
			queryMenuList(newValue, oldValue) {
				this.list = JSON.parse(JSON.stringify(newValue));
				// this.list = newValue;
			},
			// 初始化拖动元素
			init() {
				let dom = document.getElementById('drag');
				var ops = {
					delay: 200,
					animation: 1000,
					//拖动结束
					onEnd: function(e) {
						//获取拖动后的排序
						let list = [];
						// 获取排序后的数据  处理
						let arr = sortable.toArray();
						arr.forEach(item => {
							list.push(JSON.parse(item))
						})
						console.log(list);
					}
				};
				//初始化
				let sortable = Sortable.create(dom, ops);
			}
		}
	}
</script>


<style lang="scss">
	.navBar {
		display: flex;
		justify-content: center;
		align-items: center;
		height: var(--status-bar-height);
	}

	.box {
		padding-top: var(--status-bar-height);
	}
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ღ张明宇࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值