当前位置:首页 > uni-app

uniapp 编辑表格

2026-02-06 12:55:09uni-app

在 uniapp 中编辑表格

在 uniapp 中编辑表格可以通过多种方式实现,以下是一些常见的方法和实现步骤。

使用 u-table 组件

uniapp 官方没有提供专门的表格组件,但可以使用第三方组件库如 uview-ui 中的 u-table 组件。

安装 uview-ui

uniapp 编辑表格

npm install uview-ui

main.js 中引入:

import uView from 'uview-ui'
Vue.use(uView)

在页面中使用 u-table

uniapp 编辑表格

<template>
  <view>
    <u-table>
      <u-tr>
        <u-th>姓名</u-th>
        <u-th>年龄</u-th>
        <u-th>操作</u-th>
      </u-tr>
      <u-tr v-for="(item, index) in tableData" :key="index">
        <u-td>{{ item.name }}</u-td>
        <u-td>{{ item.age }}</u-td>
        <u-td>
          <u-button @click="editItem(index)">编辑</u-button>
        </u-td>
      </u-tr>
    </u-table>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ]
    }
  },
  methods: {
    editItem(index) {
      // 编辑逻辑
    }
  }
}
</script>

使用原生 table 标签

uniapp 支持 HTML 原生 table 标签,可以通过原生方式实现表格编辑功能。

<template>
  <view>
    <table>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>
          <button @click="editItem(index)">编辑</button>
        </td>
      </tr>
    </table>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ]
    }
  },
  methods: {
    editItem(index) {
      // 编辑逻辑
    }
  }
}
</script>

使用 uni-forms 动态表单

如果需要动态编辑表格内容,可以结合 uni-forms 实现表单编辑功能。

<template>
  <view>
    <uni-forms ref="form">
      <uni-forms-item v-for="(item, index) in tableData" :key="index">
        <input v-model="item.name" placeholder="姓名" />
        <input v-model="item.age" placeholder="年龄" />
        <button @click="removeItem(index)">删除</button>
      </uni-forms-item>
    </uni-forms>
    <button @click="addItem">添加</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ]
    }
  },
  methods: {
    addItem() {
      this.tableData.push({ name: '', age: '' })
    },
    removeItem(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

使用第三方表格库

如果需要更复杂的表格功能,可以使用第三方库如 vxe-tableelement-ui 的表格组件,通过条件编译在 H5 端使用。

<template>
  <view>
    <!-- #ifdef H5 -->
    <vxe-table :data="tableData">
      <vxe-column field="name" title="姓名"></vxe-column>
      <vxe-column field="age" title="年龄"></vxe-column>
      <vxe-column title="操作">
        <template #default="{ row }">
          <button @click="editItem(row)">编辑</button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- #endif -->
  </view>
</template>

<script>
// #ifdef H5
import VxeTable from 'vxe-table'
// #endif

export default {
  // #ifdef H5
  components: { VxeTable },
  // #endif
  data() {
    return {
      tableData: [
        { name: '张三', age: 20 },
        { name: '李四', age: 25 }
      ]
    }
  },
  methods: {
    editItem(row) {
      // 编辑逻辑
    }
  }
}
</script>

注意事项

  • 跨平台兼容性:不同平台对表格的支持不同,H5 端支持较好,小程序和 App 端可能需要使用特定组件或自定义实现。
  • 性能优化:大量数据渲染时需考虑性能,可使用虚拟滚动或分页加载。
  • 样式调整:表格样式可能需要根据平台调整,确保在不同设备上显示正常。

通过以上方法,可以在 uniapp 中实现表格的编辑功能,根据需求选择适合的方案。

标签: 表格编辑
分享给朋友:

相关文章

vue实现子表格

vue实现子表格

实现子表格的基本思路 在Vue中实现子表格通常需要结合组件化思想和动态渲染技术。常见的场景是在主表格的行内嵌套子表格,展开主表行时显示对应的子表数据。 使用el-table实现子表格(Element…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

vue实现的表格

vue实现的表格

Vue 实现表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 的 <table> 标签创建表格。数据可以通过 v-for 指令动态渲染。 <templ…

vue实现表格选择

vue实现表格选择

Vue 实现表格选择功能 在 Vue 中实现表格选择功能可以通过多种方式完成,常见的方法包括使用复选框、单选框或通过点击行高亮选中。以下是几种常见的实现方式。 使用复选框实现多选 在表格的每一行添加…

vue实现表格打印

vue实现表格打印

Vue 实现表格打印的方法 使用 window.print() 方法 在 Vue 中可以通过调用浏览器的 window.print() 方法实现打印功能。创建一个打印按钮,点击时触发打印事件。 &l…

vue表格预览实现

vue表格预览实现

Vue 表格预览实现 使用 Element UI 的表格组件 Element UI 提供了功能丰富的表格组件,可以快速实现数据展示和预览。安装 Element UI 后,通过 el-table 组件渲…