nutrient.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. :default-first-option="true"
  12. style="width: 200px;margin-left: 10px;"
  13. placeholder="请输入营养素关键词"
  14. :remote-method="queryNutrients"
  15. :loading="loading"
  16. >
  17. <el-option v-for="item in nutrients" :key="item.id" :label="item.name" :value="item.id" />
  18. </el-select>
  19. 营养素计量:
  20. <el-input
  21. v-model="params.quantity"
  22. style="width: 80px;"
  23. class="filter-item"
  24. />
  25. <el-autocomplete
  26. class="inline-input"
  27. v-model="params.unit"
  28. :fetch-suggestions="queryUnits"
  29. placeholder="单位关键词"
  30. />
  31. Nv_Spec计量:
  32. <el-input
  33. v-model="params.nvSpec"
  34. style="width: 80px;"
  35. class="filter-item"
  36. />
  37. <el-autocomplete
  38. class="inline-input"
  39. v-model="params.nvSpecUnit"
  40. :fetch-suggestions="queryUnits"
  41. placeholder="单位关键词"
  42. />
  43. <el-button
  44. class="filter-item"
  45. style="margin-left: 10px;"
  46. type="primary"
  47. @click="addNutrient">
  48. 添加
  49. </el-button>
  50. </div>
  51. <el-table
  52. :key="tableKey"
  53. v-loading="listLoading"
  54. :data="list"
  55. border
  56. fit
  57. highlight-current-row
  58. style="width: 60%;margin-top: 10px"
  59. >
  60. <el-table-column type="index" label="序号" align="center" width="60px" />
  61. <el-table-column label="营养素名称" align="center">
  62. <template slot-scope="{row}">
  63. <span>{{ row.nutrientName }}</span>
  64. </template>
  65. </el-table-column>
  66. <el-table-column label="营养素计量" align="center" width="80">
  67. <template slot-scope="{row}">
  68. <template v-if="row.edit">
  69. <el-input v-model="row.quantity" class="filter-item" />
  70. </template>
  71. <span v-else>{{ row.quantity }}</span>
  72. </template>
  73. </el-table-column>
  74. <el-table-column label="计量单位" align="center" width="150">
  75. <template slot-scope="{row}">
  76. <template v-if="row.edit">
  77. <el-autocomplete v-model="row.unit" :fetch-suggestions="queryUnits" placeholder="单位关键词" />
  78. </template>
  79. <span v-else>{{ row.unit }}</span>
  80. </template>
  81. </el-table-column>
  82. <el-table-column label="Nv_Spec" align="center" width="80">
  83. <template slot-scope="{row}">
  84. <template v-if="row.edit">
  85. <el-input v-model="row.nvSpec" class="filter-item" />
  86. </template>
  87. <span v-else>{{ row.nvSpec }}</span>
  88. </template>
  89. </el-table-column>
  90. <el-table-column label="计量单位" align="center" width="150">
  91. <template slot-scope="{row}">
  92. <template v-if="row.edit">
  93. <el-autocomplete v-model="row.nvSpecUnit" :fetch-suggestions="queryUnits" placeholder="单位关键词" />
  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" width="320" fixed="right">
  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="fetchData"
  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 size="mini" type="primary" @click="updateSort(row, 0)">上移</el-button>
  119. <el-button size="mini" type="primary" @click="updateSort(row, 1)">下移</el-button>
  120. <el-button size="mini" type="danger" @click="removeNutrient(row)">
  121. 删除
  122. </el-button>
  123. </template>
  124. </template>
  125. </el-table-column>
  126. </el-table>
  127. </div>
  128. </template>
  129. <script>
  130. import { getNutrientList, addFoodNutrient, updateFoodNutrient, removeFoodNutrient,
  131. updateFoodNutrientSort } from '@/api/food'
  132. import { getList } from '@/api/nutrient'
  133. import { getList as getUnits } from '@/api/unit'
  134. export default {
  135. name: 'AddNutrient',
  136. created() {
  137. this.foodId = this.$route.params && this.$route.params.id
  138. this.fetchData()
  139. this.queryNutrients()
  140. },
  141. mounted() {
  142. this.$nextTick(() => {
  143. this.$refs.nutrientSelect.focus()
  144. })
  145. },
  146. data() {
  147. return {
  148. tableKey: 0,
  149. listLoading: false,
  150. units: [],
  151. foodId: '',
  152. list: [],
  153. nutrients: [],
  154. params: {},
  155. loading: false,
  156. unitLoading: false
  157. }
  158. },
  159. methods: {
  160. fetchData() {
  161. this.listLoading = true
  162. getNutrientList(this.foodId).then(res => {
  163. this.list = res.data
  164. this.listLoading = false
  165. if (this.list.length > 0) {
  166. this.$set(this.params, "nvSpec", this.list[0].nvSpec)
  167. this.$set(this.params, "nvSpecUnit", this.list[0].nvSpecUnit)
  168. } else {
  169. this.$set(this.params, 'nvSpec', null)
  170. this.$set(this.params, 'nvSpecUnit', null)
  171. }
  172. })
  173. },
  174. addNutrient() {
  175. this.params.foodId = this.foodId
  176. addFoodNutrient(this.foodId, this.params).then(res => {
  177. this.params = {}
  178. this.fetchData()
  179. this.$notify.success('添加营养素成功')
  180. this.$refs.nutrientSelect.focus()
  181. }).catch(res => {
  182. this.$message.error(res.data.message)
  183. })
  184. },
  185. confirmEdit(row) {
  186. updateFoodNutrient(this.foodId, row.nutrientId, row).then(res => {
  187. this.fetchData()
  188. this.$notify.success('提交成功')
  189. }).catch(res => {
  190. this.$message.error(res.data.message)
  191. })
  192. },
  193. removeNutrient(row) {
  194. removeFoodNutrient(row.foodId, row.nutrientId).then(res => {
  195. this.fetchData()
  196. this.$notify.success('删除营养素成功')
  197. this.$refs.nutrientSelect.focus()
  198. }).catch(res => {
  199. this.$message.error(res.data.message)
  200. })
  201. },
  202. queryNutrients(query) {
  203. getList({ query }).then(res => {
  204. this.nutrients = res.data.list
  205. }).catch(() => {
  206. this.nutrients = []
  207. })
  208. },
  209. queryUnits(query, cb) {
  210. let units = []
  211. getUnits({ query }).then(res => {
  212. res.data.list.forEach(item => units.push({ value: item.name }))
  213. cb(units)
  214. })
  215. },
  216. updateSort(row, type) {
  217. updateFoodNutrientSort(this.foodId, row.nutrientId, { type }).then(res => {
  218. this.$notify.success('提交成功')
  219. this.fetchData()
  220. }).catch(res => {
  221. this.$message.error(res.data.message)
  222. })
  223. }
  224. }
  225. }
  226. </script>
  227. <style scoped>
  228. </style>