1、Element UI中表格數據的新增
- 在
Element UI
中,實現新增操做,須要彈出一個對話框,可使用Dialog
對話框組件。在data
中,咱們須要定義一個數據dialogVisible
,它的做用是控制添加用戶對話框的顯示與隱藏,默認爲隱藏,也就是不展現,代碼以下:
dialogVisible: false
複製代碼
- 在頁面當中,使用
Dialog
對話框組件,title
爲對話框的標題visible
爲是否顯示 Dialog
,支持 .sync
修飾符,是boolean
類型的值,默認值爲false
,咱們能夠給它綁定屬性dialogVisible
控制顯示與隱藏。width
爲Dialog
的寬度,能夠設置爲50%
,也就是佔據屏幕的百分之五十的位置。
<el-dialog title="添加用戶" :visible.sync="dialogVisible" width="50%">
<span>這是一段信息</span>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="dialogVisible = false">確 定</el-button>
</span>
</el-dialog>
複製代碼
- 在添加的按鈕當中,給它綁定一個事件,控制
dialogVisible
,默認爲false
隱藏,不展現。當點擊按鈕之後,值就變爲true
,展現,代碼以下:
<el-button type="primary" @click="dialogVisible = true">添加用戶</el-button>
複製代碼
- 對於數據的新增填寫,須要用到
Element UI
中的Form
表單組件,咱們能夠引用Form
表單。el-form
是表單的主體對象,el-form-item
是表單的每一項。在el-form
中,ref
是可以經過引用拿到表單的引用對象。model
是綁定表單的數據項,rules
是綁定表單的校驗規則,label-width
是每一項的寬度。在el-form-item
中,label
是標籤文本,prop
是傳入表單域的驗證規則。在el-input
中,v-model
是可以實現表單域的雙向數據綁定,代碼以下:
<el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="70px">
<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"></el-input>
</el-form-item>
<el-form-item label="郵箱" prop="email">
<el-input v-model="addForm.email"></el-input>
</el-form-item>
<el-form-item label="手機號" prop="mobile">
<el-input v-model="addForm.mobile"></el-input>
</el-form-item>
</el-form>
複製代碼
- 對於表單中的每一項填寫的內容值,經過雙向數據綁定得到。在以前的
el-form
中,:model="addForm"
是綁定表單的數據,也就是須要在data
中定義一個數據addForm
,進行綁定表單的全部數據。而對於表單中的每一項值,也就是在el-input
中,經過v-model
綁定addForm
中的每一項值,每個表單數據都須要在addForm
中去定義,這樣纔可以獲取到相應的數據,代碼以下:
addForm: {
username: '',
password: '',
email: '',
mobile: ''
}
複製代碼
- 對於表單的每一項,也須要進行規制驗證,判斷輸入框中輸入的值是不是正確的。在
el-form
中,:rules="addFormRules"
是綁定的表單驗證規則,也就是須要在data中定義一個數據addFormRules
,裏面包含了全部項的驗證規則,在裏面定義規則。對於表單的每一項值,拿到相應對於的規則,須要經過prop
屬性,所接受的值就是在addFormRules
中定義的規則名稱,required
表示爲必填項,min
和max
表示爲限定的最小長度和最大長度,validator
是綁定的自定義驗證規則,代碼以下:
addFormRules: {
username: [
{ required: true, message: '請輸入用戶名', trigger: 'blur' },
{ min: 3, max: 10, message: '長度在 3 到 10 個字符', trigger: 'blur' }
],
password: [
{ required: true, message: '請輸入密碼', trigger: 'blur' },
{ min: 3, max: 10, message: '長度在 6 到 15 個字符', trigger: 'blur' }
],
email: [
{ required: true, message: '請輸入郵箱', trigger: 'blur' },
{ validator: checkEmail, trigger: 'blur' }
],
mobile: [
{ required: true, message: '請輸入手機號', trigger: 'blur' },
{ validator: checkMobile, trigger: 'blur' }
]
}
複製代碼
- 對於表單的自定義驗證規則,咱們能夠在
data
中定義校驗規則,它其實是一個函數。好比郵箱的驗證規則checkEmail
, var checkEmail = (rule, value, callback) => {}
,第一個值是規則,第二個值是傳入對象的值,也就是輸入框中輸入的值,第三個值是回調函數。在對象裏面寫驗證規則,好比能夠定義驗證的正則表達式,經過test()
方法判斷輸入的值是否符合驗證規則。若是符合,返回callback()
。若是不符合,那麼就經過new Error()
方法提示相應的錯誤信息。在addFormRules
中,經過validator
就能夠綁定相應的每一項驗證規則,經過規則名稱,代碼以下:
var checkEmail = (rule, value, callback) => {
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
if (regEmail.test(value)) {
return callback()
}
callback(new Error('請輸入合法的郵箱'))
}
var checkMobile = (rule, value, callback) => {
const regMobile = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
if (regMobile.test(value)) {
return callback()
}
callback(new Error('請輸入合法的手機號'))
}
複製代碼
- 在新增數據,對話框的表單中輸入數據之後,而後再關閉對話框,下一次再添加數據的時候,會發現以前填的數據還會保留,沒有被清空,而咱們所但願的是每一次打開對話框的時候,表單都是新的,沒有數據內容,須要重置。咱們須要在對話框中綁定一個重置的事件,經過
@close
綁定addDialogClosed
事件,進行重置表單,代碼以下:
<el-dialog title="添加用戶" :visible.sync="dialogVisible" width="50%" @close="addDialogClosed">
複製代碼
- 在
method
中,定義addDialogClosed
事件,經過以前給表單ref
獲取到的引用對象addFormRef
,而後再經過this.$refs.addFormRef
去獲取整個表單的數據,再經過resetFields()
就能夠清空表單數據,達到重置表單的效果了。在Form
表單組件中,在Form Methods
中,有resetFields
方法,它的做用是對整個表單進行重置,將全部字段值重置爲初始值並移除校驗結果,代碼以下:
addDialogClosed () {
this.$refs.addFormRef.resetFields()
}
複製代碼
- 對於新增數據的對話框,當在填寫完表格數據之後,點擊肯定按鈕,發送保存數據,關閉對話框,須要對錶格填寫的數據進行一次預校驗,判斷數據是否有誤。只有當填寫的數據是正確的,纔可以保存和新增數據。 咱們能夠如今肯定按鈕的位置,綁定一個點擊事件
addUser
,代碼以下:
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="addUser">確 定</el-button>
</span>
複製代碼
- 在
method
中,定義addUser
方法,經過this.$refs.addFormRef
能夠拿到整個表單的數據。在Form
表單組件中,Form Methods
中,有一個validate
方法,它的做用是對整個表單進行校驗的方法,參數爲一個回調函數。該回調函數會在校驗結束後被調用,並傳入兩個參數:是否校驗成功和未經過校驗的字段。若不傳入回調函數,則會返回一個 promise
。咱們能夠在拿到表單數據之後,經過validate
方法進行表單數據的預校驗,傳入valid
參數。若是valid
的值爲true
,說明預校驗成功,反之則校驗失敗,代碼以下:
addUser () {
this.$refs.addFormRef.validate((valid) => {
if (!valid) return
})
}
複製代碼
- 在校驗表單數據成功之後,就能夠發起添加用戶的網絡請求,調用相應的
api
接口發起添加請求。因爲返回值爲promise
對象,全部能夠用async
和await
進行簡化操做,定義返回數據爲res
。若是狀態碼不爲201
,說明新增失敗,反之則新增成功。在新增成功之後,將data
中定義的dialogVisible
的值從新設置爲false
,關閉新增的對話框,再調用getUserList()
方法,從新獲取用戶列表數據,代碼以下:
addUser () {
this.$refs.addFormRef.validate(async valid => {
if (!valid) return
const {data: res} = await this.$http.post('users', this.addForm)
if (res.meta.status !== 201) {
this.$message.error('添加用戶失敗!')
}
this.$message.success('添加用戶成功!')
this.dialogVisible = false
this.getUserList()
})
}
複製代碼
2、Element UI中表格數據的新增實現
- Element表格數據的新增,完整代碼以下:
<template>
<div>
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
<el-breadcrumb-item>用戶管理</el-breadcrumb-item>
<el-breadcrumb-item>用戶列表</el-breadcrumb-item>
</el-breadcrumb>
<el-card class="box-card">
<el-row :gutter="20">
<el-col :span="8">
<el-input placeholder="請輸入內容" v-model="queryInfo.query" clearable @clear="getUserList()">
<el-button slot="append" icon="el-icon-search" @click="getUserList()"></el-button>
</el-input>
</el-col>
<el-col :span="4">
<el-button type="primary" @click="dialogVisible=true">添加用戶</el-button>
</el-col>
</el-row>
<el-table :data="userList" :stripe="true" :border="true">
<el-table-column type="index"></el-table-column>
<el-table-column prop="username" label="用戶名稱"></el-table-column>
<el-table-column prop="email" label="郵箱"></el-table-column>
<el-table-column prop="mobile" label="電話"></el-table-column>
<el-table-column prop="role_name" label="角色"></el-table-column>
<el-table-column prop="mg_state" label="狀態">
<template slot-scope="scope">
<el-switch v-model="scope.row.mg_state" @change="userStateChange(scope.row)">
</el-switch>
</template>
</el-table-column>
<el-table-column label="操做">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
<el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
<el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
<el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
</el-tooltip>
</template>
</el-table-column>
</el-table>
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
</el-pagination>
</el-card>
<el-dialog title="添加用戶" :visible.sync="dialogVisible" width="50%" @close="addDialogClosed">
<el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="70px">
<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"></el-input>
</el-form-item>
<el-form-item label="郵箱" prop="email">
<el-input v-model="addForm.email"></el-input>
</el-form-item>
<el-form-item label="手機號" prop="mobile">
<el-input v-model="addForm.mobile"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="addUser">確 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script> export default { data () { var checkEmail = (rule, value, callback) => { const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/ if (regEmail.test(value)) { return callback() } callback(new Error('請輸入合法的郵箱')) } var checkMobile = (rule, value, callback) => { const regMobile = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/ if (regMobile.test(value)) { return callback() } callback(new Error('請輸入合法的手機號')) } return { queryInfo: { query: '', pagenum: 1, pagesize: 2 }, userList: [], total: 0, dialogVisible: false, addForm: { username: '', password: '', email: '', mobile: '' }, addFormRules: { username: [ { required: true, message: '請輸入用戶名', trigger: 'blur' }, { min: 3, max: 10, message: '長度在 3 到 10 個字符', trigger: 'blur' } ], password: [ { required: true, message: '請輸入密碼', trigger: 'blur' }, { min: 3, max: 10, message: '長度在 6 到 15 個字符', trigger: 'blur' } ], email: [ { required: true, message: '請輸入郵箱', trigger: 'blur' }, { validator: checkEmail, trigger: 'blur' } ], mobile: [ { required: true, message: '請輸入手機號', trigger: 'blur' }, { validator: checkMobile, trigger: 'blur' } ] } } }, created () { this.getUserList() }, methods: { async getUserList () { const {data: res} = await this.$http.get('users', { params: this.queryInfo }) if (res.meta.status !== 200) { return this.$message('獲取用戶列表失敗!') } this.userList = res.data.users this.total = res.data.total console.log(res) }, handleSizeChange (newSize) { this.queryInfo.pagesize = newSize this.getUserList() }, handleCurrentChange (newPage) { this.queryInfo.pagenum = newPage this.getUserList() }, async userStateChange (userInfo) { console.log(userInfo) const {data: res} = await this.$http.put(`users/${userInfo.id}/state/${userInfo.mg_state}`) if (res.meta.status !== 200) { this.userInfo.mg_state = !this.userInfo.mg_state this.$message.error('更新用戶狀態失敗!') } this.$message.success('更新用戶狀態成功!') }, addDialogClosed () { this.$refs.addFormRef.resetFields() }, addUser () { this.$refs.addFormRef.validate(async valid => { if (!valid) return const {data: res} = await this.$http.post('users', this.addForm) if (res.meta.status !== 201) { this.$message.error('添加用戶失敗!') } this.$message.success('添加用戶成功!') this.dialogVisible = false this.getUserList() }) } } } </script>
<style lang="less" scoped> </style>
複製代碼