[vxe-table] 表头:点击出现输入框

html 复制代码
<vxe-grid v-bind="gridOptions">
  <template #header_num="{ column }">
    <div v-if="editingColumn === column.field" class="header-editor">
      <input
        v-model="editValue"
        type="text"
        class="header-input"
        @blur="saveHeader(column)"
        @keyup.enter="saveHeader(column)"
        @keyup.esc="cancelEdit"
        ref="inputRef"
      />
    </div>
    <div v-else class="header-title" @click="startEdit(column)">
      {{ column.title }}
    </div>
  </template>
</vxe-grid>
javascript 复制代码
// 编辑状态管理
const editingColumn = ref(null)
const editValue = ref('')
const inputRef = ref(null)

const gridOptions = reactive({
  border: true,
  columns: [
    { type: 'seq', width: 70, title: '序号' },
    { field: 'num1', title: 'num1', width: 200, slots: { header: 'header_num' } },
    { field: 'num2', title: 'num2', width: 200, slots: { header: 'header_num' } },
    { field: 'num3', title: 'num3', width: 200, slots: { header: 'header_num' } },
    { field: 'age', title: 'age', width: 200 },
  ],
  data: [
    { id: 10001, name: 'Test1', role: 'Develop', sex: '0', age: 28, num1: 234, num2: 234, num3: 234, address: 'test abc' },
    { id: 10002, name: 'Test2', role: 'Test', sex: '1', age: 22, num1: 34, num2: 34, num3: 34, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', role: 'PM', sex: '0', age: 32, num1: 12, num2: 12, num3: 12, address: 'Shanghai' }
  ]
})

// 开始编辑表头
const startEdit = (column) => {
  editingColumn.value = column.field
  editValue.value = column.title
  nextTick(() => {
    // 自动聚焦到输入框
    inputRef.value?.focus()
    inputRef.value?.select()
  })
}

// 保存表头编辑
const saveHeader = (column) => {
  if (editValue.value.trim()) {
    column.title = editValue.value.trim()
  }
  editingColumn.value = null
}

// 取消编辑
const cancelEdit = () => {
  editingColumn.value = null
}
相关推荐
恋猫de小郭9 小时前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭9 小时前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
小bo波10 小时前
从"任意文件复制"深挖Java I/O:字符流与字节流的本质抉择
java·nio·io流·后端开发·文件复制
代码煮茶11 小时前
React 组件封装方法论 —— 以 Todo App 为例
javascript·react.js
任沫12 小时前
Agent之Function Call
javascript·人工智能·go
贾艺驰12 小时前
实战Android Framework: 新增一个系统权限
android
默_笙13 小时前
🛬 我让 AI 帮我写了一个打飞机游戏,结果 Canvas 把我整不会了
前端·javascript
梯度不陡13 小时前
AI 到底能不能从零写软件?ProgramBench 和 RepoZero 给出了两种答案
前端·javascript·面试
胡萝卜术15 小时前
滑动窗口最大值:从暴力到单调队列,层层优化全解析
前端·javascript·面试
kyriewen16 小时前
2026 年了,这 6 个 npm 包可以卸载了——浏览器原生 API 已经能替代
前端·javascript·npm