vue+eltable 实现双击修改表格内容并提交

要实现表格填报功能,后台存储的是表格的数据结构,从后端获取json数组后,传给页面展示

页面采用cell-click时通过控制 flag值来确定时显示内容还是 input框

<el-table :data="tableData" border   @cell-click="cellDblClick">
	  <!-- <el-table-column label="id主键" align="center" prop="inspectId" /> --> 
	   <el-table-column label="检查项目" align="center" prop="name" />
	   <el-table-column prop="result" label="检查结果" >
		     <template slot-scope="{ row}" >
		        <!-- <span v-if="!scope.row.isEditCell" style="cursor:pointer"> {{resultFormat(scope.row.result)}}
		        </span> -->
					    <el-radio   v-model="row.result"   
					      v-for="dict in resultOptions"
					      :key="dict.dictValue"
					      :label="dict.dictValue"
					    >{{dict.dictLabel}}</el-radio>
<!-- 		       <el-button v-if="scope.row.isEditCell" type="success" icon="el-icon-check" size="mini" @click="submitName(scope.row)"></el-button> --> 
		     </template>
		  </el-table-column>
		  <el-table-column label="检查标准" align="center" prop="checkStandard" width="350" />
		 <el-table-column prop="hitch" label="发生故障位置" >
			 <template slot-scope="scope">
				<span v-if="!scope.row.isEditCell" style="cursor:pointer">{{scope.row.hitch}}
				</span>
				<el-input v-if="scope.row.isEditCell"  
						  v-model="scope.row.hitch" placeholder="发生故障位置"
						   @blur="cellBlur(scope.row,scope.column)"  
						 style="width:70%" ref="hitchRef"></el-input>
						<!-- <el-button v-if="scope.row.isEditCell" type="success" icon="el-icon-check" size="mini" @click="submitName(scope.row)"></el-button> -->
			 </template>
		  </el-table-column>
		  <el-table-column prop="remark" label="备注">
		  			 <template slot-scope="scope">
		  				<span v-if="!scope.row.isEditCell" style="cursor:pointer">{{scope.row.remark}}
		  				</span>
		  				<el-input v-if="scope.row.isEditCell"  
		  						  v-model="scope.row.remark" placeholder="请输入备注"
		  						 @blur="cellBlur(scope.row,scope.column)"  
		  						 style="width:70%" ref="remarkRef"></el-input>
		  			 </template>
		  </el-table-column>
		  
	</el-table>

 

脚本

 data() {
        return {
		  tableData: [],
		  resultOptions:null,
		  submitData:{},
		  inspectId:''
        }
      },
	  created() {
		this.inspectId = this.$route.params && this.$route.params.inspectId;
	    this.getTableList(this.inspectId);
		this.getDicts("sys_check_result").then(response => {
		  this.resultOptions = response.data;
		});
	  },
      methods: {
	   // 获取列表
	   getTableList(inspectId) {
			 this.loading = true;
			 listInspectItem(inspectId).then(response => {
				console.log("response")
				 	 console.log(response) 	
					 	 console.log(response.data.tabledata) 	
								 
				 this.tableData = response.data;
			   this.loading = false;
			 });
			
			 console.log("this.tableData") 
			 	 console.log(this.tableData)   
			  // 遍历表数据,为每条数据添加isEditCell属性
			  var length = this.tableData.length;
			  
			  for (var i = 0; i < length; i++) {
			    this.tableData[i].isEditCell = false;
			  }
	   },
	   // 双击编辑
	   cellDblClick(row,column) {
		    console.log(column.property)
	       if (column.property == "remark" ||column.property == "result" ||column.property == "hitch") {
	           this.$set(row, "isEditCell", true);
	       }
	       this.tableData= this.tableData.filter(item => {
	         return item;
	       }) //视图刷新
		   console.log(column.property)
		    if (column.property == "remark" ) {
		      this.$nextTick(() => {
		        this.$refs.remarkRef.focus(); // 视图出现后使input获取焦点
		      })
		   }  else {
		      this.$nextTick(() => {
		        this.$refs.hitchRef.focus(); // 视图出现后使input获取焦点
		      })
		   } 
	      
	   },
	   // 可以编辑框失去焦点
	   cellBlur(row, column) {
	      row.isEditCell= false;
	       this.$set(row, 'isEditCell', false);
	   },
	    
	   // 提交
	   submitName(row) {
		 this.loading = true;
		 let data = {inspectId: this.inspectId,result:this.tableData.toString()};
		 this.submitData.inspectId = this.inspectId;
		 
		 this.submitData.ext1 =JSON.stringify(this.tableData);
		 updateInspect(this.submitData).then(response => {
		   if (response.code === 200) {
		 	 this.loading = false;
		     this.msgSuccess("修改成功");
		     this.open = false;
		     this.getTableList(this.inspectId);
		   } else {
		     this.msgError(response.msg);
		   }
		 });
	   },
	   },

 

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现虚拟表格,需要结合vue-virtual-scroller和eltable的功能。以下是简单的实现步骤: 1. 安装vue-virtual-scroller和element-ui ```bash npm install vue-virtual-scroller element-ui ``` 2. 在Vue组件导入和注册vue-virtual-scroller和eltable组件 ```javascript import { VirtualScroller } from 'vue-virtual-scroller' import { Table, TableColumn } from 'element-ui' export default { components: { VirtualScroller, Table, TableColumn }, // ... } ``` 3. 在template使用vue-virtual-scroller和eltable组件 ```html <template> <virtual-scroller :items="items" :item-size="50" class="table-virtual-scroller"> <el-table :data="items" class="table-body" :row-class-name="tableRowClassName"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </virtual-scroller> </template> ``` 4. 在script设置数据源和样式 ```javascript export default { data() { return { items: [] // 设置数据源 } }, mounted() { // 获取数据并更新items this.items = getData() // 设置虚拟列表容器的高度 const windowHeight = window.innerHeight const tableHeaderHeight = 40 // 表头高度 const tableRowHeight = 50 // 行高 const tableBodyHeight = this.items.length * tableRowHeight // 表格体高度 const tableVirtualScrollerHeight = windowHeight - tableHeaderHeight // 表格虚拟容器高度 document.querySelector('.table-virtual-scroller').style.height = tableVirtualScrollerHeight + 'px' // 设置表格体高度 document.querySelector('.table-body__wrapper').style.height = tableBodyHeight + 'px' }, methods: { tableRowClassName({ rowIndex }) { // 自定义行样式 return rowIndex % 2 === 0 ? 'table-row-even' : 'table-row-odd' } } } ``` 5. 样式设置 ```css /* 虚拟列表容器 */ .table-virtual-scroller { overflow-y: auto; position: relative; } /* 表格体 */ .table-body__wrapper { overflow: hidden; } /* 奇数行样式 */ .table-row-odd { background-color: #f9f9f9; } /* 偶数行样式 */ .table-row-even { background-color: #e6f7ff; } ``` 以上是一个简单的虚拟表格实现方法,您可以根据自己的需求进行样式、数据处理等方面的修改

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值