[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
}
相关推荐
alxraves5 分钟前
医用超声远程会诊系统:会诊平台的核心架构与功能解析
java·人工智能·架构
带刺的坐椅5 分钟前
用 Solon AI ReActAgent 落地智能客服工单处理
java·ai·llm·agent·solon·react-agent
我是坏垠39 分钟前
Crypto、Cipher与Password:Java加密开发的三个核心概念
java·开发语言·python
white_ant1 小时前
JDK LTS 版本升级迁移注意事项大全
java·开发语言
你怎么知道我是队长1 小时前
JavaScript的变量和数据类型介绍
开发语言·javascript·ecmascript
名字还没想好☜2 小时前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司3 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
库克克3 小时前
【C++ 】内联函数
java·开发语言·c++
zh_xuan3 小时前
java 虚拟线程
java·开发语言·虚拟线程
心中有国也有家3 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos