[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
}
相关推荐
天若有情6733 小时前
颠覆C++传统玩法!Property属性与伪类,开辟静态语言新维度
java·c++·servlet
写不来代码的草莓熊3 小时前
el-date-picker ,自定义输入数字自动转换显示yyyy-mm-dd HH:mm:ss格式 【仅双日历 datetimerange专用】
开发语言·前端·javascript
硅基导游3 小时前
linux系统与进程内存使用情况探测
java·linux·运维
邹阿涛涛涛涛涛涛3 小时前
Jetpack Compose Modifier 深度解析:从链式调用到 Modifier.Node
android
LucaJu3 小时前
Java + EasyExcel 实现单个接口导出多个Excel
java·excel
Ava的硅谷新视界3 小时前
TypeScript 中用判别联合类型替代 instanceof 检查
前端·javascript·typescript
橘子编程3 小时前
密码学完全指南:从基础到实战
java·python·密码学
J2虾虾3 小时前
使用Idea当Jar包的反编译
java·intellij-idea·jar
白露与泡影3 小时前
2026 全新 Java 面试题汇总(含答案)
java·开发语言