记录在项目开发过程中遇到的小问题,积累开发经验,欢迎大家一起留言探讨
在工作中,碰到了双击可修改表格内容的需求,项目采用的是ElementUI 前端框架
1、在表格中声明双击事件
<el-table
......
@cell-dblclick="tableDbEdit"
......
</el-table>
2、编写方法实现功能
tableDbEdit(row, column, cell, event) {
console.log(row, column, cell, event);
let noEditColumns = [];
noEditColumns = ["name", "sid", "priority", "classtypeChinese", "操作"];
if (noEditColumns.indexOf(column.property) != -1) {
return;
}
let msg = row[column.property];
event.target.innerHTML = "";
let cellInput = document.createElement("input");
cellInput.value = msg;
cellInput.setAttribute("type", "input");
cellInput.style.width = "80%";
cellInput.style.height = "30px";
cellInput.style.paddingLeft = "10px";
cellInput.style.border = "1px solid #288EFE";
cellInput.style.borderRadius = "3px";
cell.appendChild(cellInput);
cellInput.focus();
cellInput.onblur = async () => {
if (cellInput.value == "") {
cell.removeChild(cellInput);
event.target.innerHTML = msg;
} else {
cell.removeChild(cellInput);
event.target.innerHTML = cellInput.value;
let form = {
sid: column.property === 'sid' ? cellInput.value : row.sid,
tag: column.property === 'tag' ? cellInput.value : row.tag,
msg: column.property === 'msg' ? cellInput.value : row.msg,
chineseName: column.property === 'chineseName' ? cellInput.value : row.chineseName
}
const {data} = await RuleUpdate(form)
console.log(data)
}
};
},
3、双击看效果
除去我们定义的排除编辑的列,其余列都可以进行进行双击修改的操作。
|