【问题标题】:Vue 3 Element-plus Virtualized Table fill with real dataVue 3 Element-plus 虚拟表填充真实数据
【发布时间】:2023-01-15 14:16:39
【问题描述】:

我是前端的新手,我正在尝试用真实数据实现 Element-plus 虚拟化表。基本示例结构是:

const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
    Array.from({ length }).map((_, columnIndex) => ({
        ...props,
        key: `${prefix}${columnIndex}`,
        dataKey: `${prefix}${columnIndex}`,
        title: `Column ${columnIndex}`,
        width: 150,
  }))

const generateData = (
    columns: ReturnType<typeof generateColumns>,
    length = 200,
    prefix = 'row-'
) =>
    Array.from({ length }).map((_, rowIndex) => {
        return columns.reduce(
            (rowData, column, columnIndex) => {
            rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
            return rowData
      },
      {
          id: `${prefix}${rowIndex}`,
          parentId: null,
      }
    )
  })

const columns = generateColumns(10)
const data = generateData(columns, 1000)

我有像这样的真实数据

const fetchedData = [
    { adress: "...", protocol: ["..."], email: ["..."] },
    { adress: "...", protocol: ["...", "..."], email: ["..."] },
    { adress: "...", protocol: ["..."], email: ["...", "..."] },
];

问题是如何将我的真实数据实现为示例中的函数?

【问题讨论】:

    标签: javascript arrays vue.js element-plus


    【解决方案1】:
    <script setup lang="ts">
    
    let employees = await employeeStore.getEmployees(10,0,'updated_at~desc');
    
    
    const headers = ['name', 'comapny name', 'registered at', 'updated at']
    
    const generateColumns = (length = employees.length, prefix = 'column-', props?: 
    any) =>
    Array.from({ length }).map((_, columnIndex) => ({
    ...props,
    key: `${prefix}${columnIndex}`,
    dataKey: `${prefix}${columnIndex}`,
    title: `${headers[columnIndex]}`, // change here
    width: 250,
    }))
    
    const generateData = (
    columns: ReturnType<typeof generateColumns>,
    length = employees.length,
    prefix = 'row-'
    ) =>
    Array.from({ length }).map((_, rowIndex) => {
    
    return columns.reduce(
      (rowData, column, columnIndex) => {
        // rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex} 
    
                          // change here //
    
        rowData['column-0'] = `${employees[rowIndex].first_name}`
        rowData['column-1'] = `${employees[rowIndex].email}`
        rowData['column-2'] = `${employees[rowIndex].created_at}`
        rowData['column-3'] = `${employees[rowIndex].updated_at}`
        return rowData
      },
      {
        id: `${prefix}${rowIndex}`,
        parentId: null,
      }
    )
    })
    
    const columns = generateColumns()
    const data = generateData(columns)
    </script>
    
    // inside your template code
    
    <template>
    
     <el-table-v2
        :columns="columns"
        :data="data"
        :width="1000"
        :height="400"
        fixed       
        />
    
    </template>
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2022-06-25
      • 2022-01-07
      • 2012-01-12
      • 2019-05-04
      • 2023-02-08
      • 2017-10-08
      • 2011-06-13
      • 2011-02-23
      相关资源
      最近更新 更多