0 概述
ant-admin使用指南,将常用的模式写下来,避免不断去查API
1 基础框体
1.1 location与history
import ContactForm from './ContactForm';
import React,{Fragment} from 'react';
export default class CustomList extends React.Component{
= ()=>{
render return (<ContactForm
={"/contact/customDetail"}
nameCache={"/contact/getCustom"}
apiGetContactUrl={"/contact/modCustom"}
apiModContactUrl={"/contact/addCustom"}
apiAddContactUrl={this.props.history}
history={this.props.location}
location/>);
} }
外层框体,会被默认传递一个history与location参数,记得透传到下一级下面。
//FIXME,push,pop,与传递state
1.2 dispatch与state
import React,{Fragment} from 'react';
import { connect } from 'redva';
import cache from '@/utils/cache';
connect((state)=>{
@return {loading:state.loading.global};
})export default class ContactList extends React.Component{
constructor(props){
super(props);
this.state = cache.get(this.props.nameCache) || {
contactCategoryInfo:{},
list:[],
where:{},
limit:{
pageIndex:0,
pageSize:10,
count:0,
}
}
}= async ()=>{
fetch let contactCategoryInfo = await this.props.dispatch({
type:'/contactCategory/getAll',
;
})....
}= ()=>{
render const role = this.props.login.role;
const loading = this.props.loading;
} }
connect是redva提供的组件,可以提供this.props.dispatch组件,以及读取全局的state对象到props中
1.3 qs
import qs from 'qs';
connect()
@export default class Form extends React.Component{
constructor(props){
super(props);
let query = qs.parse(this.props.location.search.substr(1));
....
} }
qs库是读取参数的
2 路由与菜单
3 基础组件
3.1 对话框
import MyModal from '@/components/MyModal';
= ()=>{
render = async(data,contactCategoryId)=>{
openModal this.myModal.open({
title:"编辑联系人分类",
render:()=>{
var onSubmit = ()=>{
this.myModal.close();
this.fetch();
}return (<ContactCategoryForm
={contactCategoryId}
contactCategoryId={data}
data={onSubmit}/>);
onSubmit
};
})
}return (
<MyModal ref={(node)=>(this.myModal=node)}/>
) }
正常的Modal,没有底部按钮
import MyModal from '@/components/MyModal';
= ()=>{
render = async(data,contactCategoryId)=>{
openModal var form = null;
var handleOk = ()=>{
var data = form.getSelect();
console.log(data);
}this.myModal.open({
title:"编辑联系人分类",
=[
footer<Button type="primary"onClick={handleOk}>
确定</Button>,
,
]render:()=>{
var onSubmit = ()=>{
this.myModal.close();
this.fetch();
}return (<ContactCategoryForm
={(node)=>(form=node)}
ref={contactCategoryId}
contactCategoryId={data}
data={onSubmit}/>);
onSubmit
};
})
}return (
<MyModal ref={(node)=>(this.myModal=node)}/>
) }
!!footer暂时不要用
3.2 输入框样式的按钮
= ()=>{
render return (
<div>
<MyInputButton onIconClick={this.onClearClick} onClick={this.openModal}>{this.props.valueName} </MyInputButton>
</div>);
}
用的就是value与onClick
4 容器组件
4.1 可编辑表格
import StandardSequenceTable from '@/components/StandardSequenceTable';
import MyInput from '@/components/MyInput';
import {Button} from 'antd';
:(value)=>{
rendervar del = (data)=>{
this.state.data.phones.splice(data._index,1);
this.setState({});
}var add = ()=>{
this.state.data.phones = this.state.data.phones || [];
this.state.data.phones.push({
name:"",
phone:"",
;
})this.setState({});
}var onChange = (newValue)=>{
this.state.data.phones = newValue;
this.setState({});
}const columns = [
{title:'联系人',
dataIndex:'name',
render:()=>{
return (<MyInput placeholder="请选择" />);
},
}
{title: '电话',
dataIndex: 'phone',
render:()=>{
return (<MyInput placeholder="电话"/>);
},
}
{title: '操作',
render:(val,data)=>{
return (<a onClick={del.bind(this,val)}>删除</a>);
}
};
]return (
<div>
<div>
<Button type="primary" onClick={add.bind(this)}>添加</Button>
</div>
<div>
<StandardSequenceTable
={{marginTop:'16px'}}
style={onChange}
onChange={value}
value={columns}
columns/>
</div>
</div>
) }
使用可编辑表格,以及StandardSequenceTable
4.2 展示表格
constructor(props){
super(props);
this.state = {
list:[],
where:{},
limit:{
pageIndex:0,
pageSize:10,
count:0,
}
}
}= (limit)=>{
onPaginactionChange this.state.limit = limit;
this.fetch();
}= async ()=>{
fetch let where = {
...this.state.where,
;
}let limit = {
...this.state.limit ,
count:undefined
;
}let data = await this.props.dispatch({
type:'/contact/getAll',
payload:{
...where,
...limit,
};
})this.state.limit.count = data.count;
this.state.list = data.data;
this.setState({});
}= ()=>{
render const columns = [
{title: this.props.labelName,
dataIndex: 'name',
,
}
{title: '供应商分类',
dataIndex: 'suppilerCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '客户分类',
dataIndex: 'customCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '备注',
dataIndex: 'remark',
,
}
{title: '操作',
render: (val,data) => (
<Fragment>
<a onClick={this.mod.bind(this,data.contactId)}>修改</a>
<Divider type="vertical" />
<a onClick={this.del.bind(this,data.contactId)}>删除</a>
</Fragment>
,
),
};
]return (
<StandardTable
={{marginTop:'16px'}}
style={'contactId'}
rowKey={this.props.loading}
loading={columns}
columns={this.state.list}
value={this.state.limit}
paginaction={this.onPaginactionChange}/>
onPaginactionChange;
) }
4.3 可单选表格
constructor(props){
super(props);
this.state = {
selectedRow:undefined,
}
}= (selectedRow)=>{
onSelectedRowChange this.state.selectedRow = selectedRow;
this.setState({});
}= ()=>{
render const columns = [
{title: this.props.labelName,
dataIndex: 'name',
,
}
{title: '供应商分类',
dataIndex: 'suppilerCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '客户分类',
dataIndex: 'customCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '备注',
dataIndex: 'remark',
,
}
{title: '操作',
render: (val,data) => (
<Fragment>
<a onClick={this.mod.bind(this,data.contactId)}>修改</a>
<Divider type="vertical" />
<a onClick={this.del.bind(this,data.contactId)}>删除</a>
</Fragment>
,
),
};
]return (
<StandardTable
={this.state.selectedRow}
selectedRow={this.state.onSelectedRowChange}
onSelectedRowChange={{marginTop:'16px'}}
style={'contactId'}
rowKey={this.props.loading}
loading={columns}
columns={this.state.list}
value={this.state.limit}
paginaction={this.onPaginactionChange}/>
onPaginactionChange;
) }
这个暂时不要用!!
4.4 可双击表格
constructor(props){
super(props);
this.state = {
selectedRow:null,
}
}= (selectedRow)=>{
onSelectedRowChange this.state.selectedRow = selectedRow;
this.setState({});
}= ()=>{
onRowDoubleClick let selectedRowIndex = this.state.list.findIndex((single)=>{
return single.itemSalesOrderId == this.state.selectedRow;
})if( selectedRowIndex != -1 ){
this.props.onSelect(this.state.list[selectedRowIndex]);
}
}= ()=>{
render const columns = [
{title: this.props.labelName,
dataIndex: 'name',
,
}
{title: '供应商分类',
dataIndex: 'suppilerCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '客户分类',
dataIndex: 'customCategoryId',
render:(value)=>{
return this.state.contactCategoryInfo[value] ? this.state.contactCategoryInfo[value].name:"";
},
}
{title: '备注',
dataIndex: 'remark',
,
}
{title: '操作',
render: (val,data) => (
<Fragment>
<a onClick={this.mod.bind(this,data.contactId)}>修改</a>
<Divider type="vertical" />
<a onClick={this.del.bind(this,data.contactId)}>删除</a>
</Fragment>
,
),
};
]return (
<StandardTable
={{marginTop:'16px'}}
style={'itemSalesOrderId'}
rowKey={this.props.loading}
loading={columns}
columns={this.state.list}
value={this.state.selectedRow}
selectedRow={this.onSelectedRowChange}
onSelectedRowChange={this.onRowDoubleClick}
onRowDoubleClick={this.state.limit}
paginaction={this.onPaginactionChange}/>
onPaginactionChange;
) }
4.5 展示树形控件
constructor(props){
super(props);
this.state = {
contactCategoryId:0,
}
}= (contactCategoryId)=>{
onTreeSelectChange this.state.contactCategoryId = contactCategoryId;
this.state.limit.pageIndex = 0;
this.setState({});
this.fetch();
}= ()=>{
getTreeCategory let result = {};
let all = this.state.contactCategoryInfo || {};
for( var i in all ){
var single = all[i];
if( single.fullId.startsWith(this.props.dataCategoryFullIdStart)){
= single;
result[i]
}
}return result;
}= (input,data)=>{
filterNode if( data.name.indexOf(input) != -1 ){
return true;
}return false;
}= ()=>{
render return (<MyTreeList
={this.state.contactCategoryId}
value={this.onTreeSelectChange}
onChange={this.getTreeCategory()}
nodes={this.filterNode}
filterNode={(data)=>(data.name)}/>);
renderNode }
展示树形控件
5 数据流动控件
5.1 redva
import { connect } from 'redva';
connect()
@class List extends React.Component{
}
注意connect的引入
- 本文作者: fishedee
- 版权声明: 本博客所有文章均采用 CC BY-NC-SA 3.0 CN 许可协议,转载必须注明出处!