Skip to content

Commit

Permalink
feat: Table组件提供API 展开关闭行(#96)
Browse files Browse the repository at this point in the history
* feat: 手动展开行

* docs: 修改文档
  • Loading branch information
wanchun committed Apr 3, 2022
1 parent ca0a8a0 commit 81ead27
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/table/components/expandTr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineComponent({
<tr class={`${prefixCls}-row`}>
<td
colspan={length}
class={`${prefixCls}-td ${prefixCls}-cell`}
class={`${prefixCls}-td ${prefixCls}-cell ${prefixCls}-expanded-cell`}
>
{column.slots.default({
row,
Expand Down
6 changes: 6 additions & 0 deletions components/table/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,17 @@
&.is-size-normal {
.@{table-prefix}-cell {
padding: @padding-md;
&.@{table-prefix}-expanded-cell {
padding: 0;
}
}
}
&.is-size-small {
.@{table-prefix}-cell {
padding: @padding-xs;
&.@{table-prefix}-expanded-cell {
padding: 0;
}
}
}
.@{table-prefix}-expand-icon {
Expand Down
4 changes: 3 additions & 1 deletion components/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ export default defineComponent({
layout,
columns,
rootProps,
toggleRowExpend,
} = useTable(props, ctx);

ctx.expose &&
ctx.expose({
toggleRowSelection: handleSelect,
toggleAllSelection: handleSelectAll,
clearSelection: clearSelect,
toggleRowExpend
});

// 计算出传入columns列的对应的宽度
Expand Down
11 changes: 9 additions & 2 deletions components/table/useTableExpand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ export default ({
return expandOpenedList.includes(rowKey);
};

const handleExpand = ({ row }: { row: RowType }) => {
const toggleRowExpend = ({ row }: { row: RowType }) => {
const rowKey = getRowKey({ row });
const index = expandOpenedList.indexOf(rowKey);
if (index !== -1) {
expandOpenedList.splice(index, 1);
} else {
expandOpenedList.push(rowKey);
}
ctx.emit('expandChange', { row, expanded: !index });
return index === -1;
};

const handleExpand = ({ row }: { row: RowType }) => {
const expanded = toggleRowExpend({ row });
ctx.emit('expandChange', { row, expanded });
};

return {
expandColumn,
isExpandOpened,
toggleRowExpend,
handleExpand,
};
};
25 changes: 23 additions & 2 deletions docs/.vitepress/components/table/expand.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<template>
<f-table :data="data" rowKey="date">
<f-table
ref="tableRef"
:data="data"
rowKey="date"
@expandChange="expandChange"
>
<f-table-column v-slot="{ row }" type="expand">
<f-grid :gutter="[20, 20]" wrap>
<f-grid
:gutter="[20, 20]"
wrap
style="background: #f8f8f8; padding: 16px"
>
<f-grid-item :span="12"> 省份:{{ row.province }} </f-grid-item>
<f-grid-item :span="12"> 市区:{{ row.city }} </f-grid-item>
<f-grid-item :span="12"> 邮编:{{ row.zip }} </f-grid-item>
Expand All @@ -11,10 +20,13 @@
<f-table-column prop="date" label="日期"></f-table-column>
<f-table-column prop="name" label="姓名"></f-table-column>
</f-table>
<f-button @click="toggle">手动展开/关闭第一行</f-button>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const tableRef = ref(null);
const data = Array.from([1, 2, 3], (i) => {
return {
date: `2016-05-2016-05-2016-05-2016-05-2016-05-2016-05-2016-05-2016-05-${
Expand All @@ -27,8 +39,17 @@ export default {
zip: 200333,
};
});
const expandChange = ({ row, expanded }) => {
console.log(row, expanded);
};
const toggle = () => {
tableRef.value.toggleRowExpend({ row: data[0] });
};
return {
tableRef,
data,
toggle,
expandChange,
};
},
};
Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/components/table/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ app.use(FTable);
| 名称 | 说明 | 参数 |
| ------------------ | ---------------------------------- | ----- |
| clearSelection | 用于多选表格,清空用户的选择 | - |
| toggleRowSelection | 用于多选表格,切换某一行的选中状态 | *{row}* |
| toggleRowSelection | 用于多选表格,切换某一行的选中状态 | *toggleRowSelection({row})* |
| toggleAllSelection | 用于多选表格,切换全选和全不选 | - |
| toggleRowExpend | 用于控制某行的展开隐藏 | *toggleRowExpend({row})* |

## FTableColumn Props

Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81ead27

Please sign in to comment.