如何基于tristana项目模板,打造一个基本列表展示 #100

简介: 如何基于tristana项目模板,打造一个基本列表展示 #100

基于tristana这套项目模板,引导大家如何创建列表展示,搜索,自动处理 loading 状态等。全部代码在仓库下。

最终效果:

网站效果预览:https://order.downfuture.com


开始之前


  1. 确保 node 版本是 8.4 或以上
  2. 用 cnpm 或 yarn 能节约你安装依赖的时间

第 1 步。安装 tristana 并创建项目


$ git clone https://github.com/Cherry-Team/tristana.git
$ cd tristana
$ cnpm i
$ npm start

浏览器会自动开启,并打开 http://localhost:8080


第 2 步。生成Dashboard 路由


我们要新增路由,新建页面文件和在routeConfig引入该文件即可。

新建 ,内容如下:src/pages/Dashboard/index.js

import React, { Component } from 'react';
class Index extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <section>
                123
            </section>
        );
    }
}
export default Index;

然后在 文件下,引入该组件src/routeConfig

// 路由配置文件
import React, { lazy } from 'react';
import PrivateRoute from './components/PrivateRoute/index';
const Dashboard = lazy(() => import(/* webpackChunkName: "Dashboard"*/'./pages/Dashboard/index'));
const routes = [
    {
        // 仪表盘页
        path: '/dashboard',
        component: Dashboard
    }
];
const RouteWithSubRoutes = route => {
    return <PrivateRoute exact path={route.path} component={route.component} />;
};
const routeConfig = routes.map((route, i) => <RouteWithSubRoutes key={i} {...route} />);
export default routeConfig;

然后访问 http://localhost:8080/#/dashboard,你会看到 123 的输出。


第 3 步。构造 mobx 和 service


新增 ,内容如下:src/mobx/Dashboard/store.js

import { observable, action, runInAction } from 'mobx';
import BasicStore, { initLoading } from '../basicStore';
import { isResultError } from '../../utils/index';
import * as api from '../../servers/dashboard';
class DashBoardStore extends BasicStore {
    @observable list = [];
    @initLoading
    @action
    async getTable() {
        const list = await api.getTable();
        runInAction(() => {
            this.list = isResultError(list);
        });
    }
}
export default DashBoardStore;

basicStor类,是我单独封装的,用于监听每个请求,这样就不用手动处理loading。

然后在引入:src/mobx/rootStore.js

import DashboardStore from './Dashboard/store';
class Store {
    constructor() {
        this.dashboardStore = new DashboardStore();
    }
}
export default new Store();

新建 :src/servers/dashboard.js

import request from '../request';
// 获取表格数据
export function getTable(params = {}) {
    return request({
        url: 'getTable',
        method: 'POST',
        data: params
    });
}


Step 4. 页面组件引入mobx


在文件中,引入:src/pages/Dashboard/index.js

import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
@inject('dashboardStore')
@observer
class Index extends Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
        const { dashboardStore } = this.props;
        dashboardStore.getTable();
    }
    render() {
        const { dashboardStore: { list }, dashboardStore } = this.props;
        return (
            <section>
                123
            </section>
        );
    }
}
export default Index;


Step 5. 添加界面,让列表展现出来


在src/pages/Dashboard/index.js文件中,引入筛选组件和列表组件:

筛选组件:

// 筛选项
const OrderSearch = (props) => {
    const { handleSubmit } = props;
    return (
        <section className="search">
            <Form
                layout="inline"
                name="orderListSearch"
                onFinish={handleSubmit}
            >
                <Form.Item
                    label="订单编号"
                    name="orderId"
                >
                    <Input placeholder="请输入订单编号" style={{ width: colWidth }} />
                </Form.Item>
                <Form.Item
                    label="客户名称"
                    name="customerName"
                >
                    <Input placeholder="请输入客户名称" style={{ width: colWidth }} />
                </Form.Item>
                <Form.Item
                    label="下单方式"
                    name="placeOrder"
                >
                    <Select
                        allowClear
                        style={{ width: colWidth }}
                        placeholder="请选择下单方式"
                    >
                        <Option value={1}>自主下单</Option>
                        <Option value={2}>代下单</Option>
                    </Select>
                </Form.Item>
                <section className="button">
                    <Form.Item>
                        <Button
                            htmlType="submit"
                        >
                            重置
                        </Button>
                        <Button
                            htmlType="submit"
                            className="btn-left"
                        >
                            导出
                        </Button>
                        <Button
                            type="primary"
                            htmlType="submit"
                            className="btn-left"
                        >
                            搜索
                        </Button>
                    </Form.Item>
                </section>
            </Form>
        </section>
    )
}

列表组件:

// 订单表格
const OrderTable = ({ list, isLoading }) =>  {
    // 表格列配置
    const columns = [
        {
            title: '订单编号',
            dataIndex: 'orderId'
        },
        {
            title: '客户名称',
            dataIndex: 'customerName'
        },
        {
            title: '下单方式',
            dataIndex: 'placeOrder'
        },
        {
            title: '商品名称',
            dataIndex: 'goodsName'
        },
        {
            title: '价格',
            dataIndex: 'price'
        },
        {
            title: '下单时间',
            dataIndex: 'placeOrderTime'
        }
    ];
    return (
        <Table
            columns={columns}
            dataSource={list || []}
            loading={isLoading}
            rowKey="orderId"
        />
    );
}

在Index 类下引入两个组件:

render() {
    const { dashboardStore: { list }, dashboardStore } = this.props;
    return (
        <section className="dashboard">
            <OrderSearch handleSubmit={this.handleSubmit} />
            <OrderTable list={list} isLoading={dashboardStore.isLoading.get('getTable')} />
        </section>
    );
}

dashboardStore.isLoading.get('getTable')用于判断请求是否完成,bool值,true ||假

目录
相关文章
|
计算机视觉 C++ Windows
关于 百度飞浆paddleOCR编译32位版本 的解决方案
关于 百度飞浆paddleOCR编译32位版本 的解决方案
关于 百度飞浆paddleOCR编译32位版本 的解决方案
|
SQL DataWorks 应用服务中间件
【教程】利用宜撘实现自动生成业务SQL功能
模板地址: https://yida.alibaba-inc.com/newApp.html?#/template/TPL_LPS8OWKVUIEHEVM80XFN?_k=z3r1e4 背景: 之前在内网上有讨论SQL算不算是编程语言,一石激起千层浪,大家参与度非常高,有很多运营同学也表示会用SQL,我觉得这个是很好的现象。编程语言只是解决问题的工具,黑猫白猫,能抓到老鼠就是好猫。 这个问
1577 0
|
JSON 数据库 数据安全/隐私保护
Django系列:Django应用(app)的创建与配置
应用是Django项目的组成部分,一个Django项目可以分为多个应用,本文讲解Django应用(app)的创建,并实际完成一个简单应用的配置和开发。
767 0
成功解决除去或展开pandas.core.frame.DataFrame输出类型中所包含的省略号(列数据或者行数据显示不完全)
成功解决除去或展开pandas.core.frame.DataFrame输出类型中所包含的省略号(列数据或者行数据显示不完全)
成功解决除去或展开pandas.core.frame.DataFrame输出类型中所包含的省略号(列数据或者行数据显示不完全)
|
资源调度 JavaScript 前端开发
sentry 服务的搭建(下)
sentry 服务的搭建(下)
|
数据采集 城市大脑 运维
战略地图|“云上数字政府”与背后的科技
“云上数字政府”是当前整合数字技术和服务,加快治理能力现代化的一种最佳实践形态。在政府政务服务、社会治理、城市区域服务、非常态应急治理等方面,“云上”与“数字”科技的结合将进一步为数字政府建设提速。
1351 0
战略地图|“云上数字政府”与背后的科技
|
设计模式 测试技术 API
干货 | 通用 api 封装实战,带你深入理解 PO
干货 | 通用 api 封装实战,带你深入理解 PO