vxe-table 使用 vxe-upload 在表格中實現非常強大的貼上上傳圖片和附件

可不简单發表於2024-12-04

看看 vxe-table 渲染器強大到什麼地步;在開發需求中,經常會在表格列表中放入圖片展示,例如頭像、影片圖片,附件列表等,但需要對錶格批次操作是,會比較繁瑣,那麼有沒有方便操作一點的放呢,肯定是有的;

配合 vxe-upload 上傳;比如複製或者截圖一張圖片,透過貼上方式快速貼上到單元格中,能支援單張、多張、檢視、預覽。上傳精進度等。

官網:https://vxetable.cn

渲染 vxe-upload,實現貼上、拖拽上傳

<template>
  <div>
    <vxe-grid v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data () {
    const fileList2CellRender = {
      name: 'VxeUpload',
      props: {
        multiple: true,
        showButtonText: false,
        pasteToUpload: true,
        moreConfig: {
          maxCount: 1,
          layout: 'horizontal'
        },
        uploadMethod ({ file }) {
          const formData = new FormData()
          formData.append('file', file)
          return axios.post('/api/pub/upload/single', formData).then((res) => {
            // { url: ''}
            return {
              ...res.data
            }
          })
        }
      }
    }

    const imgList2CellRender = {
      name: 'VxeUpload',
      props: {
        mode: 'image',
        multiple: true,
        showButtonText: false,
        pasteToUpload: true,
        moreConfig: {
          maxCount: 1
        },
        imageStyle: {
          width: 40,
          height: 40
        },
        uploadMethod ({ file }) {
          const formData = new FormData()
          formData.append('file', file)
          return axios.post('/api/pub/upload/single', formData).then((res) => {
            // { url: ''}
            return {
              ...res.data
            }
          })
        }
      }
    }

    const gridOptions = {
      border: true,
      showOverflow: true,
      columnConfig: {
        resizable: true
      },
      columns: [
        { type: 'seq', width: 70 },
        { field: 'name', title: 'Name', minWidth: 180 },
        { field: 'fileList2', title: '上傳附件', width: 300, cellRender: fileList2CellRender },
        { field: 'imgList2', title: '上傳圖片', width: 210, cellRender: imgList2CellRender }
      ],
      data: [
        { id: 10001, name: 'Test1', imgList2: [], fileList2: [{ name: 'fj562.png', url: 'https://vxeui.com/resource/img/fj562.png' }] },
        { id: 10002, name: 'Test2', imgList2: [{ name: 'fj562.png', url: 'https://vxeui.com/resource/img/fj562.png' }, { name: 'fj573.jpeg', url: 'https://vxeui.com/resource/img/fj573.jpeg' }], fileList2: [] },
        { id: 10003, name: 'Test3', imgList2: [{ name: 'fj577.jpg', url: 'https://vxeui.com/resource/img/fj577.jpg' }], fileList2: [{ name: 'fj562.png', url: 'https://vxeui.com/resource/img/fj562.png' }, { name: 'fj573.jpeg', url: 'https://vxeui.com/resource/img/fj573.jpeg' }, { name: 'fj187.jpg', url: 'https://vxeui.com/resource/img/fj187.jpg' }] }
      ]
    }
    return {
      gridOptions,
      fileList2CellRender,
      imgList2CellRender
    }
  }
}
</script>

檢視 Gitee https://gitee.com/x-extends/vxe-table

相關文章