table-form-item.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <HcTableForm
  3. ref="tableFormRef"
  4. :cols="colsKeys"
  5. :form="tableFormInfo"
  6. :height="heights"
  7. :html="excelHtml"
  8. :loading="loading"
  9. :pid="activeKey"
  10. :pkey="keyId"
  11. :scroll="scroll"
  12. :width="widths"
  13. @excel-body-tap="excelTableFormClick"
  14. @render="tableFormRender"
  15. @right-tap="tableFormRightTap"
  16. />
  17. </template>
  18. <script setup>
  19. import { onMounted, ref, watch } from 'vue'
  20. import { useAppStore } from '~src/store'
  21. import wbsApi from '~api/data-fill/wbs'
  22. import { deepClone, getArrValue, getObjVal, isString } from 'js-fast-way'
  23. //初始
  24. const props = defineProps({
  25. tid: { // 树节点
  26. type: [String, Number],
  27. default: '',
  28. },
  29. kid: { // pkeyId
  30. type: [String, Number],
  31. default: '',
  32. },
  33. classify: { // 类型
  34. type: [String, Number],
  35. default: '',
  36. },
  37. scroll: {
  38. type: Boolean,
  39. default: true,
  40. },
  41. height: {
  42. type: String,
  43. default: '100%',
  44. },
  45. width: {
  46. type: String,
  47. default: 'auto',
  48. },
  49. datas: {
  50. type: Object,
  51. default: () => ({}),
  52. },
  53. nodeName: { // 表单名称
  54. type: String,
  55. default: '',
  56. },
  57. pid: { // 折叠ID
  58. type: String,
  59. default: '',
  60. },
  61. })
  62. //事件
  63. const emit = defineEmits(['rightTap', 'render', 'excelBodyTap'])
  64. //初始变量
  65. const useAppState = useAppStore()
  66. const projectId = ref(useAppState.getProjectId)
  67. const contractId = ref(useAppState.getContractId)
  68. const keyId = ref(props.kid ? props.kid + '' : '')
  69. const treeId = ref(props.tid)
  70. const classify = ref(props.classify)
  71. const loading = ref(false)
  72. const changeData = ref(props.datas)
  73. const nodeNames = ref(props.nodeName)
  74. const heights = ref(props.height)
  75. const widths = ref(props.width)
  76. const activeKey = ref(props.pid)
  77. const tableFormRef = ref(null)
  78. //监听
  79. watch(() => [
  80. useAppState.getProjectId,
  81. useAppState.getContractId,
  82. props.tid,
  83. props.kid,
  84. props.classify,
  85. props.nodeName,
  86. props.height,
  87. props.width,
  88. props.pid,
  89. ], ([project_id, contract_id, tree_id, key_id, cid, nodeName, height, width, pid]) => {
  90. projectId.value = project_id
  91. contractId.value = contract_id
  92. treeId.value = tree_id
  93. keyId.value = key_id ? key_id + '' : ''
  94. classify.value = cid
  95. nodeNames.value = nodeName
  96. heights.value = height
  97. widths.value = width
  98. activeKey.value = pid
  99. })
  100. //深度监听变动的对象数据
  101. watch(() => [
  102. props.datas,
  103. ], ([data]) => {
  104. changeData.value = data
  105. setFormChangeData(data)
  106. }, { deep: true })
  107. //渲染完成
  108. onMounted(async () => {
  109. loading.value = true
  110. //获取已填写的数据
  111. await getTableFormInfo(keyId.value)
  112. //按键key列表
  113. await getHtmlBussColsApi(keyId.value)
  114. //渲染表单
  115. await getExcelHtml(keyId.value)
  116. loading.value = false
  117. })
  118. //表单被点击
  119. const excelTableFormClick = (item) => {
  120. emit('excelBodyTap', item)
  121. }
  122. //获取表单初始数据
  123. const getFormDataInit = () => {
  124. return {
  125. projectId: projectId.value,
  126. contractId: contractId.value,
  127. classify: classify.value,
  128. pkeyId: keyId.value,
  129. nodeId: treeId.value,
  130. isRenderForm: false,
  131. }
  132. }
  133. //获取已填写的数据
  134. const tableFormInfo = ref({})
  135. const getTableFormInfo = async (pkeyId) => {
  136. if (pkeyId) {
  137. const { error, code, data } = await wbsApi.getBussDataInfo({
  138. pkeyId: pkeyId,
  139. nodeId:treeId.value,
  140. }, false)
  141. const resData = getObjVal(data)
  142. if (!error && code === 200 && resData) {
  143. tableFormInfo.value = {
  144. ...resData,
  145. ...getFormDataInit(),
  146. ...changeData.value,
  147. }
  148. } else {
  149. tableFormInfo.value = {
  150. ...getFormDataInit(),
  151. ...changeData.value,
  152. }
  153. }
  154. } else {
  155. tableFormInfo.value = {}
  156. window?.$message?.warning('pkeyId为空')
  157. }
  158. }
  159. //获取按键切换输入框的key列表
  160. const colsKeys = ref([])
  161. const getHtmlBussColsApi = async (pkeyId) => {
  162. if (pkeyId) {
  163. const { error, code, data } = await wbsApi.getHtmlBussCols({
  164. pkeyId: pkeyId,
  165. }, false)
  166. if (!error && code === 200) {
  167. let keys = getArrValue(data)
  168. for (let i = 0; i < keys.length; i++) {
  169. if (keys[i].length <= 0) {
  170. keys.splice(i, 1)
  171. }
  172. }
  173. colsKeys.value = keys
  174. } else {
  175. colsKeys.value = []
  176. }
  177. } else {
  178. colsKeys.value = []
  179. }
  180. }
  181. //获取模板标签数据
  182. const excelHtml = ref('')
  183. const getExcelHtml = async (pkeyId) => {
  184. if (pkeyId) {
  185. const { error, code, data } = await wbsApi.getExcelHtml({
  186. pkeyId: pkeyId,
  187. }, false)
  188. const resData = isString(data) ? data || '' : ''
  189. if (!error && code === 200 && resData) {
  190. excelHtml.value = resData
  191. } else {
  192. excelHtml.value = ''
  193. tableFormInfo.value.isRenderForm = false
  194. window?.$message?.warning('暂无表单')
  195. }
  196. } else {
  197. excelHtml.value = ''
  198. tableFormInfo.value.isRenderForm = false
  199. window?.$message?.warning('pkeyId为空')
  200. }
  201. }
  202. //渲染完成
  203. const tableFormRender = (form) => {
  204. tableFormInfo.value = form
  205. emit('render', form)
  206. }
  207. //右键点击
  208. const tableFormRightTap = (item) => {
  209. emit('rightTap', item)
  210. }
  211. //设置数据
  212. const setFormChangeData = (data) => {
  213. const form = deepClone(tableFormInfo.value)
  214. tableFormInfo.value = { ...form, ...data }
  215. //console.log('设置数据', {...form, ...data})
  216. }
  217. const getFormData = () => {
  218. return tableFormInfo.value
  219. //return tableFormRef.value?.getFormData()
  220. }
  221. const getCols = ()=>{
  222. return colsKeys.value
  223. }
  224. const setFormData = (data) => {
  225. setFormChangeData(data)
  226. tableFormRef.value?.setFormData(tableFormInfo.value)
  227. }
  228. const getRegExpJson = () => {
  229. return tableFormRef.value?.getRegExpJson()
  230. }
  231. const isFormRegExp = async () => {
  232. return await tableFormRef.value?.isFormRegExp()
  233. }
  234. //获取表单名称
  235. const getNodeName = () => {
  236. return nodeNames.value
  237. }
  238. //按下ctrl键
  239. const setIsCtrlKey = (data) => {
  240. tableFormRef.value?.setIsCtrlKey(data)
  241. }
  242. //按下复制快捷键
  243. const setCopyKeyList = (event) => {
  244. tableFormRef.value?.setCopyKeyList(event)
  245. }
  246. //按下粘贴快捷键
  247. const setPasteKeyList = async (event) => {
  248. await tableFormRef.value?.setPasteKeyList(event)
  249. }
  250. const getCetCopyKeyList = async (event)=>{
  251. let res = await tableFormRef.value?.getCetCopyKeyList(event)
  252. return res
  253. }
  254. const clearCheckKeyList = ()=>{
  255. tableFormRef.value?.clearCheckKeyList()
  256. }
  257. // 暴露出去
  258. defineExpose({
  259. getFormData,
  260. setFormData,
  261. getRegExpJson,
  262. isFormRegExp,
  263. getNodeName,
  264. setIsCtrlKey,
  265. setCopyKeyList,
  266. setPasteKeyList,
  267. getExcelHtml,
  268. getTableFormInfo,
  269. getHtmlBussColsApi,
  270. getCols,
  271. getCetCopyKeyList,
  272. clearCheckKeyList,
  273. })
  274. </script>