detail.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. 营养素:
  5. <el-select
  6. ref="nutrientSelect"
  7. v-model="params.nutrientId"
  8. filterable
  9. remote
  10. reserve-keyword
  11. style="width: 200px;margin-left: 10px;"
  12. placeholder="请输入营养素关键词"
  13. :remote-method="queryNutrients"
  14. :loading="loading"
  15. >
  16. <el-option v-for="item in nutrients" :key="item.id" :label="item.name" :value="item.id" />
  17. </el-select>
  18. 营养素计量单位:
  19. <el-autocomplete
  20. class="inline-input"
  21. v-model="params.unit"
  22. :fetch-suggestions="queryUnits"
  23. placeholder="单位关键词"
  24. />
  25. Nv_Spec计量:
  26. <el-input
  27. v-model="params.nvSpec"
  28. style="width: 80px;"
  29. class="filter-item"
  30. />
  31. Nv_Spec计量单位:
  32. <el-autocomplete
  33. class="inline-input"
  34. v-model="params.nvSpecUnit"
  35. :fetch-suggestions="queryUnits"
  36. placeholder="单位关键词"
  37. />
  38. <el-button
  39. class="filter-item"
  40. style="margin-left: 10px;"
  41. type="primary"
  42. @click="addNutrient"
  43. >
  44. 添加
  45. </el-button>
  46. <el-table
  47. :key="tableKey"
  48. v-loading="listLoading"
  49. :data="list"
  50. border
  51. fit
  52. highlight-current-row
  53. style="width: 60%;margin-top: 10px"
  54. >
  55. <el-table-column type="index" label="序号" align="center" width="60px" />
  56. <el-table-column label="营养素名称" align="center">
  57. <template slot-scope="{row}">
  58. <span>{{ row.nutrientName }}</span>
  59. </template>
  60. </el-table-column>
  61. <el-table-column label="营养素计量单位" align="center" width="150px">
  62. <template slot-scope="{row}">
  63. <template v-if="row.edit">
  64. <el-autocomplete
  65. class="inline-input"
  66. v-model="row.unit"
  67. :fetch-suggestions="queryUnits"
  68. placeholder="单位关键词"
  69. />
  70. </template>
  71. <span v-else>{{ row.unit }}</span>
  72. </template>
  73. </el-table-column>
  74. <el-table-column label="Nv_Spec" align="center" width="100px">
  75. <template slot-scope="{row}">
  76. <template v-if="row.edit">
  77. <el-input
  78. v-model="row.nvSpec"
  79. class="filter-item"
  80. />
  81. </template>
  82. <span v-else>{{ row.nvSpec }}</span>
  83. </template>
  84. </el-table-column>
  85. <el-table-column label="Nv_Spec单位" align="center" width="150px">
  86. <template slot-scope="{row}">
  87. <template v-if="row.edit">
  88. <el-autocomplete
  89. class="inline-input"
  90. v-model="row.nvSpecUnit"
  91. :fetch-suggestions="queryUnits"
  92. placeholder="单位关键词"
  93. />
  94. </template>
  95. <span v-else>{{ row.nvSpecUnit }}</span>
  96. </template>
  97. </el-table-column>
  98. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="320px">
  99. <template slot-scope="{row}">
  100. <template v-if="row.edit">
  101. <el-button
  102. type="success"
  103. size="mini"
  104. @click="confirmEdit(row)"
  105. >
  106. 提交
  107. </el-button>
  108. <el-button
  109. type="danger"
  110. size="mini"
  111. @click="fetchNutrients"
  112. >
  113. 取消
  114. </el-button>
  115. </template>
  116. <template v-else>
  117. <el-button size="mini" type="primary" @click="row.edit=true">编辑</el-button>
  118. <el-button type="primary" size="mini" @click="updateSort(row, 0)">
  119. 上移
  120. </el-button>
  121. <el-button type="primary" size="mini" @click="updateSort(row, 1)">
  122. 下移
  123. </el-button>
  124. <el-button size="mini" type="danger" @click="removeNutrient(row)">
  125. 删除
  126. </el-button>
  127. </template>
  128. </template>
  129. </el-table-column>
  130. </el-table>
  131. </div>
  132. </div>
  133. </template>
  134. <script>
  135. import { getList } from '@/api/nutrient'
  136. import { getList as getUnits } from '@/api/unit'
  137. import { getNutrients, addTemplateNutrient, updateTemplateNutrient, removeNutrient,
  138. updateTemplateNutrientSort } from '@/api/nutrientTemplate'
  139. export default {
  140. name: 'Detail',
  141. components: {},
  142. data() {
  143. return {
  144. templateId: '',
  145. tableKey: 0,
  146. listLoading: false,
  147. list: [],
  148. units: [],
  149. nutrients: [],
  150. params: {},
  151. loading: false,
  152. unitLoading: false
  153. }
  154. },
  155. created() {
  156. this.templateId = this.$route.params.id
  157. if (!this.templateId) {
  158. this.$message.error('获取数据失败')
  159. this.$router.push({ path: '/nutrient-template' })
  160. }
  161. this.fetchNutrients()
  162. this.queryNutrients()
  163. this.$nextTick(() => {
  164. this.$refs['nutrientSelect'].focus()
  165. })
  166. },
  167. methods: {
  168. addNutrient() {
  169. if (this.params.nutrientId) {
  170. addTemplateNutrient(this.templateId, this.params).then(res => {
  171. this.fetchNutrients()
  172. this.$notify.success('添加成功')
  173. this.params = {}
  174. this.$nextTick(() => {
  175. this.$refs['nutrientSelect'].focus()
  176. })
  177. }).catch(res => {
  178. this.$message.error(res.data.message)
  179. })
  180. } else {
  181. this.$message.error('未选择营养素')
  182. }
  183. },
  184. confirmEdit(row) {
  185. updateTemplateNutrient(this.templateId, row.nutrientId, row).then(res => {
  186. this.fetchNutrients()
  187. this.$notify.success('提交成功')
  188. }).catch(res => {
  189. this.$message.error(res.data.message)
  190. })
  191. },
  192. updateSort(row, type) {
  193. updateTemplateNutrientSort(this.templateId, row.nutrientId, { type }).then(res => {
  194. this.fetchNutrients()
  195. this.$notify.success('提交成功')
  196. }).catch(res => {
  197. this.fetchNutrients()
  198. this.$message.error(res.data.message)
  199. })
  200. },
  201. queryNutrients(query) {
  202. getList({ query }).then(res => {
  203. this.nutrients = res.data.list
  204. }).catch(() => {
  205. this.nutrients = []
  206. })
  207. },
  208. queryUnits(query, cb) {
  209. let units = []
  210. getUnits({ query }).then(res => {
  211. res.data.list.forEach(item => units.push({ value: item.name }))
  212. cb(units)
  213. })
  214. },
  215. fetchNutrients() {
  216. this.listLoading = true
  217. getNutrients(this.templateId).then(res => {
  218. this.list = res.data
  219. this.listLoading = false
  220. if (this.list.length > 0) {
  221. this.$set(this.params, "nvSpec", this.list[0].nvSpec)
  222. this.$set(this.params, "nvSpecUnit", this.list[0].nvSpecUnit)
  223. } else {
  224. this.$set(this.params, 'nvSpec', null)
  225. this.$set(this.params, 'nvSpecUnit', null)
  226. }
  227. }).catch(() => {
  228. this.$message.error('获取数据失败')
  229. this.listLoading = false
  230. this.$router.push({ path: '/nutrient-template' })
  231. })
  232. },
  233. removeNutrient(row) {
  234. removeNutrient(this.templateId, row.nutrientId).then(res => {
  235. this.fetchNutrients()
  236. this.$notify.success('提交成功')
  237. }).catch(res => {
  238. this.fetchNutrients()
  239. this.$message.error(res.data.message)
  240. })
  241. }
  242. }
  243. }
  244. </script>
  245. <style scoped>
  246. </style>