ElementUI實現表格(table) 行上下移動的效果


參考地址
https://blog.csdn.net/sunshine0508/article/details/88390155
看大佬的地址

<div id="app">
		<el-table :data="URLModles" :show-header="false" highlight-current-row style="width: 100%"
			@selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55px">
			</el-table-column>
			<el-table-column type="index" width="55px">
			</el-table-column>
			<el-table-column prop="expressCode" label="快遞代碼" width="100px">
			</el-table-column>
			<el-table-column prop="expressName" label="快遞名稱" width="100px">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button size="mini" :disabled="scope.$index===0" @click="moveUp(scope.$index,scope.row)"><i
							class="el-icon-arrow-up"></i></el-button>
					<el-button size="mini" :disabled="scope.$index===(URLModles.length-1)"
						@click="moveDown(scope.$index,scope.row)"><i class="el-icon-arrow-down"></i></el-button>
					<el-button type="info" size="mini" round v-if="scope.$index===0">默認</el-button>
				</template>

			</el-table-column>
		</el-table>
	</div>
	var vm = new Vue({
		el: "#app",

		data() {
			return {
				URLModles: [{
					index: '1',
					expressCode: 'SF',
					expressName: '順豐快遞',
					status: true,
				}, {
					index: '2',
					expressCode: 'YTO',
					expressName: '圓通快遞',
					status: true,
				}, {
					index: '3',
					expressCode: 'UC',
					expressName: '優速快遞',
					status: true,
				}],
				multipleSelection: []
			}
		},

		methods: {
			//選擇復選框數據
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},

			//上移
			moveUp(index, row) {
				var that = this;
				console.log('上移', index, row);
				console.log(that.URLModles[index]);
				if (index > 0) {
					let upDate = that.URLModles[index - 1];
					that.URLModles.splice(index - 1, 1);
					that.URLModles.splice(index, 0, upDate);
				} else {
					alert('已經是第一條,不可上移');
				}
			},

			//下移
			moveDown(index, row) {
				var that = this;
				console.log('下移', index, row);
				if ((index + 1) === that.URLModles.length) {
					alert('已經是最后一條,不可下移');
				} else {
					console.log(index);
					// 保存下一條數據
					let downDate = that.URLModles[index + 1];
					// 刪除下一條數據
					that.URLModles.splice(index + 1, 1);
					// 增添被刪除的那一條數據
					that.URLModles.splice(index, 0, downDate);
				}
			}
		}

	})
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM