index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-button
  5. class="filter-item"
  6. style="margin: 0 10px 20px 0; float: right;"
  7. type="success"
  8. icon="el-icon-circle-plus-outline"
  9. @click="handleCreate"
  10. >
  11. 新建
  12. </el-button>
  13. </div>
  14. <el-table
  15. :key="0"
  16. v-loading="listLoading"
  17. :data="list"
  18. border
  19. fit
  20. highlight-current-row
  21. style="width: 100%;"
  22. @row-click="showDialog"
  23. >
  24. <el-table-column type="index" label="序号" align="center" fixed width="60px" />
  25. <el-table-column label="规格名称" fixed align="center">
  26. <template slot-scope="{row}">
  27. <span>{{ row.name }}</span>
  28. </template>
  29. </el-table-column>
  30. <el-table-column label="转换关系" align="center">
  31. <template slot-scope="{row}">
  32. <span v-if="row.quantity">{{ row.quantity + row.unit }}</span>
  33. <span v-else>无</span>
  34. </template>
  35. </el-table-column>
  36. <el-table-column label="推荐摄入量" align="center">
  37. <template slot-scope="{row}">
  38. <span>{{ row.inInit + row.inInitUnit }}</span>
  39. </template>
  40. </el-table-column>
  41. <el-table-column label="最小刻度" align="center" width="80px">
  42. <template slot-scope="{row}">
  43. <span>{{ row.minScale }}</span>
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="最大刻度" align="center" width="80px">
  47. <template slot-scope="{row}">
  48. <span>{{ row.maxScale }}</span>
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="卡尺每格刻度" align="center" width="120px">
  52. <template slot-scope="{row}">
  53. <span>{{ row.stepScale }}</span>
  54. </template>
  55. </el-table-column>
  56. <el-table-column label="创建时间" width="180px" align="center">
  57. <template slot-scope="{row}">
  58. <span>{{ row.createTime }}</span>
  59. </template>
  60. </el-table-column>
  61. <el-table-column label="更新时间" width="180px" align="center">
  62. <template slot-scope="{row}">
  63. <span>{{ row.updateTime }}</span>
  64. </template>
  65. </el-table-column>
  66. <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="320">
  67. <template slot-scope="{row}">
  68. <el-button type="primary" size="mini" @click="handleUpdate(row)">
  69. 更新
  70. </el-button>
  71. <el-button type="primary" size="mini" @click="updateSort(row, 0)">
  72. 上移
  73. </el-button>
  74. <el-button type="primary" size="mini" @click="updateSort(row, 1)">
  75. 下移
  76. </el-button>
  77. <el-button size="mini" type="danger" @click="handleDelete(row)">
  78. 删除
  79. </el-button>
  80. </template>
  81. </el-table-column>
  82. </el-table>
  83. <el-dialog title="营养素计算" :visible.sync="dialogFormVisible">
  84. <span v-html="dialogHtml" />
  85. </el-dialog>
  86. </div>
  87. </template>
  88. <script>
  89. import { getFoodModifiers, removeFoodModifier, updateFoodModifierSort, convertFoodUnit, getNutrientList,
  90. getDetail, calculateNutrientsContent } from '@/api/food'
  91. import { convertUnits } from '@/api/unit'
  92. export default {
  93. name: 'FoodModifierIndex',
  94. data() {
  95. return {
  96. listLoading: false,
  97. list: [],
  98. food: {},
  99. foodId: '',
  100. dialogHtml: '',
  101. dialogFormVisible: false
  102. }
  103. },
  104. created() {
  105. this.foodId = this.$route.params && this.$route.params.id
  106. this.fetchData()
  107. this.fetchFood()
  108. },
  109. methods: {
  110. handleCreate() {
  111. this.$router.push({ path: `/food/${this.foodId}/modifier/create` })
  112. },
  113. handleUpdate(row) {
  114. this.$router.push({ path: `/food/${this.foodId}/modifier/${row.id}/edit` })
  115. },
  116. handleDelete(row) {
  117. removeFoodModifier(this.foodId, row.id).then(res => {
  118. this.$notify.success('提交成功')
  119. this.fetchData()
  120. }).catch(res => {
  121. this.$message.error(res.data.message)
  122. this.fetchData()
  123. })
  124. },
  125. fetchFood() {
  126. getDetail(this.foodId).then(res => {
  127. this.food = res.data
  128. })
  129. },
  130. fetchData() {
  131. this.listLoading = true
  132. getFoodModifiers(this.foodId).then(res => {
  133. this.list = res.data.list
  134. this.listLoading = false
  135. }).catch(res => {
  136. this.$message.error('获取数据失败')
  137. this.listLoading = false
  138. })
  139. },
  140. updateSort(row, type) {
  141. updateFoodModifierSort(this.foodId, row.id, { type }).then(res => {
  142. this.$notify.success('提交成功')
  143. this.fetchData()
  144. }).catch(res => {
  145. this.$message.error(res.data.message)
  146. })
  147. },
  148. async showDialog(row, column, event) {
  149. calculateNutrientsContent(row.foodId, { unit: row.inInitUnit, quantity: row.inInit }).then(res => {
  150. this.dialogFormVisible = true
  151. this.dialogHtml = res.data
  152. }).catch(res => {
  153. this.$message.error(res.data.message)
  154. })
  155. }
  156. }
  157. }
  158. </script>
  159. <style scoped>
  160. </style>