index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-input
  5. v-model="listQuery.query"
  6. placeholder="请输入检索词"
  7. style="width: 60%;"
  8. class="filter-item"
  9. @keyup.enter.native="fetchData()"
  10. />
  11. <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="fetchData">
  12. 检索
  13. </el-button>
  14. <el-button
  15. class="filter-item"
  16. style="margin: 0 10px 20px 0; float: right;"
  17. type="success"
  18. icon="el-icon-circle-plus-outline"
  19. @click="handleCreateOrUpdate('create')"
  20. >
  21. 新建
  22. </el-button>
  23. </div>
  24. <el-table
  25. ref="table"
  26. :key="tableKey"
  27. v-loading="listLoading"
  28. :data="list"
  29. border
  30. fit
  31. highlight-current-row
  32. >
  33. <el-table-column type="index" label="序号" align="center" fixed width="60" />
  34. <el-table-column label="名称" align="center" fixed width="150">
  35. <template slot-scope="{row}">
  36. <span>{{ row.name }}</span>
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="描述" align="center" width="300">
  40. <template slot-scope="{row}">
  41. <span>{{ row.description }}</span>
  42. </template>
  43. </el-table-column>
  44. <el-table-column label="创建人" align="center" width="150">
  45. <template slot-scope="{row}">
  46. <span>{{ row.userName }}</span>
  47. </template>
  48. </el-table-column>
  49. <el-table-column label="创建时间" width="180px" align="center">
  50. <template slot-scope="{row}">
  51. <span>{{ row.createTime }}</span>
  52. </template>
  53. </el-table-column>
  54. <el-table-column label="更新时间" width="180px" align="center">
  55. <template slot-scope="{row}">
  56. <span>{{ row.updateTime }}</span>
  57. </template>
  58. </el-table-column>
  59. <el-table-column label="操作" align="center" fixed="right" width="90">
  60. <template slot-scope="{row}">
  61. <el-dropdown @command="handleCommand">
  62. <el-button size="small" type="primary" @click.stop="{}">
  63. 操作<i class="el-icon-arrow-down el-icon--right" />
  64. </el-button>
  65. <el-dropdown-menu slot="dropdown">
  66. <el-dropdown-item :command="{row: row, command: 'update'}">更新/查看</el-dropdown-item>
  67. <el-dropdown-item :command="{row: row, command: 'manage'}">管理营养素</el-dropdown-item>
  68. <el-dropdown-item :disabled="!canUpdate(row.userId)" :command="{row: row, command: 'moveUp'}">上移</el-dropdown-item>
  69. <el-dropdown-item :disabled="!canUpdate(row.userId)" :command="{row: row, command: 'moveDown'}">下移</el-dropdown-item>
  70. <el-dropdown-item :disabled="!canUpdate(row.userId)" :command="{row: row, command: 'moveTop'}">置顶</el-dropdown-item>
  71. <el-dropdown-item :disabled="!canUpdate(row.userId)" :command="{row: row, command: 'moveBottom'}">置底</el-dropdown-item>
  72. <el-dropdown-item :disabled="!canUpdate(row.userId)" :command="{row: row, command: 'delete'}">删除</el-dropdown-item>
  73. </el-dropdown-menu>
  74. </el-dropdown>
  75. </template>
  76. </el-table-column>
  77. </el-table>
  78. <pagination
  79. v-show="total>0"
  80. :total="total"
  81. :page.sync="listQuery.pageNum"
  82. :limit.sync="listQuery.pageSize"
  83. @pagination="fetchData"
  84. />
  85. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  86. <el-form ref="dataForm" :rules="rules" :model="params" label-position="left" label-width="80px" style="width: 400px; margin-left:50px;">
  87. <el-form-item label="名称" prop="name">
  88. <el-input v-model="params.name" placeholder="请输入名称" />
  89. </el-form-item>
  90. <el-form-item label="描述" prop="description">
  91. <el-input v-model="params.description" placeholder="请输入描述" type="textarea" :rows="2" />
  92. </el-form-item>
  93. </el-form>
  94. <div slot="footer" class="dialog-footer">
  95. <el-button @click="dialogFormVisible = false">
  96. 取消
  97. </el-button>
  98. <el-button type="primary" :disabled="dialogStatus === 'update' && !canUpdate(params.userId)" @click="createOrUpdateData">
  99. 提交
  100. </el-button>
  101. </div>
  102. </el-dialog>
  103. </div>
  104. </template>
  105. <script>
  106. import store from '@/store'
  107. import Pagination from '@/components/Pagination'
  108. import { getNutrientTemplates, createTemplate, updateSort, updateTemplate, removeTemplate } from '@/api/nutrientTemplate'
  109. export default {
  110. name: 'NutrientTemplateList',
  111. components: { Pagination },
  112. data() {
  113. return {
  114. tableKey: 0,
  115. listQuery: {
  116. query: '',
  117. pageNum: 1,
  118. pageSize: 20
  119. },
  120. listLoading: false,
  121. list: [],
  122. total: 0,
  123. textMap: {
  124. update: '更新',
  125. create: '新建'
  126. },
  127. dialogStatus: '',
  128. dialogFormVisible: false,
  129. rules: {},
  130. params: {}
  131. }
  132. },
  133. created() {
  134. this.fetchData()
  135. },
  136. methods: {
  137. fetchData() {
  138. this.listLoading = true
  139. getNutrientTemplates(this.listQuery).then(res => {
  140. this.list = res.data.list
  141. this.total = res.data.count
  142. this.listLoading = false
  143. }).catch(res => {
  144. this.$message.error('获取数据失败')
  145. this.listLoading = false
  146. })
  147. },
  148. handleCreateOrUpdate(type, row) {
  149. this.dialogStatus = type
  150. this.params = type === 'create' ? {} : Object.assign(row, {})
  151. this.dialogFormVisible = true
  152. },
  153. updateSort(row, type) {
  154. updateSort(row.id, { type }).then(res => {
  155. this.fetchData()
  156. this.$notify.success('提交成功')
  157. }).catch(res => {
  158. this.fetchData()
  159. this.$message.error(res.data.message)
  160. })
  161. },
  162. handleDelete(row) {
  163. removeTemplate(row.id).then(res => {
  164. this.fetchData()
  165. this.$notify.success('提交成功')
  166. }).catch(res => {
  167. this.fetchData()
  168. this.$message.error(res.data.message)
  169. })
  170. },
  171. createOrUpdateData() {
  172. const resultPromise = this.dialogStatus === 'create' ? createTemplate(this.params) : updateTemplate(this.params.id,
  173. this.params)
  174. resultPromise.then(res => {
  175. this.$notify.success('提交成功')
  176. this.fetchData()
  177. this.dialogFormVisible = false
  178. }).catch(res => {
  179. this.$message.error(res.data.message)
  180. this.fetchData()
  181. })
  182. },
  183. canUpdate(userId) {
  184. return store.getters.isAdmin || store.getters.userId === userId
  185. },
  186. manageNutrients(row) {
  187. this.$router.push({ path: `nutrient-template/${row.id}/detail` })
  188. },
  189. handleCommand(data) {
  190. switch (data.command) {
  191. case 'update':
  192. this.handleCreateOrUpdate('update', data.row)
  193. break
  194. case 'manage':
  195. this.manageNutrients(data.row)
  196. break
  197. case 'moveUp':
  198. this.updateSort(data.row, 0)
  199. break
  200. case 'moveDown':
  201. this.updateSort(data.row, 1)
  202. break
  203. case 'moveTop':
  204. this.updateSort(data.row, 2)
  205. break
  206. case 'moveBottom':
  207. this.updateSort(data.row, 3)
  208. break
  209. case 'delete':
  210. this.handleDelete(data.row)
  211. break
  212. }
  213. }
  214. }
  215. }
  216. </script>
  217. <style scoped>
  218. </style>