Nav1.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div>
  3. <el-button type="primary" @click="addEdit">增加</el-button>
  4. <el-table
  5. :data="tableData"
  6. style="width: 100%">
  7. <el-table-column
  8. label="id">
  9. <template slot-scope="scope">
  10. <span style="margin-left: 10px">{{ scope.row.id }}</span>
  11. </template>
  12. </el-table-column>
  13. <el-table-column
  14. label="姓名">
  15. <template slot-scope="scope">
  16. <span style="margin-left: 10px">{{ scope.row.name }}</span>
  17. </template>
  18. </el-table-column>
  19. <el-table-column label="操作">
  20. <template slot-scope="scope">
  21. <el-button
  22. size="mini"
  23. @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
  24. <el-button
  25. size="mini"
  26. type="danger"
  27. @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  28. </template>
  29. </el-table-column>
  30. </el-table>
  31. <el-dialog
  32. :title="title"
  33. :visible.sync="dialogVisible"
  34. width="50%"
  35. center>
  36. <el-form :model="form" ref="ruleForm">
  37. <el-form-item label="姓名" prop="name" :label-width="formLabelWidth" :rules="{required: true, message: '姓名不能为空'}">
  38. <el-input v-model="form.name" autocomplete="off"></el-input>
  39. </el-form-item>
  40. </el-form>
  41. <span slot="footer" class="dialog-footer">
  42. <el-button @click="dialogVisible = false">取 消</el-button>
  43. <el-button type="primary" @click="setTable('ruleForm')">确 定</el-button>
  44. </span>
  45. </el-dialog>
  46. </div>
  47. </template>
  48. <script>
  49. import { companyList, setCompanyList, addCompanyList } from '../../api/nav1.js';
  50. import { mapGetters } from 'vuex';
  51. export default {
  52. data() {
  53. return {
  54. tableData: [],
  55. dialogVisible: false,
  56. form: {
  57. name: '',
  58. status:'DEL'
  59. },
  60. formLabelWidth: '120px',
  61. setIndex: '',
  62. title: '',
  63. type: ''
  64. }
  65. },
  66. methods: {
  67. handleEdit(index, row) {
  68. this.dialogVisible = true;
  69. this.setIndex = index;
  70. this.form.id = row.id;
  71. this.title = '编辑';
  72. this.type = 'set';
  73. console.log(index, row);
  74. },
  75. handleDelete(index, row) {
  76. console.log(index, row);
  77. },
  78. setTable(formName) {
  79. this.$refs[formName].validate((valid) => {
  80. if (valid) {
  81. console.log(this.form)
  82. const form = this.form;
  83. if(this.type == 'add') {
  84. addCompanyList(form).then(res => {
  85. companyList().then(res => {
  86. if(res.code == 200) {
  87. this.tableData = res.data;
  88. }
  89. })
  90. })
  91. }else {
  92. const index = this.setIndex;
  93. setCompanyList(form).then(res => {
  94. this.tableData[index].name = form.name;
  95. });
  96. }
  97. this.dialogVisible = false;
  98. } else {
  99. console.log('error submit!!');
  100. return false;
  101. }
  102. });
  103. },
  104. addEdit() {
  105. this.dialogVisible = true;
  106. this.title = '增加';
  107. this.type = 'add';
  108. }
  109. },
  110. created () {
  111. companyList().then(res => {
  112. if(res.code == 200) {
  113. this.tableData = res.data;
  114. }
  115. })
  116. }
  117. }
  118. </script>