From 18f5d07d764d288297341878fbc0b579a5eae261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E4=BA=8C=E7=8C=9B?= Date: Tue, 19 Nov 2019 14:23:12 +0800 Subject: [PATCH] U both add host categories --- spug_api/apps/host/views.py | 3 ++- spug_web/src/pages/host/Form.js | 42 +++++++++++++++++++++++++++++--- spug_web/src/pages/host/Table.js | 3 +++ spug_web/src/pages/host/index.js | 16 +++++++++--- spug_web/src/pages/host/store.js | 8 ++++-- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/spug_api/apps/host/views.py b/spug_api/apps/host/views.py index dd8e181..483b722 100644 --- a/spug_api/apps/host/views.py +++ b/spug_api/apps/host/views.py @@ -9,7 +9,8 @@ from libs import human_time class HostView(View): def get(self, request): hosts = Host.objects.filter(deleted_by_id__isnull=True) - return json_response(hosts) + zones = [x['zone'] for x in hosts.order_by('zone').values('zone').distinct()] + return json_response({'zones': zones, 'hosts': [x.to_dict() for x in hosts]}) def post(self, request): form, error = JsonParser( diff --git a/spug_web/src/pages/host/Form.js b/spug_web/src/pages/host/Form.js index 9252775..e5815c9 100644 --- a/spug_web/src/pages/host/Form.js +++ b/spug_web/src/pages/host/Form.js @@ -1,14 +1,17 @@ import React from 'react'; -import { Modal, Form, Input, message } from 'antd'; +import { observer } from 'mobx-react'; +import { Modal, Form, Input, Select, Col, Button, message } from 'antd'; import http from 'libs/http'; import store from './store'; +@observer class ComForm extends React.Component { constructor(props) { super(props); this.state = { loading: false, password: null, + zone: null, } } @@ -56,6 +59,28 @@ class ComForm extends React.Component { ) }; + handleAddZone = () => { + Modal.confirm({ + icon: 'exclamation-circle', + title: '添加主机类别', + content: this.addZoneForm, + onOk: () => { + if (this.state.zone) { + store.zones.push(this.state.zone); + this.props.form.setFieldsValue({'zone': this.state.zone}) + } + }, + }) + }; + + addZoneForm = ( +
+ + this.setState({zone: val.target.value})}/> + +
+ ); + render() { const info = store.record; const {getFieldDecorator} = this.props.form; @@ -79,9 +104,18 @@ class ComForm extends React.Component { onOk={this.handleSubmit}>
- {getFieldDecorator('zone', {initialValue: info['zone']})( - - )} + + {getFieldDecorator('zone', {initialValue: info['zone']})( + + )} + + + + {getFieldDecorator('name', {initialValue: info['name']})( diff --git a/spug_web/src/pages/host/Table.js b/spug_web/src/pages/host/Table.js index 3596f1d..81d7c5b 100644 --- a/spug_web/src/pages/host/Table.js +++ b/spug_web/src/pages/host/Table.js @@ -61,6 +61,9 @@ class ComTable extends React.Component { if (store.f_name) { data = data.filter(item => item['name'].toLowerCase().includes(store.f_name.toLowerCase())) } + if (store.f_zone) { + data = data.filter(item => item['zone'].toLowerCase().includes(store.f_zone.toLowerCase())) + } return ( diff --git a/spug_web/src/pages/host/index.js b/spug_web/src/pages/host/index.js index f458f8e..b88ae14 100644 --- a/spug_web/src/pages/host/index.js +++ b/spug_web/src/pages/host/index.js @@ -1,15 +1,23 @@ import React from 'react'; -import { Card, Input, Button } from 'antd'; +import { observer } from 'mobx-react'; +import { Card, Input, Button, Select } from 'antd'; import { SearchForm } from 'components'; import ComTable from './Table'; import store from './store'; -export default function () { +export default observer(function () { return ( + + + - store.f_name = e.target.value} placeholder="请输入"/> + store.f_name = e.target.value} placeholder="请输入"/> @@ -21,4 +29,4 @@ export default function () { ) -} \ No newline at end of file +}) \ No newline at end of file diff --git a/spug_web/src/pages/host/store.js b/spug_web/src/pages/host/store.js index 83299a3..f2c05bf 100644 --- a/spug_web/src/pages/host/store.js +++ b/spug_web/src/pages/host/store.js @@ -3,17 +3,21 @@ import http from 'libs/http'; class Store { @observable records = []; + @observable zones = []; @observable record = {}; @observable isFetching = false; @observable formVisible = false; @observable f_name; - @observable f_status; + @observable f_zone; fetchRecords = () => { this.isFetching = true; http.get('/api/host/') - .then(res => this.records = res) + .then(({hosts, zones}) => { + this.records = hosts; + this.zones = zones; + }) .finally(() => this.isFetching = false) };