import React from 'react';
import { Table, Button, Input, Spin, Message, Icon } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
const Permission = React.createClass({
getInitialState() {
return {
data: [],
selectedRowKeys: [],
selectedRows: [],
loading: false
}
},
componentDidMount() {
this.loadData();
},
loadData() {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/permissions',
method: 'get',
cache: false
}).done((res) => {
if (!res.error.length) {
this.setState({
data: res.data
})
}
}).always(() => {
this.setState({
loading: false
});
});
},
save() {
this.setState({
loading: true
});
$.ajax({
url: globalConfig.context + '/api/permission',
method: 'post',
data: {
data: JSON.stringify(this.state.data)
}
}).done((res) => {
if (!res.error.length) {
Message.success("保存成功");
this.loadData();
} else {
Message.error(res.error[0].message);
}
}).always(() => {
this.setState({
loading: false
});
})
},
remove() {
let deletedIds = [];
for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
let rowItem = this.state.selectedRows[idx];
if (rowItem.id) {
deletedIds.push(rowItem.id)
}
};
this.setState({
loading: deletedIds.length > 0
});
if (deletedIds.length) {
$.ajax({
url: globalConfig.context + '/api/permission/delete',
method: 'post',
data: {
data: JSON.stringify(deletedIds)
}
}).done((res) => {
if (!res.error.length) {
Message.success("删除成功");
this.state.selectedRowKeys.sort((a, b) => { return b - a });
for (let idx = 0; idx < this.state.selectedRowKeys.length; idx++) {
let dataIndex = this.state.selectedRowKeys[idx];
this.state.data.splice(dataIndex, 1);
};
this.setState({
data: this.state.data,
selectedRowKeys: []
});
} else {
Message.error(res.error[0].message);
}
}).always(() => {
this.setState({
loading: false
});
})
}
},
addNew() {
this.state.data.push({
id: null,
name: '',
url: ''
});
this.setState({
data: this.state.data
})
},
render() {
const columns = [{
title: '权限名字',
dataIndex: 'name',
key: 'name',
render: (text, record, index) => {
return { record.name = e.target.value; this.setState({ data: this.state.data }); }} />
}
}, {
title: '接口路径',
dataIndex: 'url',
key: 'url',
render: (text, record, index) => {
return { record.url = e.target.value; this.setState({ data: this.state.data }); }} />
}
}];
const rowSelection = {
type: 'checkbox',
selectedRowKeys: this.state.selectedRowKeys,
onChange: (selectedRowKeys, selectedRows) => {
this.setState({
selectedRows: selectedRows,
selectedRowKeys: selectedRowKeys
});
}
};
const hasSelected = this.state.selectedRowKeys.length > 0;
return (
);
}
});
export default Permission;