Skip to content

GTable 表格

以数据驱动的表格组件

所有属性都继承自element-plus

基础用法

<template>
  <g-table :columns="columns" :data="tableData" :show-page="false"></g-table>
</template>

<script setup lang="ts">
import { ITableOrFormColumn } from 'gao-components-plus';
import { GTable } from 'gao-components-plus';
import { ref } from 'vue';

type TRow = {
  name: string
  age: number
  sex: string
}

const tableData = ref([
  { name: '张三', age: 18, sex: '男' },
  { name: '李四', age: 20, sex: '女' },
  { name: '王五', age: 22, sex: '男' },
  { name: '赵六', age: 24, sex: '女' },
])

const columns = ref<ITableOrFormColumn<TRow>[]>([
  {label: '姓名', prop: 'name'},
  {label: '年龄', prop: 'age'},
  {label: '性别', prop: 'sex'}
])
</script>

<style lang="scss" scoped>

</style>

api获取数据

<template>
  <g-table :columns="columns" :api="getList" :show-page="false"></g-table>
</template>

<script setup lang="ts">
import { ITableOrFormColumn } from 'gao-components-plus';
import { GTable } from 'gao-components-plus';
import { ref } from 'vue';

type TRow = {
  name: string
  age: number
  sex: string
}

const columns = ref<ITableOrFormColumn<TRow>[]>([
  {label: '姓名', prop: 'name'},
  {label: '年龄', prop: 'age'},
  {label: '性别', prop: 'sex'}
])

function getList() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200, 
        data: [
          { name: '张三', age: 18, sex: '男' },
          { name: '李四', age: 20, sex: '女' },
          { name: '王五', age: 22, sex: '男' },
          { name: '赵六', age: 24, sex: '女' },
        ]
      })
    }, 1000)
  })
}
</script>

<style lang="scss" scoped>

</style>

分页

<template>
  <g-table :columns="columns" :api="getList" :show-page="true"></g-table>
</template>

<script setup lang="ts">
import { ITableOrFormColumn } from 'gao-components-plus';
import { GTable } from 'gao-components-plus';
import { ref } from 'vue';

type TRow = {
  name: string
  age: number
  sex: string
}



const columns = ref<ITableOrFormColumn<TRow>[]>([
  {label: '姓名', prop: 'name'},
  {label: '年龄', prop: 'age'},
  {label: '性别', prop: 'sex'}
])

function getList() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          list: [
            { name: '张三', age: 18, sex: '男' },
            { name: '李四', age: 20, sex: '女' },
            { name: '王五', age: 22, sex: '男' },
            { name: '赵六', age: 24, sex: '女' },
          ],
          total: 4
        }
      })
    }, 1000)
  })
}
</script>

<style lang="scss" scoped>

</style>

自定义插槽

两种方式templaterender

<template>
  <g-table :columns="columns" :data="tableData" :show-page="false">
    <template #name="scope">通过template自定义=={{ scope.row.name }}</template>
  </g-table>
</template>

<script setup lang="tsx">
import { ITableOrFormColumn } from 'gao-components-plus';
import { GTable } from 'gao-components-plus';
import { ref } from 'vue';

type TRow = {
  name: string
  age: number
  sex: string
}

const columns = ref<ITableOrFormColumn<TRow>[]>([
  {label: '姓名', prop: 'name'},
  {label: '年龄', prop: 'age', tableRender(scope){
    return '通过tableRender自定义==' + scope.row.age
  }},
  {label: '性别', prop: 'sex'}
])

const tableData = ref([
  { name: '张三', age: 18, sex: '男' },
  { name: '李四', age: 20, sex: '女' },
  { name: '王五', age: 22, sex: '男' },
  { name: '赵六', age: 24, sex: '女' },
])

</script>

<style lang="scss" scoped>

</style>

搜索

<template>
  <g-table :columns="columns" :api="getList" :show-page="true"></g-table>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus';
import { type ITableOrFormColumn, type GTableSearchParams } from 'gao-components-plus';
import { GTable } from 'gao-components-plus';
import { ref } from 'vue';

type TRow = {
  name: string
  age: number
  sex: string
}


const columns = ref<ITableOrFormColumn<TRow>[]>([
  {label: '姓名', prop: 'name', search: true, type: 'input'},
  {label: '年龄', prop: 'age'},
  {label: '性别', prop: 'sex'}
])

function getList(params: GTableSearchParams<TRow>) {
  ElMessage.info(`搜索参数==> ${JSON.stringify(params)}`)
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          list: [
            { name: '张三', age: 18, sex: '男' },
            { name: '李四', age: 20, sex: '女' },
            { name: '王五', age: 22, sex: '男' },
            { name: '赵六', age: 24, sex: '女' },
          ],
          total: 4
        }
      })
    }, 1000)
  })
}
</script>

<style lang="scss" scoped>

</style>

属性

除以下属性外,其他属性都继承自element-plus

名称描述类型默认值
data数据Array-
apiapi获取(...args: any[]) => Promise<any>-
colNum搜索表单列数Number1
columns表格项配置IformITableOrFormColumn[]-
pageSize每页条数Number10
showPage是否展示分页Booleantrue
paginationConfig分页配置GPaginationProps-
immediate是否立即加载数据Booleantrue

columns配置项

名称描述类型默认值
labellabelString-
prop绑定的keyString-
type表单项类型'index' 'selection' 'input' 'textarea' 'number' 'color' 'select' 'radio' 'switch' 'date' 'dates' 'daterange' 'datetime' 'datetimerange' 'month' 'months' 'monthrange''year' 'years' 'yearrange' 'week' 'weekrange'-
children多级表头IformITableOrFormColumn[]-
labelWidthlabel宽度string | number继承gform
span列宽 能被24整除的整数number24/colNum
tableRender自定义渲染(scope: ITableScope<T>) => VNodestring
clearable可被清除booleanfalse
showAlpha颜色是否显示透明(仅type=color)booleantrue
unlinkPanels左右无关联(仅type=daterange、datetimerange、monthrange、yearrange、weekrange)string-
renderPickerCell日期自定义渲染(仅type=daterange、datetimerange、monthrange、yearrange、weekrange)(cell: DateCell) => VNode | string-

emit事件

名称描述类型默认值
search搜索(params: any) => void-
reset重置() => void-

插槽

名称描述类型默认值
toolbar工具栏(params: any) => void-

除以上方法外,其他方法都继承自element-plus

实例

vue
<g-table ref="tableRef"></g-table>
<script>
const tableRef = useTemplateRef<GTableInstance>('tabelRef')

formRef.value?.GTable 返回的是element-plus的table实例

</script>

Released under the MIT License.