• 页面编写
  • 配置路由
  • 页面访问

在项目的react/routes目录下新建一个新的功能文件夹table及其相关的JS文件。

  1. // table/index.js
  2. import React, { Component } from 'react';
  3. import { Route, Switch } from 'react-router-dom';
  4. import { asyncRouter, nomatch } from '@choerodon/boot';
  5. const List = asyncRouter(() => import('./list'));
  6. const Index = ({ match }) => (
  7. <Switch>
  8. <Route exact path={match.url} component={List} />
  9. <Route path="*" component={nomatch} />
  10. </Switch>
  11. );
  12. export default Index;
  1. // table/list/index.js
  2. import React from 'react';
  3. import { StoreProvider } from './stores';
  4. import ListView from './ListView.js';
  5. export default (props) => (
  6. <StoreProvider {...props}>
  7. <ListView />
  8. </StoreProvider>
  9. );

2.编写stores代码

  1. // stores/adminListDataSet.js
  2. export default () => {
  3. return {
  4. autoQuery: true,
  5. selection: false,
  6. transport: {
  7. read: {
  8. url: '/base/v1/users/admin',
  9. },
  10. },
  11. fields: [
  12. { name: 'realName', type: 'string', label: '用户名' },
  13. ],
  14. queryFields: [
  15. { name: 'realName', type: 'string', label: '用户名' },
  16. { name: 'loginName', type: 'string', label: '登录名' },
  17. ],
  18. };
  19. };
  1. // stores/index.js
  2. import React, { createContext, useMemo } from 'react';
  3. import { DataSet } from 'choerodon-ui/pro';
  4. import { inject } from 'mobx-react';
  5. import { injectIntl } from 'react-intl';
  6. import AdminListDataSet from './adminListDataSet';
  7. const Store = createContext();
  8. export default Store;
  9. export const StoreProvider = injectIntl(inject('AppState')(
  10. (props) => {
  11. const { children } = props;
  12. const adminListDataSet = useMemo(() => new DataSet(AdminListDataSet()), []);
  13. const value = {
  14. ...props,
  15. adminListDataSet,
  16. };
  17. return (
  18. <Store.Provider value={value}>
  19. {children}
  20. </Store.Provider>
  21. );
  22. },
  23. ));
  1. // table/list/ListView.js
  2. import React, { useContext } from 'react';
  3. import { Table, Button } from 'choerodon-ui/pro';
  4. import Store from './stores';
  5. const { Column } = Table;
  6. const { adminListDataSet } = useContext(Store);
  7. return (
  8. <TabPage>
  9. <Header>
  10. <Button icon="playlist_add" onClick={() => console.log('创建')}>创建</Button>
  11. </Header>
  12. <Breadcrumb />
  13. <Content style={{ paddingTop: 0 }}>
  14. <Table pristine dataSet={adminListDataSet}>
  15. <Column name="realName" />
  16. <Column name="loginName" />
  17. </Table>
  18. </Content>
  19. </TabPage>
  20. );
  21. }

修改react/routes/index.js文件中配置新建文件的访问路径:

  1. // index.js
  2. import React from 'react';
  3. import { Route, Switch } from 'react-router-dom';
  4. import { inject } from 'mobx-react';
  5. import { asyncRouter, nomatch } from '@choerodon/boot';
  6. const HelloIndex = asyncRouter(() => import('./routes/hello'));
  7. const TableIndex = asyncRouter(() => import('./routes/table'));
  8. function Index({ match }) {
  9. return (
  10. <Switch>
  11. <Route path={`${match.url}/hello`} component={HelloIndex} />
  12. <Route path={`${match.url}/table`} component={TableIndex} />
  13. <Route path="*" component={nomatch} />
  14. </Switch>
  15. );
  16. }
  17. export default inject('AppState')(Index);