0.1.9 • Published 3 years ago

antdesign-etable v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Ant Design Editable Table

Usage

import React, {useContext, useState} from "react";
import EditableTable from 'antdesign-etable';
import {Button} from 'antd';
import styles from './index.css';

const data = [
  {id:1,name:'Test 1',title:'Haha',status:0,desc:'Description 1',type:0,created_time:'2019-5-2'},
  {id:2,name:'Test 2',title:'Haha',status:1,desc:'Description 2',type:1,created_time:'2019-5-3'},
  {id:3,name:'Test 3',title:'Haha',status:2,desc:'Description 3',type:0,created_time:'2019-5-4'}
];
const type = ['Type 1','Type 2'];
const status = ['Normal','Abnormal','Stop'];
const cols = [
  {
    title: 'Name',
    dataIndex: 'name',
    editable:true,
    editor: {
      required: true,
    },
  },
  {
    title: 'Type',
    dataIndex: 'type',
    editable:true,
    editor: {
      type: 'select',
      options: [
        {key: 1, value: 'Type 1'},
        {key: 2, value: 'Type 2'},
      ]
    },
    render: (text, record) => (
      type[text]
    ),
  },
  {
    title: 'date',
    dataIndex: 'created_time',
    editable:true,
    editor: {
      type: 'datetime'
    }
  },
];
const allCols = [
  ...cols.slice(0,2),
  {
    title: 'title',
    dataIndex: 'title',
    editable:true,
    width: 120,
  },
  ...cols.slice(2),
  {
    title: 'status',
    dataIndex: 'status',
    editable:true,
    width: 120,
    editor: {
      type: 'select',
      options: status.map((value,key) => ({key,value}))
    },
    render: (text, record) => (
      status[text]
    ),
  }
];
export default function() {
  const [changedData,setChangedData] = useState([]);
  const fetch = (pager,filter,sorter) => {
    // Do Remote Fetch
  };
  return (
    <div className={styles.root}>
      <div style={{textAlign:'right',marginBottom:16}}><Button type="primary">save</Button></div>
      <EditableTable
        title=""
        loading={false}
        data={data}
        changedData={changedData}
        pageSize={10}
        total={100}
        cols={cols}
        allCols={allCols}
        onFetch={()=>fetch()}
        onChangedDataUpdate={(d)=>{setChangedData(d)}}
      />
    </div>
  );
}

API

EditableTable
Attributes
NameDescriptionTypeDefault Value
dataInitialization dataArray[]
changedDataUsed to save the updated data added, deleted and modifiedArray[]
colsTable ColumnsArray[]
allColsTable columns can be displayed (the format is the same as the cols attribute)Array[]
rowKeyUnique IDString'id'
newRowKeyPrefixNew data unique identification prefixString'new_'
titleTitleString or Component''
loadingRead statusBooleanfalse
pageSizeNumber of records per pageNumber10
totalTotal number of recordsNumber0
multiSelectMultiple selectionsBooleanfalse
showHeaderWhether to show the top barBooleantrue
showFooterWhether to show the bottom barBooleantrue
showToolbarWhether to show the top toolbarBooleantrue
showSelectorWhether to show the selection buttonBooleanfalse
showAddBtnWhether to show the add buttonBooleantrue
showOpBtnWhether to show edit and delete buttonsBooleantrue
showTopPagerWhether to show the top pagerBooleantrue
showBottomPagerWhether to show the bottom pagerBooleanfalse
buttonsCustom action button groupComponentNone
stylestyleObjectnull
expandedRowRenderRendered content when expanding rowReactNodenull
expandedFirstRowExpand the first row by defaultBooleanfalse
editOnSelectedEdit when a row is clickedBooleanfalse
parentFormincoming formFormInstancenull
events
NameDescriptionParametersReturn Value
canEditWhether each line is editablerecordBoolean
canRemoveWhether each row can be deletedrecordBoolean
beforeEditTriggered before editing dataNoneNone
afterEditTriggered after editing dataNoneNone
onAddDefault object for new dataNoneObject
onFetchRequest data eventpager,filter,sorterNone
onChangedDataUpdateTriggered when update data changesarrNone
onSelectRowNumber of records per pagerowsNone
onDownloadNumber of records per pagefilter,sorterNone
onExpandedRowTriggered when a row is expandedrecordNone
methods
NameDescriptionParametersReturn Value
resetTableReset table page numberNoneNone

Config

changedData
Array, used to save the changed data, each piece of data will use isNew, isUpdate, isDelete to identify whether the data is new, updated or deleted
cols
Parameter example
[{
   title: 'ID',
   dataIndex: 'id',
   editable:false,
},{
   title: 'name',
   dataIndex: 'name',
   sorter: true,
   editable:true,
   editor: {
     required: true,
     type: 'select',
     options: [
       {key: 1, value: 'Type 1'},
       {key: 2, value: 'Type 2'},
     ],
     validator: (rule,value,callback) => {
       if(data.find(d => d.name === value))
         callback('Name already exists!');
       else
         callback();
     },
   },
}]
editable:Set editable status
editor:The default type of the object is text and Supported types include select、number、datetime、checkbox,
If you need to pass in the options parameter for select
rowKey
The unique identifier of the data, must be unique, used to judge the editing status and matching data
onAdd
When you click Add, the method of initializing data can be configured to return a new data object, which can be used to set some default values
onChangedDataUpdate
This method is triggered every time you add, update, or delete, and pass in the updated array
onSelectRow
This method will pass in an array of selected objects. If it is a single-selection mode, the array only contains the objects of the currently clicked row
onDownload
Triggered when you click the toolbar to download. If the method is configured, the method will receive two parameters of filter and sorter. If there is no configuration method, the excel download of the current page will be generated by default.
0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.0

3 years ago