基于ant-design-vue實現(xiàn)表格操作按鈕組件
使用效果
先看下默認效果

支持按鈕超過一定數(shù)量自動放到【更多】下拉按鈕里面

按鈕支持動態(tài)控制是否禁用,是否顯示。
下面是一個代碼示例。
export default Vue.extend({
data() {
return {
dataSource: [
{ id: 1, username: '張三', disabled: 1 },
{ id: 2, username: '李四', disabled: 0 },
{ id: 3, username: '王二麻子', disabled: 0 },
] as MyData[],
columns: [
//
{ title: '#', dataIndex: 'id' },
{ title: '姓名', dataIndex: 'username' },
{
title: '狀態(tài)',
dataIndex: 'disabled',
customRender: (value: number) => {
return value === 0 ? (
<a-tag color="green">啟用</a-tag>
) : (
<a-tag>禁用</a-tag>
);
},
},
useActionButtons<MyData>({
align: 'center',
title: '操作',
limit: 0,
showDivider: false,
buttons: [
{ icon: 'search', text: '查看' },
{ icon: 'edit', text: '編輯' },
{
text: '禁用',
visible: (record) => record.disabled === 0,
},
{
text: '啟用',
visible: (record) => record.disabled === 1,
},
{
icon: 'message',
text(record, index) {
return `未讀消息(${record.id.toString()})`;
},
},
{
icon: 'delete',
text: '刪除',
disabled: (record) => record.disabled === 0,
click: (record, index) => {
this.$confirm({
content: `確認刪除${record.username}嗎?`,
});
},
},
],
}),
],
};
},
});介紹用法
type useActionButtons = (config: ActionButtonsConfig) => Table.Column;
原理就是通過一個函數(shù),返回了一個帶customRender的列的配置。
參數(shù)介紹 函數(shù)聲明
export declare interface ActionButtonsConfig<T> extends Table.Column {
limit: number;
showDivider: boolean;
buttons: Array<ActionButtonsColumnConfig<T>>;
}| 參數(shù) | 類型 | 描述 |
|---|---|---|
| limit | number | 設置一個數(shù)量,超過該數(shù)量,則展示【更多】下拉按鈕。默認 0,表示按鈕將全部展示 |
| showDivider | boolean | 是否展示分隔符,默認展示 |
| buttons | ButtonConfig[] | 要展示的按鈕數(shù)組,具體配置看下面介紹 |
ButtonConfig
| 參數(shù) | 類型 | 描述 |
|---|---|---|
| text? | string 或 (record, index) => string | 設置按鈕文本,同時支持動態(tài)設置,參數(shù)為當前行的相關(guān)數(shù)據(jù) |
| icon? | string | 設置按鈕圖標 |
| click | (record, index) => void | 設置按鈕點擊事件,參數(shù)為當前行的相關(guān)數(shù)據(jù) |
| disabled? | (record, index) => boolean | 動態(tài)設置按鈕是否禁用,參數(shù)為當前行的相關(guān)數(shù)據(jù) |
| visible? | (record, index) => boolean | 動態(tài)設置按鈕是否顯示,參數(shù)為當前行的相關(guān)數(shù)據(jù) |
安裝
npm i action-buttons
使用
目前沒有寫任何樣式文件,想修改樣式的話,需要自行在項目處理。
按鈕的根節(jié)點設置了一個 class="action-buttons"
直接在頁面引入
import { useActionButtons, ActionButtons } from 'action-buttons';然后寫到 Table Columns 數(shù)組里面對應位置即可。
雖然導出了組件ActionButtons,但還是推薦直接使用提供的useActionButtons方法直接去配置表格的操作列。
源碼 https://github.com/Tickly/action-buttons
以上就是基于ant-design-vue實現(xiàn)表格操作按鈕組件的詳細內(nèi)容,更多關(guān)于ant-design-vue表格操作按鈕的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3 element-plus el-tree自定義圖標方式
這篇文章主要介紹了vue3 element-plus el-tree自定義圖標方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue實現(xiàn)html轉(zhuǎn)word與word全過程
本文介紹了如何在Vue中實現(xiàn)HTML轉(zhuǎn)Word功能,通過拖拽組件生成類似Word的界面,并實時預覽,主要使用了htmlDocx和htmlToDocx插件將HTML轉(zhuǎn)換為.docx格式,同時,還討論了檢查.docx文件是否損壞的方法2026-01-01
使用vue + less 實現(xiàn)簡單換膚功能的示例
下面小編就為大家分享一篇使用vue + less 實現(xiàn)簡單換膚功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue?cli3配置image-webpack-loader方式
這篇文章主要介紹了vue?cli3配置image-webpack-loader方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
如何用vue-pdf包實現(xiàn)pdf文件預覽,支持分頁
這篇文章主要介紹了如何用vue-pdf包實現(xiàn)pdf文件預覽,支持分頁問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

