• SPA项目之主页面--数据表格的增删改查


    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    接下来看看由辉辉所写的关于Vue+ElementUI的相关操作吧

    目录

    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    一.增删改查

    1.样式准备

    2.编码

    ①增

    代码

    效果

    ②删

    代码

    效果

    ③改

    代码

    效果

    ④查

    代码

    效果

    二.表单验证

    1.在表单中使用验证规则

    2.定义验证规则

    3..触发表单验证


    一.增删改查

    1.样式准备

    1. <style>
    2. style>

    2.编码

    ①增

    代码

    1. myshow() {
    2. this.dialogFormVisible = false;
    3. this.title = '新增窗体';
    4. this.book = {
    5. id: '',
    6. bookname: '',
    7. price: '',
    8. booktype: ''
    9. }
    10. },
    11. open(index, row) {
    12. this.dialogFormVisible = true;
    13. if (row) {
    14. this.title = '编辑窗体';
    15. this.book.id = row.id,
    16. this.book.bookname = row.bookname,
    17. this.book.price = row.price,
    18. this.book.booktype = row.booktype
    19. }
    20. },
    21. sure() {
    22. /* 表单验证 */
    23. this.$refs['book'].validate((valid) => {
    24. if (valid) {
    25. let url = this.axios.urls.BOOK_ADD;
    26. if (this.title == '编辑窗体') {
    27. url = this.axios.urls.BOOK_UPD;
    28. }
    29. let params = {
    30. id: this.book.id,
    31. bookname: this.book.bookname,
    32. price: this.book.price,
    33. booktype: this.book.booktype
    34. }
    35. this.axios.post(url, params).then(r => {
    36. console.log(r);
    37. this.myshow();
    38. this.select({});
    39. }).catch(e => {
    40. })
    41. } else {
    42. console.log('error submit!!');
    43. return false;
    44. }
    45. });
    46. },
    效果

    ②删

    代码
    1. del(idx, row) {
    2. this.$confirm('此操作将永久删除该行数据, 是否继续??', '提示', {
    3. confirmButtonText: '确定',
    4. cancelButtonText: '取消',
    5. type: 'warning'
    6. }).then(() => {
    7. let url = this.axios.urls.BOOK_DEL;
    8. this.axios.post(url, {id:row.id}).then(r => {
    9. console.log(r);
    10. this.$message({
    11. type: 'success',
    12. message: '删除成功!'
    13. });
    14. this.select({});
    15. }).catch(e => {
    16. })
    17. }).catch(() => {
    18. this.$message({
    19. type: 'info',
    20. message: '已取消删除'
    21. });
    22. });
    23. },
    效果

    ③改

    代码
    1. myshow() {
    2. this.dialogFormVisible = false;
    3. this.title = '新增窗体';
    4. this.book = {
    5. id: '',
    6. bookname: '',
    7. price: '',
    8. booktype: ''
    9. }
    10. },
    11. open(index, row) {
    12. this.dialogFormVisible = true;
    13. if (row) {
    14. this.title = '编辑窗体';
    15. this.book.id = row.id,
    16. this.book.bookname = row.bookname,
    17. this.book.price = row.price,
    18. this.book.booktype = row.booktype
    19. }
    20. },
    21. sure() {
    22. /* 表单验证 */
    23. this.$refs['book'].validate((valid) => {
    24. if (valid) {
    25. let url = this.axios.urls.BOOK_ADD;
    26. if (this.title == '编辑窗体') {
    27. url = this.axios.urls.BOOK_UPD;
    28. }
    29. let params = {
    30. id: this.book.id,
    31. bookname: this.book.bookname,
    32. price: this.book.price,
    33. booktype: this.book.booktype
    34. }
    35. this.axios.post(url, params).then(r => {
    36. console.log(r);
    37. this.myshow();
    38. this.select({});
    39. }).catch(e => {
    40. })
    41. } else {
    42. console.log('error submit!!');
    43. return false;
    44. }
    45. });
    46. },
    效果

    ④查

    代码
    1. select(params) {
    2. let url = this.axios.urls.BOOK_LIST;
    3. this.axios.get(url, {
    4. params: params
    5. }).then(r => {
    6. console.log(r);
    7. this.tableData = r.data.rows;
    8. this.total = r.data.total;
    9. }).catch(e => {
    10. })
    11. },
    12. onSubmit() {
    13. let params = {
    14. bookname: this.bookname
    15. }
    16. this.select(params)
    17. },
    18. handleSizeChange(num) { //当前所展示的数据条数值发生变化时所触发的事件
    19. console.log("展示的条数是" + num)
    20. let params = {
    21. bookname: this.bookname,
    22. rows: num,
    23. page: this.page
    24. }
    25. this.select(params)
    26. },
    27. handleCurrentChange(p) { //当前所展示的页码发生变化
    28. console.log("当前是第" + p + "页")
    29. let params = {
    30. bookname: this.bookname,
    31. rows: this.rows,
    32. page: p
    33. }
    34. this.select(params)
    35. },
    效果

    所有代码

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. bookname: '',
    6. tableData: [],
    7. rows: 10,
    8. page: 1,
    9. total: 0,
    10. title: '',
    11. formLabelWidth: '100px',
    12. dialogFormVisible: false,
    13. types: [],
    14. book: {
    15. id: '',
    16. bookname: '',
    17. price: '',
    18. booktype: ''
    19. },
    20. rules: {
    21. bookname: [{
    22. required: true,
    23. message: '请输入书籍名称',
    24. trigger: 'blur'
    25. }],
    26. price: [{
    27. required: true,
    28. message: '请输入书籍价格',
    29. trigger: 'blur'
    30. }],
    31. booktype: [{
    32. required: true,
    33. message: '请选择书籍类型',
    34. trigger: 'blur'
    35. }]
    36. },
    37. }
    38. },
    39. methods: {
    40. select(params) {
    41. let url = this.axios.urls.BOOK_LIST;
    42. this.axios.get(url, {
    43. params: params
    44. }).then(r => {
    45. console.log(r);
    46. this.tableData = r.data.rows;
    47. this.total = r.data.total;
    48. }).catch(e => {
    49. })
    50. },
    51. onSubmit() {
    52. let params = {
    53. bookname: this.bookname
    54. }
    55. this.select(params)
    56. },
    57. handleSizeChange(num) { //当前所展示的数据条数值发生变化时所触发的事件
    58. console.log("展示的条数是" + num)
    59. let params = {
    60. bookname: this.bookname,
    61. rows: num,
    62. page: this.page
    63. }
    64. this.select(params)
    65. },
    66. handleCurrentChange(p) { //当前所展示的页码发生变化
    67. console.log("当前是第" + p + "页")
    68. let params = {
    69. bookname: this.bookname,
    70. rows: this.rows,
    71. page: p
    72. }
    73. this.select(params)
    74. },
    75. myshow() {
    76. this.dialogFormVisible = false;
    77. this.title = '新增窗体';
    78. this.book = {
    79. id: '',
    80. bookname: '',
    81. price: '',
    82. booktype: ''
    83. }
    84. },
    85. open(index, row) {
    86. this.dialogFormVisible = true;
    87. if (row) {
    88. this.title = '编辑窗体';
    89. this.book.id = row.id,
    90. this.book.bookname = row.bookname,
    91. this.book.price = row.price,
    92. this.book.booktype = row.booktype
    93. }
    94. },
    95. sure() {
    96. /* 表单验证 */
    97. this.$refs['book'].validate((valid) => {
    98. if (valid) {
    99. let url = this.axios.urls.BOOK_ADD;
    100. if (this.title == '编辑窗体') {
    101. url = this.axios.urls.BOOK_UPD;
    102. }
    103. let params = {
    104. id: this.book.id,
    105. bookname: this.book.bookname,
    106. price: this.book.price,
    107. booktype: this.book.booktype
    108. }
    109. this.axios.post(url, params).then(r => {
    110. console.log(r);
    111. this.myshow();
    112. this.select({});
    113. }).catch(e => {
    114. })
    115. } else {
    116. console.log('error submit!!');
    117. return false;
    118. }
    119. });
    120. },
    121. del(idx, row) {
    122. this.$confirm('此操作将永久删除该行数据, 是否继续??', '提示', {
    123. confirmButtonText: '确定',
    124. cancelButtonText: '取消',
    125. type: 'warning'
    126. }).then(() => {
    127. let url = this.axios.urls.BOOK_DEL;
    128. this.axios.post(url, {id:row.id}).then(r => {
    129. console.log(r);
    130. this.$message({
    131. type: 'success',
    132. message: '删除成功!'
    133. });
    134. this.select({});
    135. }).catch(e => {
    136. })
    137. }).catch(() => {
    138. this.$message({
    139. type: 'info',
    140. message: '已取消删除'
    141. });
    142. });
    143. },
    144. },
    145. created() {
    146. this.select({})
    147. this.types = [{
    148. id: 1,
    149. name: "爽文"
    150. }, {
    151. id: 2,
    152. name: "爱情"
    153. }, {
    154. id: 3,
    155. name: "谍战"
    156. }, {
    157. id: 4,
    158. name: "古装"
    159. }]
    160. }
    161. }
    162. script>
    163. <style>
    164. style>

    url配置

    1. /**
    2. * 对后台请求的地址的封装,URL格式如下:
    3. * 模块名_实体名_操作
    4. */
    5. export default {
    6. 'SERVER': 'http://localhost:8080', //服务器
    7. 'SYSTEM_USER_DOLOGIN': '/user/userLogin', //登陆
    8. 'SYSTEM_USER_DOREG': '/user/userRegister', //注册
    9. 'SYSTEM_MENU_TREE': '/module/queryRootNode', //动态树
    10. 'BOOK_LIST': '/book/queryBookPager', //书籍列表
    11. 'BOOK_ADD': '/book/addBook', //书籍增加
    12. 'BOOK_UPD': '/book/editBook', //书籍修改
    13. 'BOOK_DEL': '/book/delBook', //书籍删除
    14. 'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
    15. return this.SERVER + this[k];
    16. }
    17. }

    二.表单验证

    1.在表单中使用验证规则

    1. <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="myshow()">
    2. <el-form :model="book" :rules="rules" ref="book">
    3. <el-form-item label="书籍名称" :label-width="formLabelWidth" prop="bookname">
    4. <el-input v-model="book.bookname" autocomplete="off">el-input>
    5. el-form-item>
    6. <el-form-item label="书籍价格" :label-width="formLabelWidth" prop="price">
    7. <el-input v-model="book.price" autocomplete="off">el-input>
    8. el-form-item>
    9. <el-form-item label="书籍类型" :label-width="formLabelWidth" prop="booktype">
    10. <el-select v-model="book.booktype" placeholder="请选择书籍类型">
    11. <el-option v-for="t in types" :label="t.name" :value="t.name" :key="'key'+t.id">el-option>
    12. el-select>
    13. el-form-item>
    14. el-form>
    15. <div slot="footer" class="dialog-footer">
    16. <el-button @click="dialogFormVisible = false">取 消el-button>
    17. <el-button type="primary" @click="sure">确 定el-button>
    18. div>
    19. el-dialog>

    2.定义验证规则

    1. rules: {
    2. bookname: [{
    3. required: true,
    4. message: '请输入书籍名称',
    5. trigger: 'blur'
    6. }],
    7. price: [{
    8. required: true,
    9. message: '请输入书籍价格',
    10. trigger: 'blur'
    11. }],
    12. booktype: [{
    13. required: true,
    14. message: '请选择书籍类型',
    15. trigger: 'blur'
    16. }]
    17. },

    3..触发表单验证

    1. sure() {
    2. /* 表单验证 */
    3. this.$refs['book'].validate((valid) => {
    4. if (valid) {
    5. let url = this.axios.urls.BOOK_ADD;
    6. if (this.title == '编辑窗体') {
    7. url = this.axios.urls.BOOK_UPD;
    8. }
    9. let params = {
    10. id: this.book.id,
    11. bookname: this.book.bookname,
    12. price: this.book.price,
    13. booktype: this.book.booktype
    14. }
    15. this.axios.post(url, params).then(r => {
    16. console.log(r);
    17. this.myshow();
    18. this.select({});
    19. }).catch(e => {
    20. })
    21. } else {
    22. console.log('error submit!!');
    23. return false;
    24. }
    25. });
    26. },

    好啦,今天的分享就到这了,希望能够帮到你呢!😊😊 

  • 相关阅读:
    ewebeditor编辑器漏洞
    用CSS的@property 定义变量
    高级算法复习
    Kotlin中的委托、属性委托和延迟加载
    机器学习-sklearn-高斯混合模型-学习笔记
    Java 大神面试经验
    【Python学习】—Python基础语法(五)
    iOS 16.2 的7个惊人变化
    Spring 事务编程实践
    全局后置路由守卫(afterEach)
  • 原文地址:https://blog.csdn.net/m0_74315688/article/details/133343457