审查视图

src/views/nav1/Table.vue 12.8 KB
朱兆平 authored
1 2 3 4 5 6
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
7
					<el-input v-model="filters.realname" placeholder="姓名"></el-input>
朱兆平 authored
8 9 10
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.username" placeholder="账号"></el-input>
朱兆平 authored
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
朱兆平 authored
27 28 29 30
			<el-table-column prop="userId" label="ID" width="100" sortable>
			</el-table-column>
			<el-table-column prop="username" label="账号" width="120" sortable>
			</el-table-column>
31
			<el-table-column prop="realname" label="姓名" width="120" sortable>
朱兆平 authored
32 33 34
			</el-table-column>
			<el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
			</el-table-column>
朱兆平 authored
35
			<el-table-column prop="mobilephone" label="电话" width="125" sortable>
朱兆平 authored
36
			</el-table-column>
朱兆平 authored
37
			<el-table-column prop="creattime" label="创建时间" width="170" sortable>
朱兆平 authored
38
			</el-table-column>
朱兆平 authored
39 40 41
			<el-table-column prop="updatetime" label="更新时间" width="170" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" min-width="180" sortable>
朱兆平 authored
42
			</el-table-column>
43 44 45 46
			<el-table-column prop="email" label="Email" min-width="180" sortable>
			</el-table-column>
			<el-table-column prop="state" label="启用" width="100" :formatter="formatState" sortable>
			</el-table-column>
朱兆平 authored
47
			<el-table-column label="操作" width="150">
朱兆平 authored
48
				<template slot-scope="scope">
朱兆平 authored
49 50 51 52 53 54 55 56 57
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
shenhailong authored
58
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
朱兆平 authored
59 60 61 62
			</el-pagination>
		</el-col>

		<!--编辑界面-->
63
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
朱兆平 authored
64
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
朱兆平 authored
65 66 67
				<el-form-item label="ID">
					<span>{{editForm.userId}}</span>
				</el-form-item>
68 69
				<el-form-item label="账号">
					<span>{{editForm.username}}</span>
朱兆平 authored
70 71 72
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off" type="password"></el-input>
朱兆平 authored
73
				</el-form-item>
74 75 76 77 78 79 80 81 82
				<el-form-item label="姓名" prop="realname">
					<el-input v-model="editForm.realname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="mobilephone">
					<el-input v-model="editForm.mobilephone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="Email" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
朱兆平 authored
83 84
				<el-form-item label="性别">
					<el-radio-group v-model="editForm.sex">
朱兆平 authored
85 86
						<el-radio class="radio" label="1" >男</el-radio>
						<el-radio class="radio" label="0" >女</el-radio>
朱兆平 authored
87 88 89 90 91
					</el-radio-group>
				</el-form-item>
				<el-form-item label="年龄">
					<el-input-number v-model="editForm.age" :min="0" :max="200"></el-input-number>
				</el-form-item>
92 93 94
				<!--<el-form-item label="创建日期">-->
					<!--<el-date-picker type="date" placeholder="创建日期" v-model="editForm.creattime" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"></el-date-picker>-->
				<!--</el-form-item>-->
朱兆平 authored
95
				<el-form-item label="地址">
朱兆平 authored
96
					<el-input type="textarea" v-model="editForm.address"></el-input>
朱兆平 authored
97
				</el-form-item>
98 99 100 101 102 103
				<el-form-item label="启用">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" label="true" >是</el-radio>
						<el-radio class="radio" label="false" >否</el-radio>
					</el-radio-group>
				</el-form-item>
朱兆平 authored
104 105 106 107 108 109 110 111
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
112
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
朱兆平 authored
113
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
114 115 116 117 118 119 120 121 122 123 124 125 126 127
				<el-form-item label="账号" prop="username">
					<el-input v-model="addForm.username"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="addForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="realname">
					<el-input v-model="addForm.realname"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="mobilephone">
					<el-input v-model="addForm.mobilephone"></el-input>
				</el-form-item>
				<el-form-item label="Email" prop="email">
					<el-input v-model="addForm.email"></el-input>
朱兆平 authored
128 129 130
				</el-form-item>
				<el-form-item label="性别">
					<el-radio-group v-model="addForm.sex">
131 132
						<el-radio class="radio" label="1">男</el-radio>
						<el-radio class="radio" label="0">女</el-radio>
朱兆平 authored
133 134
					</el-radio-group>
				</el-form-item>
135 136
				<el-form-item label="年龄" prop="age">
				<el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
朱兆平 authored
137 138
				</el-form-item>
				<el-form-item label="地址">
139
					<el-input type="textarea" v-model="addForm.address"></el-input>
朱兆平 authored
140 141 142 143 144 145 146 147 148 149 150 151 152
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
153 154
	import { getUserList, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
    import moment from 'moment'
朱兆平 authored
155 156 157 158
	export default {
		data() {
			return {
				filters: {
159 160
                    username: '',
					realname: ''
朱兆平 authored
161 162 163
				},
				users: [],
				total: 0,
朱兆平 authored
164
                pageNum: 1,
165
                pageSize: 5,
朱兆平 authored
166 167
				listLoading: false,
				sels: [],//列表选中列
朱兆平 authored
168 169
				//编辑界面是否显示
				editFormVisible: false,
朱兆平 authored
170 171 172 173 174 175 176 177
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
朱兆平 authored
178 179 180 181 182
                    userId: 1,
                    username: '',
					password: '',
                    sex: 1,
                    creattime: '',
183 184 185 186 187
                    address: '',
					realname: '',
					email:	'',
					mobilephone: '',
					state: 'true'
朱兆平 authored
188 189 190 191 192
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
193
                    username: [
朱兆平 authored
194 195 196 197 198
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
199 200 201 202 203 204 205 206
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
					age: 1
朱兆平 authored
207 208 209 210 211 212 213 214 215
				}

			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
216 217 218
            formatState: function (row, column) {
                return row.state == true ? '是' : row.state == false ? '否' : '未知';
            },
朱兆平 authored
219
			handleCurrentChange(val) {
朱兆平 authored
220
				this.pageNum = val;
朱兆平 authored
221 222 223 224 225
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				let para = {
朱兆平 authored
226
                    pageNum: this.pageNum,
227 228
                    pageSize: this.pageSize,
                    username: this.filters.username
朱兆平 authored
229 230 231
				};
				this.listLoading = true;
				//NProgress.start();
232
                getUserList(para).then((res) => {
朱兆平 authored
233
					this.total = res.data.total;
朱兆平 authored
234
					this.users = res.data.list;
朱兆平 authored
235 236
					this.listLoading = false;
					//NProgress.done();
237 238 239 240 241
				}).catch((error) => {
                    this.listLoading = false;
                    let res= error.response.data;

                    alert(res.msg);
朱兆平 authored
242
				});
243
朱兆平 authored
244 245 246 247 248 249 250 251
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
252
					let para = { userId: row.userId };
朱兆平 authored
253 254 255 256 257 258 259 260
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
261 262 263
					}).catch((error) => {
                        this.listLoading = false;
                        alert(error);
朱兆平 authored
264
					});
265
				}).catch();
朱兆平 authored
266
			},
朱兆平 authored
267 268 269 270 271
            /**
			 * 显示编辑界面
             * @param index
             * @param row 为这行的数据对象
             */
朱兆平 authored
272 273 274 275
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
276
			//显示新增界面,每次点开初始化数据
朱兆平 authored
277 278 279
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
280 281 282 283 284 285 286 287
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
                    age: 1
朱兆平 authored
288 289 290 291 292 293 294 295 296 297
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
298 299 300 301 302 303 304 305 306 307 308 309 310 311
							//不需要提交的 去掉,后端不好接收
							para.authorities = null;
							para.permissions = null;
							para.roles = null;
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            /*
        					查询之后格式this.filters.column.create_start_date中日期发生变化;
        					Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 变成了 "2017-08-08T16:00:00.000Z";
        					所以使用 moment 日期格式化插件将时间转换成 [ Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) ] 格
        					式;
    						*/
							/*moment 安装 npm install moment --save*/
                            para.creattime = moment(para.creattime).format('YYYY-MM-DD HH:mm:ss');
                            this.editLoading = false;
朱兆平 authored
312
							editUser(para).then((res) => {
313
朱兆平 authored
314 315 316 317 318 319 320 321
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
322
							}).catch(error => alert(error));
朱兆平 authored
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							addUser(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getUsers();
346
							}).catch(error => alert(error));
朱兆平 authored
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>