detail.vue 8.0 KB

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