Skip to content

Commit 2161705

Browse files
RJQingHuanMoonofweisheng
authored andcommitted
feat: ✨ table组件添加index参数
1 parent 50133b9 commit 2161705

10 files changed

Lines changed: 106 additions & 17 deletions

File tree

docs/component/table.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ const dataList = reactive([
5151
</wd-table>
5252
```
5353

54+
## 显示索引
55+
56+
通过`index`设置表格是否显示序号列,默认为`false`。同时也可以传入对象对序号列进行配置,参数同`TableColumnProps`
57+
58+
```html
59+
<wd-table :data="dataList" height="328px" :index="true">
60+
<wd-table-col prop="name" label="姓名" :sortable="true"></wd-table-col>
61+
<wd-table-col prop="grade" label="分数" :sortable="true"></wd-table-col>
62+
<wd-table-col prop="hobby" label="一言以蔽之" :sortable="true" :width="160"></wd-table-col>
63+
</wd-table>
64+
65+
<wd-table :data="dataList" height="328px" :index="{ align: 'center', width: 200 }">
66+
<wd-table-col prop="name" label="姓名" :sortable="true" align="center"></wd-table-col>
67+
<wd-table-col prop="grade" label="分数" :sortable="true" align="center"></wd-table-col>
68+
<wd-table-col prop="hobby" label="一言以蔽之" :sortable="true" :width="160"></wd-table-col>
69+
</wd-table>
70+
```
71+
5472
## 斑马纹
5573

5674
通过`stripe`设置表格是否展示斑马纹,默认`true`
@@ -216,15 +234,16 @@ function handleSort(e) {
216234

217235
## Attributes
218236

219-
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
220-
| ---------- | -------------------------- | ----------------- | ------ | ------ | -------- |
221-
| data | 显示的数据 | Array | - | - | 0.0.39 |
222-
| border | 是否带有边框 | boolean | - | true | 0.0.39 |
223-
| stripe | 是否为斑马纹表 | boolean | - | true | 0.0.39 |
224-
| height | Table 的高度,默认为`80vh` | string | - | `80vh` | 0.0.39 |
225-
| rowHeight | 行高 | `number / string` | - | 50 | 0.0.39 |
226-
| showHeader | 是否显示表头 | boolean | - | true | 0.0.39 |
227-
| ellipsis | 是否超出 2 行隐藏 | boolean | - | true | 0.0.39 |
237+
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
238+
| ---------- | ------------------------------------------------- | ---------------------------- | ------ | ------ | ---------------- |
239+
| data | 显示的数据 | Array | - | - | 0.0.39 |
240+
| border | 是否带有边框 | boolean | - | true | 0.0.39 |
241+
| stripe | 是否为斑马纹表 | boolean | - | true | 0.0.39 |
242+
| height | Table 的高度,默认为`80vh` | string | - | `80vh` | 0.0.39 |
243+
| rowHeight | 行高 | `number / string` | - | 50 | 0.0.39 |
244+
| showHeader | 是否显示表头 | boolean | - | true | 0.0.39 |
245+
| ellipsis | 是否超出 2 行隐藏 | boolean | - | true | 0.0.39 |
246+
| index | 是否显示索引列,可传入`boolean`也可传入column配置 | `boolean / TableColumnProps` | | false | $LOWEST_VERSION$ |
228247

229248
## TableColumn Attributes
230249

src/pages/table/Index.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939
</wd-table>
4040
</demo-block>
4141

42+
<demo-block title="显示索引">
43+
<wd-table :data="dataList" height="328px" :index="{ align: 'center' }">
44+
<wd-table-col prop="name" label="姓名" :sortable="true" align="center"></wd-table-col>
45+
<wd-table-col prop="grade" label="分数" :sortable="true" align="center"></wd-table-col>
46+
<wd-table-col prop="hobby" label="一言以蔽之" :sortable="true" :width="160"></wd-table-col>
47+
<wd-table-col prop="school" label="求学之所" :width="180"></wd-table-col>
48+
<wd-table-col prop="major" label="专业"></wd-table-col>
49+
<wd-table-col prop="gender" label="性别"></wd-table-col>
50+
</wd-table>
51+
</demo-block>
52+
4253
<demo-block title="自定义列模板">
4354
<wd-table :data="dataList" @sort-method="doSort" @row-click="handleRowClick" height="328px">
4455
<wd-table-col prop="name" label="姓名" fixed :sortable="true" align="center"></wd-table-col>

src/uni_modules/wot-design-uni/components/wd-table/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
import type { ExtractPropTypes } from 'vue'
1111
import { baseProps, makeBooleanProp, makeNumberProp, makeRequiredProp, makeStringProp } from '../common/props'
12+
import type { TableColumnProps } from '../wd-table-col/types'
13+
import type { PropType } from 'vue'
1214

1315
export const tableProps = {
1416
...baseProps,
@@ -39,7 +41,14 @@ export const tableProps = {
3941
/**
4042
* 是否超出2行隐藏
4143
*/
42-
ellipsis: makeBooleanProp(true)
44+
ellipsis: makeBooleanProp(true),
45+
/**
46+
* 是否显示索引列
47+
*/
48+
index: {
49+
type: [Object, Boolean] as PropType<boolean | Omit<Partial<TableColumnProps>, 'prop'>>,
50+
default: false
51+
}
4352
}
4453

4554
export type TableProps = ExtractPropTypes<typeof tableProps>

src/uni_modules/wot-design-uni/components/wd-table/wd-table.vue

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@
4040
:scrollLeft="reactiveState.scrollLeft"
4141
>
4242
<view id="table-body" class="wd-table__content" :style="realWidthStyle">
43+
<wd-table-col
44+
v-if="index !== false"
45+
:prop="indexColumn.prop"
46+
:label="indexColumn.label"
47+
:width="indexColumn.width"
48+
:sortable="indexColumn.sortable"
49+
:fixed="indexColumn.fixed"
50+
:align="indexColumn.align"
51+
>
52+
<template #value="{ index }">
53+
<text>{{ index + 1 }}</text>
54+
</template>
55+
</wd-table-col>
4356
<slot></slot>
4457
</view>
4558
</scroll-view>
@@ -58,10 +71,14 @@ export default {
5871
</script>
5972

6073
<script lang="ts" setup>
61-
import { type CSSProperties, computed, provide, watch, reactive } from 'vue'
62-
import { addUnit, debounce, deepClone, isDef, objToStyle } from '../common/util'
63-
import type { SortDirection, TableColumn } from '../wd-table-col/types'
74+
import { type CSSProperties, computed, provide, watch, reactive, ref } from 'vue'
75+
import { addUnit, debounce, deepClone, isDef, isObj, objToStyle, uuid } from '../common/util'
76+
import type { SortDirection, TableColumn, TableColumnProps } from '../wd-table-col/types'
6477
import { tableProps } from './types'
78+
import WdTableCol from '../wd-table-col/wd-table-col.vue'
79+
import { useTranslate } from '../composables/useTranslate'
80+
81+
const { translate } = useTranslate('tableCol')
6582
6683
const props = defineProps(tableProps)
6784
const emit = defineEmits(['click', 'sort-method', 'row-click'])
@@ -79,6 +96,17 @@ const reactiveState = reactive({
7996
setColumns
8097
})
8198
99+
const indexUUID = uuid()
100+
const indexColumn = ref<TableColumnProps>({
101+
prop: indexUUID,
102+
label: translate('indexLabel'),
103+
width: '100rpx',
104+
sortable: false,
105+
fixed: false,
106+
align: 'left',
107+
...(isObj(props.index) ? props.index : {})
108+
})
109+
82110
const scroll = debounce(handleScroll, 100, { leading: false }) // 滚动事件
83111
84112
provide('wdTable', reactiveState)
@@ -191,7 +219,11 @@ function isLastFixed(column: TableColumn) {
191219
* @param column
192220
*/
193221
function setColumns(column: TableColumn) {
194-
reactiveState.columns = deepClone([...reactiveState.columns, column])
222+
if (column.prop === indexUUID) {
223+
reactiveState.columns = deepClone([column, ...reactiveState.columns])
224+
} else {
225+
reactiveState.columns = deepClone([...reactiveState.columns, column])
226+
}
195227
}
196228
197229
/**

src/uni_modules/wot-design-uni/locale/lang/en-US.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ export default {
120120
},
121121
textarea: {
122122
placeholder: 'Please input information...'
123+
},
124+
tableCol: {
125+
indexLabel: 'index'
123126
}
124127
}

src/uni_modules/wot-design-uni/locale/lang/th-TH.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ export default {
120120
},
121121
textarea: {
122122
placeholder: 'กรุณาใส่ข้อมูล...'
123+
},
124+
tableCol: {
125+
indexLabel: 'หมายเลขซีเรียล'
123126
}
124127
}

src/uni_modules/wot-design-uni/locale/lang/vi-VN.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@ export default {
6161
input: { placeholder: 'Vui lòng nhập...' },
6262
selectPicker: { title: 'Vui lòng chọn', placeholder: 'Vui lòng chọn', select: 'Xác nhận', confirm: 'Xác nhận', filterPlaceholder: 'Tìm kiếm' },
6363
tag: { placeholder: 'Vui lòng nhập', add: 'thêm' },
64-
textarea: { placeholder: 'Vui lòng nhập...' }
64+
textarea: { placeholder: 'Vui lòng nhập...' },
65+
tableCol: {
66+
indexLabel: 'Số sê-ri'
67+
}
6568
}

src/uni_modules/wot-design-uni/locale/lang/zh-CN.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,8 @@ export default {
120120
},
121121
textarea: {
122122
placeholder: '请输入...'
123+
},
124+
tableCol: {
125+
indexLabel: '序号'
123126
}
124127
}

src/uni_modules/wot-design-uni/locale/lang/zh-HK.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@ export default {
5454
input: { placeholder: '請輸入...' },
5555
selectPicker: { title: '請選擇', placeholder: '請選擇', select: '確認', confirm: '確認', filterPlaceholder: '搜索' },
5656
tag: { placeholder: '請輸入', add: '新增標籤' },
57-
textarea: { placeholder: '請輸入...' }
57+
textarea: { placeholder: '請輸入...' },
58+
tableCol: {
59+
indexLabel: '序號'
60+
}
5861
}

src/uni_modules/wot-design-uni/locale/lang/zh-TW.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@ export default {
5454
input: { placeholder: '請輸入...' },
5555
selectPicker: { title: '請選擇', placeholder: '請選擇', select: '確認', confirm: '確認', filterPlaceholder: '搜索' },
5656
tag: { placeholder: '請輸入', add: '新增標籤' },
57-
textarea: { placeholder: '請輸入...' }
57+
textarea: { placeholder: '請輸入...' },
58+
tableCol: {
59+
indexLabel: '序號'
60+
}
5861
}

0 commit comments

Comments
 (0)