vue_table.j2 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="app-container">
  3. <el-form :inline="true" :model="param" class="demo-form-inline">
  4. {% for table in table_objs -%}
  5. {% if table['search'] == True -%}
  6. <el-form-item label="{{ table['label'] }}">
  7. <el-input v-model="param.{{ table['prop'] }}" placeholder="{{ table['label'] }}"/>
  8. </el-form-item>
  9. {% endif -%}
  10. {% endfor -%}
  11. <el-form-item>
  12. <el-button type="primary" @click="queryList">查询</el-button>
  13. </el-form-item>
  14. </el-form>
  15. <template>
  16. <!--添加-->
  17. <el-row>
  18. <el-button @click="createOne()">添加</el-button>
  19. </el-row>
  20. <!--数据-->
  21. <el-table
  22. :data="tableData"
  23. border
  24. style="width: 100%"
  25. >
  26. {% for table in table_objs -%}
  27. <el-table-column
  28. prop="{{ table['prop'] }}"
  29. label="{{ table['label'] }}"
  30. width="{{ table['width'] }}"
  31. />
  32. {% endfor -%}
  33. <el-table-column
  34. fixed="right"
  35. label="操作"
  36. width="100"
  37. >
  38. <template slot-scope="scope">
  39. <el-button
  40. type="text"
  41. size="small"
  42. @click="editOne(scope.row)"
  43. >编辑
  44. </el-button>
  45. <el-button
  46. size="small"
  47. type="text"
  48. @click="deleteAlert(scope.row.id)"
  49. >删除
  50. </el-button>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. <!--编辑层-->
  55. <el-dialog title="编辑" :visible.sync="dialogFormVisible">
  56. <el-form :model="editNews">
  57. {% for table in table_objs -%}
  58. {% if table['edit'] == True -%}
  59. <el-form-item label="{{ table['label'] }}" :label-width="formLabelWidth">
  60. <el-input v-model="editNews.{{ table['prop'] }}" placeholder="{{ table['label'] }}"/>
  61. </el-form-item>
  62. {% endif -%}
  63. {% endfor -%}
  64. </el-form>
  65. <div slot="footer" class="dialog-footer">
  66. <el-button @click="dialogFormVisible = false">取 消</el-button>
  67. <el-button type="primary" @click="saveOne">确 定</el-button>
  68. </div>
  69. </el-dialog>
  70. <!--删除提示-->
  71. <el-dialog
  72. title="提示"
  73. :visible.sync="dialogVisible"
  74. width="30%"
  75. :before-close="handleClose"
  76. >
  77. <span>真的要删吗?</span>
  78. <div slot="footer" class="dialog-footer">
  79. <el-button @click="dialogVisible = false">取 消</el-button>
  80. <el-button type="primary" @click="deleteOne">确 定</el-button>
  81. </div>
  82. </el-dialog>
  83. <!--分页-->
  84. <div class="block">
  85. <el-pagination
  86. :current-page="param.page"
  87. :page-sizes="[10, 20, 30, 40]"
  88. :page-size="param.size"
  89. layout="total, sizes, prev, pager, next, jumper"
  90. :total="total"
  91. @size-change="handleSizeChange"
  92. @current-change="handleCurrentChange"
  93. />
  94. </div>
  95. </template>
  96. </div>
  97. </template>
  98. <script>
  99. import {deleteOne, fetchList, fetchOne, saveOne} from '@/api/{{ table_name }}'
  100. export default {
  101. name: "{{ table_name.capitalize() }}",
  102. data() {
  103. return {
  104. tableData: [],
  105. // 删除层隐藏
  106. dialogVisible: false,
  107. // 弹出层隐藏
  108. dialogFormVisible: false,
  109. // 弹出层内容
  110. editNews: {
  111. {% for table in table_objs -%}
  112. {% if table['edit'] == True -%}
  113. {{ table['prop'] }}: "",
  114. {% endif -%}
  115. {% endfor -%}
  116. },
  117. // 弹出层大小
  118. formLabelWidth: '120px',
  119. // 删除使用的id
  120. id: '',
  121. // 查询参数
  122. param: {
  123. {% for table in table_objs -%}
  124. {% if table['search'] == True -%}
  125. {{ table['prop'] }}: "",
  126. {% endif -%}
  127. {% endfor -%}
  128. page: 1,
  129. size: 10
  130. },
  131. // 查询返回总条数
  132. total: 0
  133. }
  134. },
  135. mounted() {
  136. this.queryList()
  137. },
  138. methods: {
  139. // 修改页面展示数据大小
  140. handleSizeChange(val) {
  141. this.param.size = val
  142. this.queryList()
  143. },
  144. // 修改页码
  145. handleCurrentChange(val) {
  146. this.param.page = val
  147. this.queryList()
  148. },
  149. // 点击编辑,从后台获取数据
  150. editOne(row) {
  151. console.log(row)
  152. fetchOne(row.id).then(res => {
  153. if (res.flag) {
  154. this.editNews = res.data
  155. }
  156. })
  157. this.dialogFormVisible = true
  158. },
  159. // 获取删除行的id
  160. deleteAlert(id) {
  161. this.id = id
  162. this.dialogVisible = true
  163. },
  164. // 删除
  165. deleteOne() {
  166. deleteOne(this.id).then(res => {
  167. if (res.flag) {
  168. this.dialogFormVisible = false
  169. this.queryList()
  170. } else {
  171. alert('删除失败')
  172. }
  173. })
  174. this.dialogVisible = false
  175. },
  176. // 关闭提示框时,提示是否要关闭
  177. handleClose(done) {
  178. this.$confirm('确认关闭?')
  179. .then(() => {
  180. done()
  181. })
  182. .catch(() => {
  183. })
  184. },
  185. // 点击添加按钮清空
  186. createOne() {
  187. this.editNews = {}
  188. this.dialogFormVisible = true
  189. },
  190. // 点击确定后发送请求
  191. saveOne() {
  192. saveOne(this.editNews).then(res => {
  193. if (res.flag) {
  194. this.dialogFormVisible = false
  195. this.queryList()
  196. } else {
  197. this.$message('添加失败')
  198. }
  199. })
  200. },
  201. // 获取数据
  202. queryList() {
  203. fetchList(this.param).then(res => {
  204. if (res.flag) {
  205. this.tableData = res.data.list
  206. this.total = res.data.total
  207. } else {
  208. this.tableData = {}
  209. }
  210. })
  211. }
  212. }
  213. }
  214. </script>