StdDataEntry.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <a-form :layout="layout" class="std-data-entry">
  3. <a-form-item
  4. v-for="d in M_dataList" :key="d.dataIndex" :help="error[d.dataIndex] ? error[d.dataIndex].toString() : null"
  5. :label="d.title"
  6. :labelCol="d.edit.labelCol"
  7. :validate-status="error[d.dataIndex] ? 'error' :'success'"
  8. :wrapperCol="d.edit.wrapperCol"
  9. >
  10. <p v-if="d.description" v-html="d.description+'<br/>'"/>
  11. <a-input
  12. v-if="d.edit.type==='input'"
  13. v-model="dataSource[d.dataIndex]"
  14. :placeholder="getInputPlaceholder(d, dataSource)"
  15. />
  16. <a-textarea v-else-if="d.edit.type==='textarea'" v-model="dataSource[d.dataIndex]"
  17. :rows="d.edit.row?d.edit.row:5"/>
  18. <std-select-option
  19. v-else-if="d.edit.type==='select'"
  20. v-model="temp[d.dataIndex]"
  21. :options="d.mask"
  22. :key-type="d.edit.key_type ? d.edit.key_type : 'int'"
  23. style="min-width: 120px"
  24. />
  25. <std-check-tag
  26. v-else-if="d.edit.type==='check-tag'"
  27. v-model="temp[d.dataIndex]"
  28. :options="d.mask"
  29. />
  30. <std-multi-check-tag
  31. v-else-if="d.edit.type==='multi-check-tag'"
  32. v-model="temp[d.dataIndex]"
  33. :data-object="temp"
  34. :options="d.mask"
  35. />
  36. <std-selector
  37. v-else-if="d.edit.type==='selector'" v-model="temp[d.dataIndex]" :api="d.edit.api"
  38. :columns="d.edit.columns"
  39. :data_key="d.edit.data_key"
  40. :disable_search="d.edit.disable_search" :pagination_method="d.edit.pagination_method"
  41. :record-value-index="d.edit.recordValueIndex" :value="fn(temp, d.edit.valueIndex)"
  42. :get_params="get_params_fn(d)"
  43. :description="d.edit.description"
  44. selection-type="radio"
  45. />
  46. <a-input-number v-else-if="d.edit.type==='number'" v-model="temp[d.dataIndex]"
  47. :min="d.edit.min" :step="d.edit.step" :max="d.edit.max"
  48. />
  49. <std-upload v-else-if="d.edit.type==='upload'" :id="temp.id?temp.id:null" :ref="'std_upload_'+d.dataIndex"
  50. v-model="temp[d.dataIndex]" :api="d.edit.api"
  51. :api_delete="d.edit.api_delete"
  52. :list="temp[d.dataIndex]"
  53. :crop="d.edit.crop"
  54. :auto-upload="d.edit.auto_upload"
  55. :crop-options="d.edit.cropOptions" :type="d.edit.upload_type ? d.edit.upload_type : 'img'"
  56. @uploaded="url => {$emit('uploaded', url)}"
  57. />
  58. <std-date-picker v-else-if="d.edit.type==='date_picker'" v-model="temp[d.dataIndex]"
  59. :show-time="d.edit.showTime"/>
  60. <a-slider
  61. v-else-if="d.edit.type==='slider'"
  62. v-model="temp[d.dataIndex]"
  63. :marks="d.mask"
  64. :max="d.edit.max"
  65. :min="d.edit.min"
  66. />
  67. <a-switch
  68. v-else-if="d.edit.type==='switch'"
  69. v-model="temp[d.dataIndex]"
  70. />
  71. <a-checkbox
  72. v-else-if="d.edit.type==='checkbox'"
  73. v-model="temp[d.dataIndex]"
  74. >
  75. {{ d.text }}
  76. </a-checkbox>
  77. <std-check-group
  78. v-else-if="d.edit.type==='check-group'"
  79. v-model="temp[d.dataIndex]"
  80. :options="d.options"
  81. :allow-other="d.edit.allow_other"
  82. />
  83. <std-radio-group
  84. v-else-if="d.edit.type==='radio-group'"
  85. v-model="temp[d.dataIndex]"
  86. :options="d.options"
  87. :key-type="d.edit.key_type"
  88. />
  89. <std-transfer
  90. v-else-if="d.edit.type==='transfer'"
  91. v-model="temp[d.dataIndex]"
  92. :api="d.edit.api"
  93. :data-key="d.edit.dataKey"
  94. />
  95. <p v-else-if="d.edit.type==='readonly'">
  96. {{ d.mask ? d.mask[fn(temp, d.dataIndex)] : fn(temp, d.dataIndex) }}
  97. </p>
  98. <p v-else>{{ 'edit.type 参数非法 ' + d.edit.type }}</p>
  99. <p v-if="!dataSource[d.dataIndex] && d.empty_description" v-html="d.empty_description"/>
  100. </a-form-item>
  101. <a-form-item v-if="$slots.supplement||$slots.action">
  102. <slot name="supplement"/>
  103. <slot name="action"/>
  104. </a-form-item>
  105. </a-form>
  106. </template>
  107. <script>
  108. import StdSelectOption from './StdSelectOption'
  109. import StdSelector from './StdSelector'
  110. import StdUpload from './StdUpload'
  111. import StdDatePicker from './StdDatePicker'
  112. import StdTransfer from './StdTransfer'
  113. import StdCheckTag from '@/components/StdDataEntry/StdCheckTag'
  114. import StdMultiCheckTag from '@/components/StdDataEntry/StdMultiCheckTag'
  115. import StdCheckGroup from '@/components/StdDataEntry/StdCheckGroup'
  116. import StdRadioGroup from '@/components/StdDataEntry/StdRadioGroup'
  117. export default {
  118. name: 'StdDataEntry',
  119. components: {
  120. StdRadioGroup,
  121. StdCheckGroup,
  122. StdMultiCheckTag,
  123. StdCheckTag,
  124. StdTransfer,
  125. StdDatePicker,
  126. StdSelectOption,
  127. StdSelector,
  128. StdUpload
  129. },
  130. props: {
  131. dataList: [Array, Object],
  132. dataSource: Object,
  133. error: {
  134. type: Object,
  135. default() {
  136. return {}
  137. }
  138. },
  139. layout: {
  140. default: 'vertical',
  141. validator: value => {
  142. return ['horizontal', 'vertical', 'inline'].indexOf(value) !== -1
  143. }
  144. }
  145. },
  146. model: {
  147. prop: 'dataSource',
  148. event: 'changeDataSource'
  149. },
  150. data() {
  151. return {
  152. temp: null,
  153. i: 0,
  154. M_dataList: {}
  155. }
  156. },
  157. watch: {
  158. dataSource() {
  159. this.temp = this.dataSource ?? []
  160. },
  161. dataList() {
  162. this.M_dataList = this.editableColumns(this.dataList ?? [])
  163. }
  164. },
  165. created() {
  166. this.temp = this.dataSource ?? []
  167. if (this.layout === 'horizontal') {
  168. this.labelCol = {span: 4}
  169. this.wrapperCol = {span: 18}
  170. }
  171. this.M_dataList = this.editableColumns(this.dataList)
  172. },
  173. methods: {
  174. get_params_fn(d) {
  175. return {...d.edit.get_params, ...this.bindModel(d.edit.bind, this.temp)}
  176. },
  177. fn: (obj, desc) => {
  178. const arr = desc.split('.')
  179. while (arr.length) {
  180. const top = obj[arr.shift()]
  181. if (top === undefined) {
  182. return null
  183. }
  184. obj = top
  185. }
  186. return obj
  187. },
  188. editableColumns(columns) {
  189. if (typeof columns === 'object') {
  190. columns = Object.values(columns)
  191. }
  192. return columns.filter((c) => {
  193. return c.edit
  194. })
  195. },
  196. bindModel(bind, dataSource) {
  197. let object = {}
  198. if (bind) {
  199. for (const [key, value] of Object.entries(bind)) {
  200. object[key] = this.fn(dataSource, value)
  201. }
  202. }
  203. return object
  204. },
  205. getInputPlaceholder(d, dataSource) {
  206. // edit 模式
  207. if (dataSource.id) {
  208. return d.edit.placeholder?.edit ?? d.edit.placeholder
  209. } else {
  210. // add 模式
  211. return d.edit.placeholder?.add ?? d.edit.placeholder
  212. }
  213. }
  214. }
  215. }
  216. </script>
  217. <style scoped>
  218. </style>