Skip to content

Commit

Permalink
优化异步表单代码
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkuang committed Oct 27, 2017
1 parent cd44c06 commit f341d08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/containers/AsycTables.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react';
import PropTypes from 'prop-types'
import { Row, Col, Card, Table } from 'antd';
import { getQueryString } from '../utils/tools';

const PERPAGE = 15;
const USERNAME = 'nelsonkuang';

class AsycTables extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -30,8 +34,7 @@ class AsycTables extends React.Component {
(async () => {
try {
const page = parseInt(matchUrl.split('/')[2], 10);
const LOGIN = 'nelsonkuang';
const res = await fetch(`https://api.github.com/users/${LOGIN}/starred?page=${page}`);
const res = await fetch(`https://api.github.com/users/${USERNAME}/starred?per_page=${PERPAGE}&page=${page}`);
const link = await res.headers.get('link');
const json = await res.json();
const dataSource = await json.map(item => {
Expand Down Expand Up @@ -89,19 +92,19 @@ class AsycTables extends React.Component {
let total = 0;
let str = link.split(',').find(s => s.indexOf('rel="last"') > -1);
if(str) {
total = str.split(';')[0].slice(1, -1).split('=')[1];
total = getQueryString(str.split(';')[0].slice(1, -1), 'page');
} else {
str = link.split(',').find(s => s.indexOf('rel="prev"') > -1);
if(str) {
total = str.split(';')[0].slice(1, -1).split('=')[1] * 1 + 1;
total = getQueryString(str.split(';')[0].slice(1, -1), 'page') * 1 + 1;
}
}

return {
total: total * 30,
total: total * PERPAGE,
current: currentPage * 1,
defaultPageSize: 30,
pageSize: 30, // github默认每页30条
defaultPageSize: PERPAGE,
pageSize: PERPAGE, // github默认每页30条
itemRender: (current, type, originalElement) => { // 修改上一页、下一页中文
if (type === 'prev') {
return <a>上一页</a>;
Expand All @@ -110,7 +113,7 @@ class AsycTables extends React.Component {
}
return originalElement;
},
onChange: (page, pageSize) => { // 分布跳转
onChange: (page, pageSize) => { // 分页跳转
const {match, history} = this.props;
history.push(match.path.replace(':pageid',page));
}
Expand Down
1 change: 0 additions & 1 deletion src/containers/Tables.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { Row, Col, Card, Table, Icon } from 'antd';


class Tables extends React.Component {
render() {
const dataSource1 = [{
Expand Down
5 changes: 5 additions & 0 deletions src/utils/tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getQueryString = (str, name) => {
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
const r = str.match(reg);
return r ? unescape(r[2]) : null;
};

0 comments on commit f341d08

Please sign in to comment.