[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
}
相关推荐
Hilaku7 小时前
一行 CSS 新特性干掉 20 行 JavaScript ?
前端·javascript·程序员
三8447 小时前
PHP 绕过污点追踪构造 Webshell:反序列化引用复用与 Trait 可变函数 RCE
android
风月说与山鬼7 小时前
JS闭包详解
开发语言·javascript
石头猫灯7 小时前
WordPress wp2shell漏洞断点逐过程到注入点
android·学习
吴声子夜歌7 小时前
Java面试——数据结构(一)
java·数据结构·面试
罗超驿7 小时前
SpringBoot 快速入门
java·spring boot·后端
疯狂打码的少年7 小时前
【数据结构】选择类排序:简单选择与堆排序
java·数据结构·笔记·算法
粥里有勺糖8 小时前
视野修炼-技术周刊第130期 | 光影效果
前端·javascript·github
吴声子夜歌8 小时前
Java面试——基础
java·开发语言·面试
Android打工仔8 小时前
Kotlin 协程源码解析(五):BaseContinuationImpl.resumeWith() —— Continuation 链是如何被展开的?
android·kotlin