edit-table.vue 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818
  1. <!-- @desc:编辑表格 @auth:周兴 @time:2022/11/15 13:06 -->
  2. <template>
  3. <div :style="{height: 100 + '%',width:divWidth,paddingRight:'3px'}">
  4. <div v-if="title" class="table-title">{{ title }}</div>
  5. <vxe-table
  6. border
  7. size="mini"
  8. show-overflow
  9. :cell-style="cellStyle"
  10. :height="height +'px'"
  11. show-footer
  12. align="center"
  13. auto-resize
  14. sync-resize
  15. :ref="name"
  16. :edit-rules="validRules"
  17. :valid-config="{showMessage:false}"
  18. :data="tableData"
  19. :show-header="showHeader"
  20. :show-footer="showFooter"
  21. :highlight-current-row="highlightCurrentRow"
  22. :footer-method="footerMethod"
  23. :mouse-config="{selected: true}"
  24. :checkbox-config="{range: true}"
  25. :column-config="{resizable: true}"
  26. :menu-config="{body:header,header:header,visibleMethod:visibleMethod}"
  27. :keyboard-config="{isArrow: true, isDel: true, isEnter: true, isTab: true, isEdit: true, editMethod}"
  28. :edit-config="{ mode: 'row',trigger:'click',autoClear:false, enabled:enabledEdit}"
  29. :row-config="{isCurrent: true}"
  30. @contextmenu.native.prevent
  31. @scroll="handleScroll"
  32. @keydown="handleTableKeyDown"
  33. @cell-mouseenter="handleCellMouseEnter"
  34. @cell-mouseleave="handleCellMouseLeave"
  35. @edit-closed="editClosed"
  36. @current-change="currentChangeGetData"
  37. @menu-click="menuClick"
  38. >
  39. <div v-for="(colItem,colIndex) in columnsData" :key="colIndex">
  40. <!-- 空白 (TODO第一列留一个余白,不然顺序就会乱,没有找到原因 周兴 2022-6-11) -->
  41. <vxe-column type="blank" v-if="colItem.type === 'blank'" :field="colItem.field" :width="colItem.width"
  42. :resizable="false">
  43. </vxe-column>
  44. <!--校验信息-->
  45. <vxe-column type="info" v-if="colItem.type === 'info'" :field="colItem.field" :width="colItem.width"
  46. :resizable="false">
  47. <template #default="{ row,rowIndex,column }">
  48. <Poptip trigger="hover" transfer placement="top-start" :content="row.info" v-if="row.info">
  49. <Icon type="ios-information-circle" style="cursor: pointer;font-size: 14px" color="red" ref="info-icon"/>
  50. </Poptip>
  51. </template>
  52. </vxe-column>
  53. <!-- 序列 -->
  54. <vxe-column v-if="colItem.type === 'seq'" type="seq" :field="colItem.field" width="60" :title="vm.$t('seq')">
  55. <template #default="{row, seq, rowIndex}">
  56. <div v-if="!row._hover" :class="row.parentId === null ?'seq-class':'seq-class-s'">
  57. {{ seq }}
  58. </div>
  59. <seq-button :delete-flag="deleteFlag && canDelete(row,colItem)" v-if="row._hover" :add-flag="addFlag"
  60. @add="add(rowIndex)" @del="del(rowIndex)"/>
  61. </template>
  62. </vxe-column>
  63. <!-- 选取类型 -->
  64. <vxe-column v-if="colItem.type === 'tableSelect'" :field="colItem.field"
  65. :type="colItem.type"
  66. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  67. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  68. :title="colItem.title?colItem.title:$t(colItem.field)" :params="{type:'tableSelect'}"
  69. :edit-render="{autofocus: '.vxe-input--inner'}">
  70. <template #header="{ row }">
  71. <span>{{ colItem.title ? colItem.title : $t(colItem.field) }}</span>
  72. <Poptip trigger="click" transfer @on-popper-hide="popperHide">
  73. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  74. v-if="colItem.copyVisible"/>
  75. <div slot="content">
  76. <div style="display: flex;align-items: center;justify-content: center;">
  77. <TableSelect :field="colItem.field" :dataType="colItem.dataType"
  78. :sortBoolean="colItem.sortBoolean"
  79. :multiple="colItem.multiple"
  80. :showType="colItem.showType"
  81. :promotionType="colItem.promotionType"
  82. @onBatchChoose="onBatchChooseCopy($event,colItem,'batch')"
  83. @onChoose="onBatchChooseCopy($event,colItem)"
  84. :search-param="typeof colItem.param === 'function'? colItem.param():colItem.param"></TableSelect>
  85. <Button type="primary" size="small" @click="inputDown(colItem)">{{ $t('filling') }}</Button>
  86. </div>
  87. </div>
  88. </Poptip>
  89. <!--冻结/解冻列-->
  90. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  91. v-if="freezeData && colItem.freezeFlag"
  92. type="ios-lock"
  93. @click="handleFreeze(colItem.field)"/>
  94. </template>
  95. <template style="width:100%;" #edit="{ row,column,rowIndex }">
  96. <TableSelect :text="row[colItem.field]" :field="colItem.field" :dataType="colItem.dataType"
  97. v-if="setCellCanTableSelect(row,colItem) "
  98. :dkEditTable="this"
  99. :sortBoolean="colItem.sortBoolean"
  100. :multiple="colItem.multiple"
  101. :showType="colItem.showType"
  102. :promotionType="colItem.promotionType"
  103. :search-param="typeof colItem.param === 'function'? colItem.param():colItem.param"
  104. @on-input-blur="handleSelectInputBlur($event,row,rowIndex,colItem,colIndex)"
  105. @change="(val)=>inputChange(row,rowIndex,colItem,val)"
  106. @keydown.stop="nextInputCellByKeyDown(Object.assign($event,{column:colItem,colIndex,rowIndex,row}))"
  107. @onBatchChoose="onBatchChoose($event,column,rowIndex,colIndex,colItem.fieldUpdate,colItem)"
  108. @onChoose="onChoose($event,row,column,rowIndex,colIndex,colItem.fieldUpdate,colItem)"
  109. :ref="colItem.field+rowIndex"></TableSelect>
  110. <div class="edit-div-class" v-else @dblclick.stop="copyValue(row,colItem,rowIndex)"
  111. style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;display: inline-block;width: 100%;">
  112. {{ row[colItem.field] }}
  113. </div>
  114. </template>
  115. </vxe-column>
  116. <!-- 无法修改类型 -->
  117. <vxe-column v-if="colItem.type === 'disabled' && !colItem.switch" :field="colItem.field"
  118. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  119. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  120. :formatter="formatColumn" :params="colItem"
  121. :title="colItem.title?colItem.title:$t(colItem.field)">
  122. <!--标题-->
  123. <template #header="{ row,column,columnIndex }">
  124. <span>{{ colItem.title ? colItem.title : $t(colItem.field) }}</span>
  125. <!--冻结/解冻列-->
  126. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  127. v-if="freezeData && colItem.freezeFlag"
  128. type="ios-lock"
  129. @click="handleFreeze(colItem.field)"/>
  130. </template>
  131. <template #default="{ row,rowIndex,column }">
  132. <div class="edit-div-class" @dblclick.stop="copyValue(row,colItem,rowIndex)">{{
  133. formatColumn({
  134. column: column,
  135. cellValue: row[colItem.field],
  136. row: row
  137. })
  138. }}
  139. </div>
  140. </template>
  141. </vxe-column>
  142. <!-- 无法修改类型 开关 -->
  143. <vxe-column v-if="colItem.type === 'disabled' && colItem.switch" :field="colItem.field"
  144. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  145. :min-width="colItem.width?colItem.width:vm.$config.columnWidthMin" :params="colItem"
  146. :title="colItem.title?colItem.title:$t(colItem.field)">
  147. <!--标题-->
  148. <template #header="{ row,column,columnIndex }">
  149. <span>{{ colItem.title ? colItem.title : $t(colItem.field) }}</span>
  150. <!--冻结/解冻列-->
  151. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  152. v-if="freezeData && colItem.freezeFlag"
  153. type="ios-lock"
  154. @click="handleFreeze(colItem.field)"/>
  155. </template>
  156. <template #default="{ row,rowIndex }">
  157. <i-switch size="small" :ref="colItem.field+'_default_'+rowIndex" v-model="row[colItem.field]"
  158. :disabled="true"/>
  159. </template>
  160. </vxe-column>
  161. <!-- 文本类型 -->
  162. <vxe-column v-if="colItem.type === 'text'" :field="colItem.field"
  163. :title="colItem.title?colItem.title:$t(colItem.field)"
  164. :type="colItem.type"
  165. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  166. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  167. :edit-render="{autofocus: '.vxe-input--inner'}">
  168. <template #header="{ row }">
  169. <span>{{ colItem.title ? colItem.title : $t(colItem.field) }}</span>
  170. <Poptip trigger="click" transfer @on-popper-hide="popperHide">
  171. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  172. v-if="colItem.copyVisible"/>
  173. <div slot="content">
  174. <div style="display: flex;align-items: center;justify-content: center;">
  175. <Input size="small" v-model="inputDownValue"
  176. :maxlength="colItem.maxlength?colItem.maxlength:vm.$config.maxlength"
  177. @keydown.13.native.stop="inputDown(colItem)" :name="colItem.field"></Input>
  178. <Button type="primary" size="small" @click="inputDown(colItem)">{{ $t('filling') }}</Button>
  179. </div>
  180. </div>
  181. </Poptip>
  182. <!--冻结/解冻列-->
  183. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  184. v-if="freezeData && colItem.freezeFlag"
  185. type="ios-lock"
  186. @click="handleFreeze(colItem.field)"/>
  187. </template>
  188. <template #edit="{ row,rowIndex }">
  189. <vxe-input v-model="row[colItem.field]" type="text" v-if="setCellCanInput( row ,colItem)"
  190. :maxlength="colItem.maxlength?colItem.maxlength:vm.$config.maxlength"
  191. :ref="colItem.field+rowIndex"
  192. @dblclick.native.stop="copyValue(row,colItem,rowIndex)"
  193. @change.native="changeValue(colItem,rowIndex,row)"
  194. @keydown.native="nextInputCellByKeyDown(Object.assign($event,{column:colItem,colIndex,rowIndex, row}))"></vxe-input>
  195. <div class="edit-div-class" @dblclick.stop="copyValue(row,colItem,rowIndex)" v-else>{{
  196. row[colItem.field]
  197. }}
  198. </div>
  199. </template>
  200. </vxe-column>
  201. <!-- 数值 -->
  202. <vxe-column v-if="colItem.type === 'number'" :field="colItem.field"
  203. :title="colItem.title?colItem.title:$t(colItem.field)"
  204. :type="colItem.type"
  205. class-name="number-class"
  206. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  207. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  208. :edit-render="{autofocus: '.vxe-input--inner', autoselect: true}" :formatter="formatColumn"
  209. :params="colItem">
  210. <template #header="{ row }">
  211. <span>{{ colItem.title ? colItem.title : $t(colItem.field) }}</span>
  212. <Poptip trigger="click" transfer @on-popper-show="popperShow(colIndex,'inputNumber')"
  213. @on-popper-hide="popperHide">
  214. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  215. v-if="colItem.copyVisible"/>
  216. <div slot="content">
  217. <!--点回车相当于点按钮填充-->
  218. <div style="display: flex;align-items: center;justify-content: center;">
  219. <InputNumberPop size="small" :digits="colItem.digits" v-model="inputDownValue"
  220. :ref="'pop_' + colIndex"
  221. @keydown.13.native.stop="inputDown(colItem)" :pop-visible="false"
  222. :name="colItem.field"></InputNumberPop>
  223. <Button type="primary" size="small" @click="inputDown(colItem)">{{ $t('filling') }}</Button>
  224. </div>
  225. </div>
  226. </Poptip>
  227. <!--冻结/解冻列-->
  228. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  229. v-if="freezeData && colItem.freezeFlag"
  230. type="ios-lock"
  231. @click="handleFreeze(colItem.field)"/>
  232. </template>
  233. <template #edit="{ row ,rowIndex}">
  234. <vxe-input v-model="row[colItem.field]" type="number"
  235. :ref="colItem.field+rowIndex"
  236. v-if="setCellCanInput(row,colItem)"
  237. @contextmenu.native.prevent
  238. :controls="false"
  239. @dblclick.native.stop="copyValue(row,colItem,rowIndex)"
  240. @blur="changeValue(colItem,rowIndex,row)"
  241. @keydown.native="controlNumber(Object.assign($event,{column:colItem,colIndex,rowIndex, row}))"
  242. @keyup.native="handleKeyup(Object.assign($event,{column:colItem,colIndex,rowIndex, row}))"></vxe-input>
  243. <div class="edit-div-class" @dblclick.stop="copyValue(row,colItem,rowIndex)" v-else>{{
  244. row[colItem.field]
  245. }}
  246. </div>
  247. </template>
  248. </vxe-column>
  249. <!-- 下拉 -->
  250. <vxe-column v-if="colItem.type === 'select'" :field="colItem.field"
  251. :title="colItem.title?colItem.title:$t(colItem.field)"
  252. :type="colItem.type"
  253. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  254. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  255. :edit-render="{autofocus: '.vxe-input--inner'}">
  256. <template #header="{ row }">
  257. <span>{{ $t(colItem.title ? colItem.title : colItem.field) }}</span>
  258. <Poptip trigger="click" transfer @on-popper-hide="popperHide">
  259. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  260. v-if="colItem.copyVisible"/>
  261. <div slot="content">
  262. <div style="display: flex;align-items: center;justify-content: center;">
  263. <!-- {{ colItem.options() }}-->
  264. <Select size="small" v-model="inputDownValue" style="width:200px" :multiple="colItem.multiple"
  265. clearable>
  266. <Option v-for="item in (typeof colItem.options === 'function'?colItem.options():colItem.options)"
  267. :key="item[colItem.valueKey]"
  268. :value="item[colItem.valueKey]"
  269. :label="item[colItem.labelKey]">
  270. {{ item[colItem.labelKey] }}
  271. </Option>
  272. </Select>
  273. <Button type="primary" size="small" @click="inputDown(colItem)">{{ $t('filling') }}</Button>
  274. </div>
  275. </div>
  276. </Poptip>
  277. <!--冻结/解冻列-->
  278. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  279. v-if="freezeData && colItem.freezeFlag"
  280. type="ios-lock"
  281. @click="handleFreeze(colItem.field)"/>
  282. </template>
  283. <template #default="{ row }">
  284. <div class="edit-div-class" @dblclick.stop="copyValue(row,colItem)">{{
  285. row[colItem.field + '_Name']
  286. }}
  287. </div>
  288. </template>
  289. <!--多选-->
  290. <template #edit="{ row,column,rowIndex }" v-if="colItem.multiple">
  291. <vxe-select transfer v-model="row[colItem.field]" v-show="setCellCanInput( row ,colItem)"
  292. :ref="colItem.field+rowIndex"
  293. @change="changeValue(colItem,rowIndex,row)"
  294. :disabled="setCellDisabled(row,colItem)" placeholder="请选择下拉"
  295. multiple clearable>
  296. <vxe-option v-for="item in typeof colItem.options === 'function'?colItem.options():colItem.options"
  297. :key="item[colItem.valueKey]"
  298. :value="item[colItem.valueKey]"
  299. :label="item[colItem.labelKey]"></vxe-option>
  300. </vxe-select>
  301. </template>
  302. <template v-else #edit="{ row,column,rowIndex }">
  303. <TableSelect :text="row[colItem.field + '_Name']?row[colItem.field + '_Name']:row[colItem.labelKey]"
  304. v-if="setCellCanInput( row ,colItem)"
  305. :field="colItem.field" :dataType="colItem.dataType"
  306. :ref="colItem.field + rowIndex"
  307. :keyValue="colItem.valueKey"
  308. :label="colItem.labelKey"
  309. :title="colItem.title"
  310. :clearFlag="true"
  311. :options="typeof colItem.options === 'function'?colItem.options():colItem.options"
  312. :searchDataFlag="false"
  313. :search-detail-flag="false"
  314. :sortBoolean="colItem.sortBoolean"
  315. @focus="handleSelectFocus(row,rowIndex,colItem)"
  316. @change="inputChange(row,rowIndex,colItem)"
  317. @clear="clearSelect(Object.assign({column:colItem,colIndex,rowIndex,row}))"
  318. @keydown.stop="nextInputCellByKeyDown(Object.assign($event,{column:colItem,colIndex,rowIndex,row}))"
  319. @onChoose="onChoose($event,row,column,rowIndex,colIndex,colItem.fieldUpdate,colItem)"></TableSelect>
  320. <div class="edit-div-class" @dblclick.stop="copyValue(row,colItem,rowIndex)" v-else>
  321. {{ row[colItem.field + '_Name'] }}
  322. </div>
  323. </template>
  324. </vxe-column>
  325. <!-- date -->
  326. <vxe-column v-if="colItem.type === 'date'" :params="{type:'date'}"
  327. :field="colItem.title?colItem.title:$t(colItem.field)"
  328. :type="colItem.type"
  329. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  330. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  331. :title="$t(colItem.field)" :edit-render="{autofocus: '.vxe-input--inner'}">
  332. <template #header="{ row }">
  333. <span>{{ $t(colItem.title ? colItem.title : colItem.field) }}</span>
  334. <Poptip trigger="click" transfer @on-popper-hide="popperHide">
  335. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  336. v-if="colItem.copyVisible"/>
  337. <div slot="content">
  338. <div style="display: flex;align-items: center;justify-content: center;">
  339. <DatePicker transfer v-model="inputDownValue" type="date"></DatePicker>
  340. <Button type="primary" size="small" @click="inputDown(colItem)">{{ $t('filling') }}</Button>
  341. </div>
  342. </div>
  343. </Poptip>
  344. <!--冻结/解冻列-->
  345. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  346. v-if="freezeData && colItem.freezeFlag"
  347. type="ios-lock"
  348. @click="handleFreeze(colItem.field)"/>
  349. </template>
  350. <template #default="{ row }">
  351. <span @dblclick.stop="copyValue(row,colItem)">{{ dateFormat(row[colItem.field]) }}</span>
  352. </template>
  353. <template #edit="{ row,rowIndex }">
  354. <DatePicker transfer :ref="colItem.field+rowIndex" v-model="row[colItem.field]" type="date"
  355. @on-change="changeValue(colItem,rowIndex,row)"
  356. v-show="setCellCanInput( row ,colItem)" :disabled="setCellDisabled(row,colItem)"></DatePicker>
  357. </template>
  358. </vxe-column>
  359. <!-- 开关 -->
  360. <vxe-column v-if="colItem.type === 'switch'" :params="{type:'switch'}"
  361. :title="colItem.title?colItem.title:$t(colItem.field)"
  362. :type="colItem.type"
  363. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  364. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  365. :field="$t(colItem.field)" :edit-render="{autofocus: '.vxe-input--inner'}">
  366. <template #header="{ row }">
  367. <span>{{ $t(colItem.title ? colItem.title : colItem.field) }}</span>
  368. <Poptip trigger="click" transfer @on-popper-hide="popperHide">
  369. <Icon type="iconfont iconfont icon-copy" style="cursor: pointer;" ref="down-input-icon"
  370. v-if="colItem.copyVisible"/>
  371. <div slot="content">
  372. <div style="display: flex;align-items: center;justify-content: center;">
  373. <i-switch v-model="inputDownBooleanValue"/>
  374. <Button type="primary" style="margin-left:10px" size="small" @click="inputDown(colItem)">
  375. {{ $t('filling') }}
  376. </Button>
  377. </div>
  378. </div>
  379. </Poptip>
  380. <!--冻结/解冻列-->
  381. <Icon ref="freeze" :class="colItem.freezeFlag?'freezed-class':'freeze-class' "
  382. v-if="freezeData && colItem.freezeFlag"
  383. type="ios-lock"
  384. @click="handleFreeze(colItem.field)"/>
  385. </template>
  386. <template #default="{ row,rowIndex }">
  387. <!-- <span>{{ row[colItem.field] ? '是' : '否' }}</span>-->
  388. <vxe-switch :ref="colItem.field+'_default_'+rowIndex" v-model="row[colItem.field]"
  389. v-show="setCellCanInput( row ,colItem)"
  390. @change="changeValue(colItem,rowIndex,row)"
  391. :disabled="setCellDisabled(row,colItem)"/>
  392. </template>
  393. <template #edit="{ row,rowIndex }">
  394. <vxe-switch :ref="colItem.field+rowIndex" v-model="row[colItem.field]"
  395. v-show="setCellCanInput( row ,colItem)"
  396. @change="changeValue(colItem,rowIndex,row)"
  397. :disabled="setCellDisabled(row,colItem)"/>
  398. </template>
  399. </vxe-column>
  400. <!-- 按钮 -->
  401. <vxe-column v-if="colItem.type === 'button'" :params="{type:'button'}"
  402. :title="colItem.title?colItem.title:$t(colItem.field)"
  403. :type="colItem.type"
  404. :width="colItem.width?colItem.width:vm.$config.columnWidth"
  405. :min-width="colItem.minWidth?colItem.minWidth:vm.$config.columnWidthMin"
  406. :field="$t(colItem.field)" :edit-render="{autofocus: '.vxe-input--inner'}">
  407. <template #default="{ row,rowIndex }">
  408. <vxe-button :ref="colItem.field+'_default_'+rowIndex" v-model="row[colItem.field]"
  409. v-show="setCellCanInput( row ,colItem)"
  410. :content="row[colItem.field]"
  411. @click.native="buttonClick(colItem,rowIndex,row)"
  412. :disabled="setCellDisabled(row,colItem)"/>
  413. </template>
  414. <template #edit="{ row,rowIndex }">
  415. <vxe-button class-name="edit-button" :ref="colItem.field+'_'+rowIndex" v-model="row[colItem.field]"
  416. v-show="setCellCanInput( row ,colItem)"
  417. :content="row[colItem.field]"
  418. @click.native="buttonClick(colItem,rowIndex,row)"
  419. :disabled="setCellDisabled(row,colItem)"/>
  420. </template>
  421. </vxe-column>
  422. <!-- checkbox-->
  423. <vxe-column type="checkbox" width="60" v-if="colItem.type === 'checkbox'"></vxe-column>
  424. </div>
  425. </vxe-table>
  426. <div class="setting-class" v-if="showSettingFlag"
  427. :style="'top:' + settingTop + ';left:' + settingLeft"
  428. @click="showSetting">
  429. <Icon ref="setting" type="md-settings"/>
  430. </div>
  431. </div>
  432. </template>
  433. <script>
  434. import XEUtils from "xe-utils";
  435. import SeqButton from '@/components/sub/seq-button/index'
  436. import Sortable from "sortablejs";
  437. export default {
  438. name: 'EditTable',
  439. components: {SeqButton},
  440. props: {
  441. value: {
  442. type: Array,
  443. default: () => {
  444. }
  445. },
  446. // 表格标题
  447. title: {
  448. type: String,
  449. default: undefined,
  450. },
  451. // 数据源
  452. data: {
  453. type: Array,
  454. default: () => {
  455. }
  456. },
  457. // 列数据源
  458. columns: {
  459. type: Array,
  460. default: () => []
  461. },
  462. // 列表的高度
  463. height: {
  464. type: [String, Number],
  465. default: 300
  466. },
  467. // 列表的宽度
  468. divWidth: {
  469. type: String,
  470. default: '100%'
  471. },
  472. // 是否允许重复
  473. enabledRepeat: {
  474. type: Boolean,
  475. default: false
  476. },
  477. // 允许重复的列名称
  478. enabledRepeatId: {
  479. type: [String, Array],
  480. default: ''
  481. },
  482. //该行数据有值其他可输入单元格才能进行
  483. controlId: {
  484. type: String,
  485. default: ''
  486. },
  487. // 是否显示新增按钮
  488. addFlag: {
  489. type: Boolean,
  490. default: true
  491. },
  492. //是否显示删除按钮
  493. deleteFlag: {
  494. type: Boolean,
  495. default: true
  496. },
  497. // 控制删除按钮是否可见(外部传入的方法)
  498. canDelete: {
  499. type: Function,
  500. default: () => {
  501. return true
  502. }
  503. },
  504. // 唯一勾选的列表(用于switch,数据中只有一个勾选值)
  505. defaultField: {
  506. type: String,
  507. default: ''
  508. },
  509. //操作标识(新增、删除)是否可见
  510. operateFlag: {
  511. type: Boolean,
  512. default: true
  513. },
  514. // 是否显示新行
  515. newRowFlag: {
  516. type: Boolean,
  517. default: true
  518. },
  519. // 新增新行,不判断是否有数据
  520. newRowAlwaysFlag: {
  521. type: Boolean,
  522. default: false
  523. },
  524. //光标是否进入新增行
  525. focusFlag: {
  526. type: Boolean,
  527. default: false
  528. },
  529. //是否显示表头
  530. showHeader: {
  531. type: Boolean,
  532. default: true
  533. },
  534. //是否显示合计行
  535. showFooter: {
  536. type: Boolean,
  537. default: false
  538. },
  539. //表格名字
  540. name: {
  541. type: String,
  542. default: 'xTable'
  543. },
  544. //是否高亮显示
  545. highlightCurrentRow: {
  546. type: Boolean,
  547. default: false
  548. },
  549. requiredColumn: {
  550. type: Array,
  551. default: () => []
  552. },
  553. //主键Id
  554. majorField: {
  555. type: String,
  556. default: ''
  557. },
  558. //是否只读
  559. readonly: {
  560. type: Boolean,
  561. default: false
  562. },
  563. // 是否显示设置按钮
  564. showSettingFlag: {
  565. type: Boolean,
  566. default: true
  567. },
  568. // 是否开启冻结
  569. freeze: {
  570. type: Boolean,
  571. default: true,
  572. },
  573. // 鼠标移入进行编辑的标识
  574. mouseEnterEditFlag: {
  575. type: Boolean,
  576. default: true,
  577. },
  578. // 是否自动跳转到下一个可输入框
  579. autoNextFlag: {
  580. type: Boolean,
  581. default: true,
  582. },
  583. // 是否自动聚焦一行
  584. autoFocus: {
  585. type: Boolean,
  586. default: true,
  587. }
  588. },
  589. provide() {
  590. return {
  591. dkEditTable: this
  592. }
  593. },
  594. data() {
  595. const vm = window.vm;
  596. return {
  597. vm: vm,
  598. rowIndex: -1,
  599. colIndex: -1,
  600. visibleRows: [],// 可见的行,用于控制
  601. header: {
  602. options: [
  603. [
  604. // <!--冻结-->
  605. {
  606. code: 'freeze', name: vm.$t('freeze'), disabled: false, visible: false, children: [
  607. {code: 'colFreeze', name: vm.$t('freeze'), disabled: false, visible: true},
  608. {code: 'clearFreeze', name: vm.$t('clearFreeze'), disabled: false, visible: false},
  609. ]
  610. },
  611. // <!--定位-->
  612. {
  613. code: 'position', name: vm.$t('positionItem'), disabled: false, visible: true, children: [
  614. {code: 'rowPosition', name: vm.$t('rowPosition'), disabled: false, visible: true},
  615. {code: 'exchange', name: '替换', disabled: false, visible: true},
  616. {code: 'colPosition', name: vm.$t('colPosition'), disabled: false, visible: true},
  617. ]
  618. },
  619. // <!--自适应列宽-->
  620. {
  621. code: 'adaption', name: vm.$t('adaption'), disabled: false, visible: true, children: [
  622. {code: 'selfAdaptionOne', name: vm.$t('selfAdaptionOne'), disabled: false, visible: true},
  623. {code: 'selfAdaption', name: vm.$t('selfAdaption'), disabled: false, visible: true},
  624. ]
  625. },
  626. ]
  627. ]
  628. },
  629. freezeData: false, // 是否开启冻结
  630. enabledEdit: true, //是否启用编辑模式
  631. modal1: true,
  632. filterWrapper: true,
  633. inputDownValue: null,
  634. inputDownBooleanValue: false,
  635. tableData: [],
  636. tableDataChange: [], //改变集合
  637. isScroll: false,
  638. validRules: {}, //校验的规则
  639. deleteData: [], //删除的数据
  640. columnsData: [], //记录列
  641. currentRowIndex: 0, // 当前行
  642. settingLeft: null, // 设置图标居左的位置
  643. settingTop: null, // 设置图标居上的位置
  644. }
  645. },
  646. watch: {
  647. newRowFlag(n, o) {
  648. if (n) {
  649. this.add();
  650. } else {
  651. this.tableData = []
  652. }
  653. },
  654. data(n, o) {
  655. // 编辑
  656. if (n && n.length > 0) {
  657. this.tableData = n
  658. this.handleSelectData();
  659. } else {
  660. this.tableData = []
  661. this.tableDataChange = []
  662. if (this.newRowFlag) {
  663. //新建时增加行
  664. this.add()
  665. }
  666. }
  667. },
  668. tableData(n, o) {
  669. if (n) {
  670. this.$emit('input', n)
  671. }
  672. },
  673. },
  674. methods: {
  675. /**
  676. * @desc : 重新加载列
  677. * @author : 周兴
  678. * @date : 2023/4/4 16:24
  679. */
  680. loadColumn(columns) {
  681. if (columns) {
  682. return this.$refs[this.name].loadColumn(columns);
  683. } else {
  684. const tableColumns = this.$refs[this.name].getTableColumn();
  685. this.$refs[this.name].loadColumn(tableColumns.fullColumn);
  686. }
  687. },
  688. /**
  689. * @desc : 复制单元格
  690. * @author : 周兴
  691. * @date : 2022/6/9 9:31
  692. */
  693. copyValue(row, colItem) {
  694. let field = colItem.field;
  695. if (row && row[field]) {
  696. this.$message.success(vm.$t('copySuccess') + ':' + row[field]);
  697. //创建一个input框
  698. const input = document.createElement("input");
  699. //将指定的DOM节点添加到body的末尾
  700. document.body.appendChild(input);
  701. //设置input框的value值为直播地址
  702. input.setAttribute("value", row[field]);
  703. //选取文本域中的内容
  704. input.select();
  705. //copy的意思是拷贝当前选中内容到剪贴板
  706. //document.execCommand()方法操纵可编辑内容区域的元素
  707. //返回值为一个Boolean,如果是 false 则表示操作不被支持或未被启用
  708. if (document.execCommand("copy")) {
  709. document.execCommand("copy");
  710. }
  711. //删除这个节点
  712. document.body.removeChild(input);
  713. }
  714. },
  715. /**
  716. * @desc : 单元格样式
  717. * @author : 周兴
  718. * @date : 2022/8/6 11:15
  719. */
  720. cellStyle({row, rowIndex, column}) {
  721. //序号居中
  722. if (column.type === 'seq' || column.field === 'seq' || column.type === 'checkbox' || column.type === 'date'
  723. || column.type === 'switch' || (column.params && column.params.type === 'disabled' && column.params.switch)) {
  724. return {textAlign: 'center'}
  725. } else if (column.type === 'number' || (column.params && column.params.type === 'disabled' && column.params.number)) {
  726. return {textAlign: 'right'}
  727. } else {
  728. return {textAlign: 'left'}
  729. }
  730. },
  731. /**
  732. * @desc : 点击右键
  733. * @author : 周兴
  734. * @date : 2022/12/19 11:12
  735. */
  736. closeAllPop(e) {
  737. this.$refs[this.name].$el.click(); //关闭表头的pop
  738. },
  739. /**
  740. * @desc : 右键菜单
  741. * @author : 沈博
  742. * @date : 2022/3/13 10:55
  743. */
  744. visibleMethod(e) {
  745. // 表格可以冻结
  746. if (this.freeze) {
  747. this.header.options[0].update({code: 'freeze', visible: true}, 'code', 'visible');
  748. }
  749. return true
  750. },
  751. /**
  752. * @desc : 获取当前行
  753. * @author : 周兴
  754. * @date : 2022/5/3 17:51
  755. */
  756. getCurrentRow() {
  757. return this.$refs[this.name].getCurrentRecord()
  758. },
  759. /**
  760. * @desc : 获取当前行号
  761. * @author : 周兴
  762. * @date : 2022/5/3 17:51
  763. */
  764. getCurrentRowIndex() {
  765. let row = this.$refs[this.name].getCurrentRecord()
  766. if (row) {
  767. return this.$refs[this.name].getRowIndex(row)
  768. }
  769. },
  770. /**
  771. * @desc : 点击右键按钮
  772. * @author : 周兴
  773. * @date : 2022/3/30 17:24
  774. */
  775. menuClick(e) {
  776. if (e.menu.code == 'selfAdaption') {
  777. this.autoWidthHandle()
  778. } else if (e.menu.code == 'selfAdaptionOne') {
  779. this.autoWidthHandle(e)
  780. } else if (e.menu.code == 'export') {
  781. this.export(e.column.property)
  782. } else if (e.menu.code == 'rowPosition' || e.menu.code == 'exchange') { // 行定位及替换
  783. this.showRowPosition(e, e.menu.code);
  784. } else if (e.menu.code == 'colPosition') { // 列定位
  785. this.showColPosition(e);
  786. } else if (e.menu.code == 'colFreeze') { // 列冻结
  787. this.handleFreeze(e);
  788. } else if (e.menu.code == 'clearFreeze') { // 解除冻结
  789. this.unFreeze(e);
  790. }
  791. },
  792. /**
  793. * @desc : 显示行定位定位框
  794. * @author : 周兴
  795. * @date : 2022/11/30 13:00
  796. */
  797. showRowPosition(e, code) {
  798. let tabValue = code == 'exchange' ? 'exchange' : 'search'
  799. this.$Position({component: this.$refs[this.name], tabValue, title: this.$t('findAndExchange')},
  800. {
  801. // 切换查询出的数据
  802. change: ({field, column, rowIndex, fields, equalFlag, searchText, noSelectAll, type}) => {
  803. // 没有查出数据
  804. if (noSelectAll) {
  805. this.changeFind(fields, equalFlag, searchText, type);// 查找下一条
  806. } else {
  807. // 定位当前行
  808. this.$refs[this.name].setCurrentRow(this.tableData[rowIndex]);
  809. // 定位当前列
  810. // this.$refs[this.name].setCurrentColumn(column);
  811. this.$nextTick(() => {
  812. this.$refs[this.name].scrollToRow(this.tableData[rowIndex]);
  813. this.$refs[this.name].scrollToColumn(column)
  814. });
  815. }
  816. },
  817. // 替换全部
  818. exchangeAll: (searchText, replaceText, fields, equalFlag) => {
  819. this.exchangeAll(searchText, replaceText, fields, equalFlag);
  820. },
  821. // 替换
  822. exchange: (searchText, replaceText, fields, equalFlag) => {
  823. this.exchange(searchText, replaceText, fields, equalFlag);
  824. },
  825. })
  826. },
  827. /**
  828. * @desc : 改变查找的行
  829. * @author : 周兴
  830. * @date : 2023/2/16 13:05
  831. */
  832. changeFind(fields, equalFlag, searchText, type) {
  833. // 获取当前行
  834. let currentIndex = this.getCurrentRowIndex()
  835. if (!currentIndex && currentIndex != 0) {
  836. currentIndex = -1;
  837. }
  838. let table = this.tableData
  839. // 向下
  840. if (type === 'next') {
  841. for (let i = currentIndex + 1; i < table.length; i++) {
  842. let flag = this._changeFindItem(table[i], fields, equalFlag, searchText)
  843. if (flag) {
  844. return
  845. }
  846. }
  847. } else {
  848. // 向前
  849. for (let i = currentIndex - 1; i >= 0; i--) {
  850. let flag = this._changeFindItem(table[i], fields, equalFlag, searchText)
  851. if (flag) {
  852. return
  853. }
  854. }
  855. }
  856. },
  857. /**
  858. * @desc : desc
  859. * @author : 周兴
  860. * @date : 2023/2/16 13:09
  861. */
  862. _changeFindItem(item, fields, equalFlag, searchText) {
  863. // 模糊查询
  864. for (let it of fields) {
  865. // 值等
  866. if (equalFlag) {
  867. if ((item[it.field] + '') == searchText) {
  868. // 定位当前行
  869. this.$refs[this.name].setCurrentRow(item);
  870. this.$nextTick(() => {
  871. this.$refs[this.name].scrollToRow(item);
  872. });
  873. return true;
  874. }
  875. } else {
  876. // 模糊
  877. if ((item[it.field] + '').indexOf(searchText) >= 0) {
  878. // 定位当前行
  879. this.$refs[this.name].setCurrentRow(item);
  880. this.$nextTick(() => {
  881. this.$refs[this.name].scrollToRow(item);
  882. });
  883. return true;
  884. }
  885. }
  886. }
  887. return false;
  888. },
  889. /**
  890. * @desc : 替换全部数据
  891. * @author : 周兴
  892. * @date : 2023/2/15 14:49
  893. */
  894. exchangeAll(searchText, replaceText, fields, equalFlag) {
  895. let table = this.tableData
  896. let count = 0;
  897. for (let i = 0; i < table.length; i++) {
  898. // 模糊查询
  899. fields.forEach(it => {
  900. // 值等
  901. if (equalFlag) {
  902. if ((table[i][it.field] + '') == searchText) {
  903. this.$set(table[i], it.field, replaceText);
  904. this.setTableChange(table[i]); // 设置改变数据
  905. count++;
  906. }
  907. } else {
  908. // 模糊
  909. if ((table[i][it.field] + '').indexOf(searchText) >= 0) {
  910. let value = (table[i][it.field] + '').replaceAll(searchText, replaceText)
  911. this.$set(table[i], it.field, value);
  912. this.setTableChange(table[i]); // 设置改变数据
  913. count++;
  914. }
  915. }
  916. })
  917. }
  918. if (count > 0) {
  919. this.$message.success("已经完成查找并进行" + count + '处替换');
  920. }
  921. },
  922. /**
  923. * @desc : 替换数据
  924. * @author : 周兴
  925. * @date : 2023/2/15 14:49
  926. */
  927. exchange(searchText, replaceText, fields, equalFlag) {
  928. let table = this.tableData
  929. if (!table || table.length == 0) return;
  930. // 获取当前行
  931. let currentIndex = this.getCurrentRowIndex()
  932. if (!currentIndex && currentIndex != 0) {
  933. currentIndex = 0;
  934. }
  935. let checkFlag = false;
  936. // 判断当前有可以替换的数据
  937. // 值等
  938. if (equalFlag) {
  939. fields.forEach(it => {
  940. if ((table[currentIndex][it.field] + '') == searchText) {
  941. this.$set(table[currentIndex], it.field, replaceText);
  942. this.setTableChange(table[currentIndex]); // 设置改变数据
  943. checkFlag = true;
  944. }
  945. })
  946. } else {
  947. // 模糊
  948. fields.forEach(it => {
  949. // 模糊
  950. if ((table[currentIndex][it.field] + '').indexOf(searchText) >= 0) {
  951. let value = (table[currentIndex][it.field] + '').replaceAll(searchText, replaceText)
  952. this.$set(table[currentIndex], it.field, value);
  953. this.setTableChange(table[currentIndex]); // 设置改变数据
  954. checkFlag = true;
  955. }
  956. })
  957. }
  958. // 如果到最后一行,需要切换到第一行有数据的
  959. if (currentIndex === table.length - 1) {
  960. currentIndex = -1;
  961. }
  962. // 如果当前行没有数据,就跳转到下一行有匹配数据的行
  963. if (!checkFlag) {
  964. for (let i = currentIndex + 1; i < table.length; i++) {
  965. // 模糊查询
  966. for (let it of fields) {
  967. // 值等
  968. if (equalFlag) {
  969. if ((table[i][it.field] + '') == searchText) {
  970. // 定位当前行
  971. this.$refs[this.name].setCurrentRow(this.tableData[i]);
  972. this.$nextTick(() => {
  973. this.$refs[this.name].scrollToRow(this.tableData[i]);
  974. });
  975. return;
  976. }
  977. } else {
  978. // 模糊
  979. if ((table[i][it.field] + '').indexOf(searchText) >= 0) {
  980. // 定位当前行
  981. this.$refs[this.name].setCurrentRow(this.tableData[i]);
  982. this.$nextTick(() => {
  983. this.$refs[this.name].scrollToRow(this.tableData[i]);
  984. });
  985. return;
  986. }
  987. }
  988. }
  989. }
  990. }
  991. },
  992. /**
  993. * @desc : 设置改变表格
  994. * @author : 周兴
  995. * @date : 2023/2/15 14:56
  996. */
  997. setTableChange(row) {
  998. let rows = this.tableDataChange.filter(it => it['_X_ID'] === row['_X_ID']);
  999. //说明已经存在
  1000. if (rows && rows.length > 0) {
  1001. this.tableDataChange = this.tableDataChange.filter(it => it['_X_ID'] !== row['_X_ID']).map(it => it);
  1002. }
  1003. this.tableDataChange.push(row);
  1004. },
  1005. /**
  1006. * @desc : 显示列定位定位框
  1007. * @author : 周兴
  1008. * @date : 2022/11/30 13:00
  1009. */
  1010. showColPosition(e) {
  1011. let event = e.$event
  1012. this.$Position({left: event.clientX, top: event.clientY},
  1013. {
  1014. ok: (searchText) => {
  1015. this.handleColPosition(searchText); // 执行列定位方法
  1016. },
  1017. })
  1018. },
  1019. /**
  1020. * @author : 寇珊珊
  1021. * @date : 2022/3/23 16:29
  1022. * @desc : 自适应全部列宽
  1023. */
  1024. autoWidthHandle(e) {
  1025. if (this.tableData && this.tableData.length > 0) {
  1026. let data = this.tableData.copy();
  1027. if (e) {
  1028. // 当前列的自适应
  1029. let columnValues = data.map(it => it[e.column.field]);
  1030. columnValues.push(e.column.title + '占位占位') // 把每列的表头也加进去算
  1031. let width = this.getMaxLength(columnValues) + 10 // 每列内容最大的宽度 + 表格的内间距(依据实际情况而定)
  1032. // this.$set(e.column, 'width', width)
  1033. // 取这个宽度和最小宽度的最大值
  1034. let minWidth = (e.column.minWidth + '')?.replace('px', '').toNum(0);
  1035. this.$set(e.column, 'width', [width, minWidth].max())
  1036. } else {
  1037. // 全部可见列的自适应
  1038. let columns = this.$refs[this.name].getColumns();
  1039. columns.forEach(col => {
  1040. if (col.type !== 'seq' && col.type !== 'radio' && col.type !== 'checkbox'
  1041. && col.type !== 'switch' && col.type !== 'operate' && col.type !== 'info'
  1042. && col.type !== 'blank') {
  1043. let columnValues = data.map(it => it[col.field]);
  1044. columnValues.push(col.title + '占位占位') // 把每列的表头也加进去算
  1045. let width = this.getMaxLength(columnValues) + 10 // 每列内容最大的宽度 + 表格的内间距(依据实际情况而定)
  1046. // this.$set(col, 'width', width)
  1047. // 取这个宽度和最小宽度的最大值
  1048. let minWidth = (col.minWidth + '')?.replace('px', '').toNum(0);
  1049. this.$set(col, 'width', [width, minWidth].max())
  1050. }
  1051. })
  1052. }
  1053. this.$nextTick(() => {
  1054. this.$refs[this.name].resetColumn({resizable: true})
  1055. this.$refs[this.name].recalculate()
  1056. })
  1057. }
  1058. },
  1059. /**
  1060. * @desc : 解除冻结
  1061. * @author : 周兴
  1062. * @date : 2022/12/1 13:30
  1063. */
  1064. unFreeze() {
  1065. const tableColumns = this.$refs[this.name].getTableColumn()
  1066. if (tableColumns && tableColumns.visibleColumn) {
  1067. tableColumns.visibleColumn.forEach(it => {
  1068. if (it.type != 'seq' && it.type != 'radio' && it.type != 'checkbox') {
  1069. it.fixed = ''
  1070. }
  1071. })
  1072. this.columnsData.forEach(it => {
  1073. it.freezeFlag = false;
  1074. })
  1075. // 清空冻结不可见
  1076. // this.header.options[0].update({code: 'clearFreeze', visible: false}, 'code', 'visible');
  1077. let filters = this.header.options[0].filter(it => it.code === 'freeze');
  1078. if (filters && filters.length > 0) {
  1079. filters[0].children.update({code: 'clearFreeze', visible: false}, 'code', 'visible');
  1080. }
  1081. this.$refs[this.name].refreshColumn();
  1082. }
  1083. },
  1084. /**
  1085. * @desc : 冻结列
  1086. * @author : 周兴
  1087. * @date : 2022/11/30 17:28
  1088. */
  1089. handleFreeze(e) {
  1090. let field = e.column.field;
  1091. const tableColumns = this.$refs[this.name].getTableColumn()
  1092. if (tableColumns && tableColumns.visibleColumn) {
  1093. let columnIndex = 0;
  1094. tableColumns.visibleColumn.forEach((it, index) => {
  1095. it.fixed = ''
  1096. if (it.field === field) {
  1097. columnIndex = index;
  1098. }
  1099. })
  1100. // 冻结该列之前的列,包括这列
  1101. for (let i = 0; i <= columnIndex; i++) {
  1102. tableColumns.visibleColumn[i].fixed = 'left'
  1103. }
  1104. // 先把所有的冻结标识都去掉
  1105. this.columnsData.forEach(it => {
  1106. it.freezeFlag = false;
  1107. })
  1108. // 找到当前列,给其赋值冻结标识
  1109. let colItems = this.columnsData.filter(it => it.field === field);
  1110. if (colItems && colItems.length > 0) {
  1111. colItems[0].freezeFlag = true;
  1112. }
  1113. // 清空冻结可见
  1114. // this.header.options[0].update({code: 'clearFreeze', visible: true}, 'code', 'visible');
  1115. let filters = this.header.options[0].filter(it => it.code === 'freeze');
  1116. if (filters && filters.length > 0) {
  1117. filters[0].children.update({code: 'clearFreeze', visible: true}, 'code', 'visible');
  1118. }
  1119. this.$refs[this.name].refreshColumn();
  1120. }
  1121. },
  1122. /**
  1123. * @desc : 下拉聚焦事件
  1124. * @author : 周兴
  1125. * @date : 2022/4/22 9:10
  1126. */
  1127. handleSelectFocus(row, rowIndex, colItem) {
  1128. this.$emit('on-select-focus', colItem.field, row, rowIndex)
  1129. },
  1130. /**
  1131. * @desc : 给下拉数据源增加Name列
  1132. * @author : 周兴
  1133. * @date : 2022/4/22 9:02
  1134. */
  1135. handleSelectData() {
  1136. let options = []
  1137. //记录下拉的列
  1138. this.columns.forEach(col => {
  1139. if (col.type === 'select') {
  1140. if (this.tableData && this.tableData.length > 0) {
  1141. //获取下拉数据源
  1142. if (typeof col.options === 'function') {
  1143. options = col.options()
  1144. }
  1145. this.tableData.forEach(row => {
  1146. if (row[col.field]) {
  1147. if (col.multiple) {
  1148. let selectVal = options.filter(it => row[col.field].includes(it[col.valueKey]));
  1149. if (selectVal) {
  1150. this.$set(row, col.field + '_Name', selectVal.map(it => {
  1151. return it[col.labelKey]
  1152. }).toString())
  1153. }
  1154. } else {
  1155. let selectVal = options.filter(it => row[col.field] === it[col.valueKey])[0];
  1156. if (selectVal) {
  1157. this.$set(row, col.field + '_Name', selectVal[col.labelKey])
  1158. }
  1159. }
  1160. }
  1161. })
  1162. }
  1163. }
  1164. })
  1165. },
  1166. /**
  1167. * @desc : 点击表格的keydown自动进入第一个单元格
  1168. * @author : 周兴
  1169. * @date : 2022/4/19 13:17
  1170. */
  1171. handleTableKeyDown(e) {
  1172. if (e.$event.key === 'Enter') {
  1173. this.$refs[this.name].setActiveCell(this.tableData[0], this.columns[0].field);
  1174. this.handleFocus(this.columns[0], 0);
  1175. }
  1176. },
  1177. /**
  1178. * @desc : 鼠标移入增加新增和删除按钮
  1179. * @author : 周兴
  1180. * @date : 2022/4/19 10:56
  1181. */
  1182. handleCellMouseEnter(e) {
  1183. if (this.readonly) {
  1184. return;
  1185. }
  1186. if (this.operateFlag) {
  1187. // 如果行不变,不用执行
  1188. if (e && (e.rowIndex != this.rowIndex)) {
  1189. if (this.tableData && this.tableData.length > 0) {
  1190. this.tableData.forEach(it => {
  1191. this.$set(it, '_hover', false)
  1192. })
  1193. }
  1194. }
  1195. setTimeout(() => {
  1196. if (e.rowIndex >= 0 && this.tableData[e.rowIndex]) {
  1197. this.$set(this.tableData[e.rowIndex], '_hover', true)
  1198. }
  1199. }, 300)
  1200. }
  1201. // 鼠标移入后,当前行变为编辑状态
  1202. if (e && e.row) {
  1203. this.colIndex = e.columnIndex;
  1204. // 鼠标移入自动进行行切换
  1205. if (this.mouseEnterEditFlag) {
  1206. // this.$refs[this.name].setActiveRow(e.row)
  1207. // // console.log('ttt',e.row,e.rowIndex,this.currentRowIndex)
  1208. // this.$refs[this.name].setCurrentRow(e.row)
  1209. // if(this.rowIndex !== e.rowIndex){
  1210. // this.currentChangeGetData({ oldRowIndex:this.rowIndex,rowIndex:e.rowIndex })
  1211. // }
  1212. }
  1213. this.rowIndex = e.rowIndex;
  1214. }
  1215. },
  1216. /**
  1217. * @desc : 鼠标移出删除新增和删除按钮
  1218. * @author : 周兴
  1219. * @date : 2022/4/19 10:56
  1220. */
  1221. handleCellMouseLeave(e) {
  1222. if (this.operateFlag) {
  1223. setTimeout(() => {
  1224. if (e && e.rowIndex != this.rowIndex) {
  1225. if (e.rowIndex >= 0 && this.tableData[e.rowIndex]) {
  1226. this.$set(this.tableData[e.rowIndex], '_hover', false)
  1227. }
  1228. }
  1229. }, 300)
  1230. }
  1231. },
  1232. /**
  1233. * @desc : 开关控制
  1234. * @author : 周兴
  1235. * @date : 2022/6/23 9:09
  1236. */
  1237. switchValue(colItem, rowIndex, row) {
  1238. if (colItem.switchField) {
  1239. if (colItem.type === 'switch') {
  1240. // 如果开关开启,那么相关列修改为0
  1241. if (row[colItem.field] && this.tableData[rowIndex][colItem.switchField]) {
  1242. this.tableData[rowIndex][colItem.switchField] = 0;
  1243. // 重新计算
  1244. this.calc(colItem.switchField, rowIndex);
  1245. }
  1246. } else {
  1247. let flag = false;
  1248. // 控制开关列
  1249. if (row[colItem.field] && row[colItem.field] != 0) {
  1250. flag = false;
  1251. this.tableData[rowIndex][colItem.switchField] = false;
  1252. } else {
  1253. flag = true;
  1254. this.tableData[rowIndex][colItem.switchField] = true;
  1255. }
  1256. }
  1257. }
  1258. },
  1259. /**
  1260. * @desc : 值改变
  1261. * @author : 周兴
  1262. * @date : 2022/4/10 14:21
  1263. */
  1264. changeValue(colItem, rowIndex, row) {
  1265. let field = colItem.field;
  1266. //数值
  1267. if (colItem.type === 'number') {
  1268. // 如果是这里必须是负数,需要自动转为负数
  1269. // let neg = typeof colItem.negative == 'function' ? colItem.negative() : colItem.negative;
  1270. // if (neg && parseFloat(row[field]) > 0) {
  1271. // this.tableData[rowIndex][field] = -1 * parseFloat(row[field]);
  1272. // }
  1273. this.calc(field, rowIndex) //计算
  1274. } else if (colItem.type === 'text') { // 文本
  1275. if (colItem.field != 'remarks' && (colItem.upperFlag == undefined || colItem.upperFlag)) {
  1276. row[field] = row[field]?.toUpperCase(); // 备注默认转大写
  1277. }
  1278. } else if (colItem.type === 'select' && colItem.multiple) {
  1279. let options = typeof colItem.options === 'function' ? colItem.options() : colItem.options;
  1280. let selectVal = options.filter(it => row[field].includes(it[colItem.valueKey]));
  1281. if (selectVal) {
  1282. this.$set(row, field + '_Name', selectVal.map(it => {
  1283. return it[colItem.labelKey]
  1284. }).toString())
  1285. }
  1286. }
  1287. //开关
  1288. this.switchValue(colItem, rowIndex, row);
  1289. // 有联动,数据改变需要清空对应的列数据
  1290. this.columns.forEach(it => {
  1291. if (it.controlIds && it.controlIds.length > 0) {
  1292. //说明这列管控其他列
  1293. if (it.controlIds.includes(field)) {
  1294. row[it.field] = null;
  1295. }
  1296. }
  1297. })
  1298. // 如果勾选的是指定的列
  1299. if (this.defaultField && colItem.field === this.defaultField) {
  1300. this.changeDefaultFieldValue(row);
  1301. }
  1302. //把改变的行加入到tableDataChange中
  1303. // console.log('row',this.tableDataChange,row)
  1304. let rows = this.tableDataChange.filter(it => it['_X_ID'] === row['_X_ID']);
  1305. //说明已经存在
  1306. if (rows && rows.length > 0) {
  1307. this.tableDataChange = this.tableDataChange.filter(it => it['_X_ID'] !== row['_X_ID']).map(it => it);
  1308. }
  1309. this.tableDataChange.push(row);
  1310. this.$emit('changeValue', field, row, rowIndex);
  1311. // 更新合计行
  1312. this.$refs[this.name].updateFooter();
  1313. },
  1314. /**
  1315. * @desc : 改变值
  1316. * @author : 周兴
  1317. * @date : 2022/8/5 13:38
  1318. */
  1319. changeDefaultFieldValue(row) {
  1320. // 如果勾选这条,那么其他条要设置为不勾选
  1321. if (row[this.defaultField]) {
  1322. this.tableData.forEach(it => {
  1323. if (it['_X_ID'] !== row['_X_ID']) {
  1324. it[this.defaultField] = false;
  1325. }
  1326. })
  1327. } else {
  1328. // 如果去掉本条勾选,那么默认第一条作为勾选项
  1329. for (let i = 0; i < this.tableData.length; i++) {
  1330. if (this.tableData[i]['_X_ID'] !== row['_X_ID']) {
  1331. this.tableData[i][this.defaultField] = true;
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. },
  1337. /**
  1338. * @desc : 设置单元格是否可以输入
  1339. * @author : 周兴
  1340. * @date : 2022/4/7 15:20
  1341. */
  1342. setCellCanInput(row, column) {
  1343. // 如果已经在可视范围内,不用再走
  1344. let flag = false;
  1345. if (this.controlId) {
  1346. //console.log('ttt',this.controlId,row[this.controlId],row)
  1347. //看这列是否需要进行管控
  1348. if (column.controlId && this.controlId === column.controlId) {
  1349. flag = true;
  1350. } else {
  1351. //如果该行controlId有值,不管控
  1352. if (row[this.controlId]) {
  1353. flag = true;
  1354. }
  1355. }
  1356. } else {
  1357. flag = true;
  1358. // 如果列设置了管控
  1359. if (column.controlIds && column.controlIds.length > 0) {
  1360. for (let i in column.controlIds) {
  1361. //如果有任意一个没有数据,那么就不允许
  1362. if (!row[column.controlIds[i]]) {
  1363. flag = false;
  1364. break;
  1365. }
  1366. }
  1367. }
  1368. }
  1369. /* 除了controlId之外,还有新的校验 */
  1370. if (flag) {
  1371. if (column.editStatus) {
  1372. // console.log('订制品',column.editStatus(row),row)
  1373. flag = column.editStatus(row)
  1374. }
  1375. }
  1376. // 在disbaled中控制
  1377. if (flag && (column.type === 'select' || column.type === 'text' || column.type === 'number'
  1378. || column.type === 'button')) {
  1379. if (row['readOnly']) {
  1380. let readOnly = row['readOnly']
  1381. if (readOnly.indexOf(column.field) >= 0) {
  1382. flag = false;
  1383. }
  1384. }
  1385. }
  1386. return flag;
  1387. },
  1388. /**
  1389. * @desc : 控制下拉选择是否出现
  1390. * @author : 周兴
  1391. * @date : 2022/6/6 14:39
  1392. */
  1393. setCellCanTableSelect(row, column) {
  1394. //如果这列是否禁用的,就不能选择
  1395. if (this.readonly) {
  1396. return false;
  1397. } else if (typeof column.disabled === 'function' ? column.disabled() : column.disabled) {
  1398. return false;
  1399. } else {
  1400. if (this.controlId) {
  1401. //看这列是否需要进行管控
  1402. if (column.controlId) {
  1403. if (this.controlId !== column.controlId) {
  1404. return false;
  1405. }
  1406. } else {
  1407. if (!row[this.controlId]) {
  1408. return false;
  1409. }
  1410. }
  1411. }
  1412. if (row['readOnly']) {
  1413. let readOnly = row['readOnly']
  1414. if (readOnly.indexOf(column.field) >= 0) {
  1415. return false;
  1416. }
  1417. }
  1418. }
  1419. return true;
  1420. },
  1421. /**
  1422. * @desc : 设置是否只读
  1423. * @author : 周兴
  1424. * @date : 2022/4/11 14:22
  1425. */
  1426. setCellDisabled(row, column) {
  1427. // 按照行进行控制列
  1428. if (row && row.readOnly) {
  1429. let readOnly = row.readOnly
  1430. // console.log('eee',readOnly,column.field,readOnly.indexOf(column.field))
  1431. if (readOnly.indexOf(column.field) >= 0) {
  1432. return true;
  1433. }
  1434. }
  1435. return false;
  1436. },
  1437. /**
  1438. * @desc : 点击按钮
  1439. * @author : 周兴
  1440. * @date : 2023/3/26 16:11
  1441. */
  1442. buttonClick(colItem, rowIndex, row) {
  1443. this.$emit('button-click', row, rowIndex, colItem)
  1444. },
  1445. /**
  1446. * @desc : 控制跳转到下一个输入格
  1447. * @author : 周兴
  1448. * @date : 2022/4/9 13:40
  1449. */
  1450. nextInputCellByKeyDown(e) {
  1451. //如果点击回车需要进入下一个可输入单元格
  1452. if (e.keyCode === 13) {
  1453. //回车的时候阻止冒泡事件
  1454. e.stopPropagation();
  1455. //如果是点击过去的,就不用再走一次
  1456. if (e.type === 'keyup') return;
  1457. //跳转到下一个可输入单元格
  1458. this.nextInputCell(e.row, e.rowIndex, e.colIndex, e.column.field, e.column);
  1459. }
  1460. },
  1461. /**
  1462. * @desc : 控制数值的输入
  1463. * @author : 周兴
  1464. * @date : 2022/4/5 22:28
  1465. */
  1466. controlNumber(e) {
  1467. this.$emit('on-keydown', e)
  1468. //回车和tab的时候阻止冒泡事件
  1469. if (e.keyCode === 13) {
  1470. e.stopPropagation();
  1471. }
  1472. //控制跳转到下一个输入格
  1473. this.nextInputCellByKeyDown(e);
  1474. //backspace,delete,enter,左右不控制
  1475. if (e.keyCode === 8 || e.keyCode === 46 || e.keyCode === 37
  1476. || e.keyCode === 39 || e.keyCode === 13) {
  1477. return;
  1478. }
  1479. // 获取小数位数
  1480. let digits = 0;
  1481. if (e.column.digits) {
  1482. digits = e.column.digits;
  1483. } else {
  1484. //如果没有设置小数位数,那么就读取商品中的计量单位
  1485. if (e.row.decimalPlaces) {
  1486. digits = e.row.decimalPlaces;
  1487. }
  1488. }
  1489. //ctrl+v,ctrl+c不控制
  1490. if ((e.key === 'v' || e.key === 'c') && e.ctrlKey) {
  1491. return;
  1492. }
  1493. //查看是否有选中数据,再输入的情况
  1494. let selectedLength = e.target.selectionEnd - e.target.selectionStart;
  1495. if (selectedLength > 0) {
  1496. return;
  1497. }
  1498. let num = e.target.value + "" + e.key;
  1499. //说明是插入到字符串中的
  1500. if (e.target.selectionEnd < e.target.value.length) {
  1501. num = this.$libaray.insertStr(e.target.value, e.target.selectionEnd, e.key)
  1502. }
  1503. // let negativeFlag = e.column.negative ? true : false;
  1504. let negativeFlag = e.column.negative ? true : false;
  1505. if (!this.$libaray.getRegex(digits, negativeFlag).test(num)) {
  1506. e.preventDefault();
  1507. } else {
  1508. //如果有最大值,需要判断不要超过最大值
  1509. let max = e.column.max ? e.column.max : this.$config.maxNumber;
  1510. if (num >= parseFloat(max)) {
  1511. e.preventDefault();
  1512. }
  1513. // 如果有最小值,需要判断不小于最小值
  1514. let min = e.column.min ? e.column.min : this.$config.minNumber;
  1515. if (num <= parseFloat(min)) {
  1516. e.preventDefault();
  1517. }
  1518. }
  1519. },
  1520. /**
  1521. * @desc : 控制数值的输入
  1522. * @author : 周兴
  1523. * @date : 2022/4/5 22:28
  1524. */
  1525. handleKeyup(e) {
  1526. // 获取小数位数
  1527. let digits = 0;
  1528. if (e.column.digits) {
  1529. digits = e.column.digits;
  1530. } else {
  1531. //如果没有设置小数位数,那么就读取商品中的计量单位
  1532. if (e.row.decimalPlaces) {
  1533. digits = e.row.decimalPlaces;
  1534. }
  1535. }
  1536. //复制时调用改变事件
  1537. if (e.key === 'v' && e.ctrlKey) {
  1538. this.changeValueByCtrlV(e, digits);
  1539. }
  1540. },
  1541. /**
  1542. * @desc : 值改变
  1543. * @author : 周兴
  1544. * @date : 2022/4/10 14:21
  1545. */
  1546. changeValueByCtrlV(e, digits) {
  1547. let value = e.target.value
  1548. if (Number.isNaN(Number(value))) {
  1549. e.target.value = null;
  1550. this.tableData[e.rowIndex][e.column.field] = null;
  1551. } else {
  1552. //正则表达式去控制输入
  1553. if (!this.$libaray.getRegex(digits).test(value)) {
  1554. value = XEUtils.toFixed(XEUtils.round(value, digits), digits);
  1555. }
  1556. //判断是否超过最大值,如果超过最大值,就取最大值
  1557. let max = e.column.max ? e.column.max : this.$config.maxNumber;
  1558. if (parseFloat(value) >= max) {
  1559. //获取最大值
  1560. let maxValue = this.$libaray.getMaxValue(max, digits);
  1561. e.target.value = maxValue;
  1562. this.tableData[e.rowIndex][e.column.field] = maxValue;
  1563. } else {
  1564. e.target.value = value;
  1565. this.tableData[e.rowIndex][e.column.field] = value;
  1566. }
  1567. }
  1568. },
  1569. /**
  1570. * @desc : 格式化列
  1571. * @author : 周兴
  1572. * @date : 2022/4/4 13:40
  1573. */
  1574. formatColumn({column, cellValue, row}) {
  1575. //默认显示空值
  1576. if (cellValue === undefined || !column.params) {
  1577. return null;
  1578. }
  1579. let param = column.params
  1580. // 只有数值类型,或者是diable类型中数值的需要进行处理
  1581. if (param.type === 'number' || param.number) {
  1582. //获取小数位数
  1583. let digits = 0;
  1584. if (param.digits || param.digits == 0) {
  1585. digits = param.digits;
  1586. } else {
  1587. //如果没有设置小数位数,那么就读取商品中的计量单位
  1588. if (row.decimalPlaces) {
  1589. digits = row.decimalPlaces;
  1590. }
  1591. }
  1592. // 数值列开启千分位过滤 (默认开启千分位)
  1593. if (param.formatThousandth == undefined || param.formatThousandth) {
  1594. if (cellValue != null) {
  1595. //没有开启千分位
  1596. return XEUtils.commafy(Number(cellValue), {digits});
  1597. }
  1598. } else {
  1599. if (cellValue != null) {
  1600. //没有开启千分位
  1601. return XEUtils.toFixed(XEUtils.round(cellValue, digits), digits);
  1602. }
  1603. }
  1604. } else {
  1605. //日期
  1606. if (param.date && cellValue != null) {
  1607. return new Date(cellValue).format('yyyy-MM-dd');
  1608. } else {
  1609. // 如果不是数值,就直接返回
  1610. return cellValue;
  1611. }
  1612. }
  1613. }
  1614. ,
  1615. /**
  1616. * @desc : 合计行运算
  1617. * @author : 周兴
  1618. * @date : 2022/3/31 11:28
  1619. */
  1620. footerMethod({data}) {
  1621. //如果不开启合计行,那么就不走该方法
  1622. if (!this.showFooter) return;
  1623. let total = this.$t('totalSum');
  1624. const sums = []
  1625. if (sums.indexOf(total) < 0) {
  1626. sums.push(total)
  1627. }
  1628. let sumCell = null;
  1629. // this.$nextTick(() => {
  1630. for (let column of this.columns) {
  1631. sumCell = null;
  1632. if ((column.type === vm.$config.columnType.number
  1633. || column.number) && column.sum) {
  1634. let digits = 0;
  1635. //小数位数
  1636. if (column.digits) {
  1637. digits = column.digits;
  1638. }
  1639. data.forEach(item => {
  1640. sumCell += Number(item[column.field] == undefined ? 0 : item[column.field])
  1641. })
  1642. //格式化金额开启(默认开启)
  1643. if (column.formatThousandth == undefined || column.formatThousandth) {
  1644. sumCell = XEUtils.commafy(Number(sumCell), {digits})
  1645. }
  1646. }
  1647. sums.push(sumCell);
  1648. }
  1649. // })
  1650. return [sums]
  1651. }
  1652. ,
  1653. /**
  1654. * @desc : poper隐藏事件
  1655. * @author : 沈博
  1656. * @date : 2022/3/13 10:10
  1657. */
  1658. popperHide() {
  1659. this.inputDownValue = null
  1660. },
  1661. /**
  1662. * @desc : poper显示事件 TODO
  1663. * @author : 周兴
  1664. * @date : 2022/4/11 11:37
  1665. */
  1666. popperShow(colIndex, type) {
  1667. // console.log('field', this.$refs['pop_' + colIndex], this.columns)
  1668. this.$nextTick(() => {
  1669. if (!type) {
  1670. this.$refs['pop_' + colIndex][0].$refs.input.focus();
  1671. } else {
  1672. this.$refs['pop_' + colIndex][0].$refs.inputNumber.focus();
  1673. }
  1674. })
  1675. },
  1676. /**
  1677. * @desc : 获取删除的数据
  1678. * @author : 周兴
  1679. * @date : 2022/6/18 16:32
  1680. */
  1681. getDeleteData() {
  1682. let table = this.deleteData ? this.deleteData.copy() : []
  1683. return table;
  1684. },
  1685. /**
  1686. * @desc : 获取数据
  1687. * @author : 周兴
  1688. * @date : 2022/4/10 11:38
  1689. */
  1690. getTableData() {
  1691. let table = this.tableData ? this.tableData.copy() : []
  1692. return table;
  1693. },
  1694. /**
  1695. * @desc : 获取过滤掉主键数据没有选择的信息
  1696. * @author : 周兴
  1697. * @date : 2022/4/10 11:39
  1698. */
  1699. getTableDataFilter() {
  1700. let table = this.tableData ? this.tableData.copy() : []
  1701. if (this.controlId) {
  1702. table = table.filter(it => it[this.controlId]);
  1703. }
  1704. return table;
  1705. },
  1706. /**
  1707. * @desc : 获取改变的数据
  1708. * @author : 周兴
  1709. * @date : 2022/4/10 11:38
  1710. */
  1711. getTableChangeData() {
  1712. let table = this.tableDataChange ? this.tableDataChange.copy() : []
  1713. return table;
  1714. },
  1715. /**
  1716. * @desc : 清除掉变化数据
  1717. * @author : 周兴
  1718. * @date : 2022/4/21 17:24
  1719. */
  1720. clearTableChangeData() {
  1721. this.tableDataChange = [];
  1722. },
  1723. /**
  1724. * @desc : 清空数据
  1725. * @author : 周兴
  1726. * @date : 2022/4/21 17:24
  1727. */
  1728. clearTable() {
  1729. this.tableData = [];
  1730. this.tableDataChange = [];
  1731. if (this.operateFlag && this.newRowFlag) {
  1732. // 新增一行
  1733. this.add();
  1734. }
  1735. },
  1736. /**
  1737. * @desc : 文本输入后执行的事件
  1738. * @author : 周兴
  1739. * @date : 2022/4/6 13:52
  1740. */
  1741. editClosed(e) {
  1742. this.$emit('editClosed', e, this.tableData)
  1743. },
  1744. /**
  1745. * @desc : 点击行执行事件
  1746. * @author : 于继渤
  1747. * @date : 2022/4/21 13:43
  1748. */
  1749. currentChangeGetData(e) {
  1750. e.oldRowIndex = this.currentRowIndex;
  1751. this.$emit('current-change', e, this.tableData);
  1752. this.currentRowIndex = e.rowIndex;
  1753. },
  1754. /**
  1755. * @desc : 根据field获取
  1756. * @author : 周兴
  1757. * @date : 2022/6/16 8:44
  1758. */
  1759. getColumnByField(field) {
  1760. for (let i = 0; i < this.columns.length; i++) {
  1761. if (this.columns[i].field === field) {
  1762. return this.columns[i];
  1763. }
  1764. }
  1765. },
  1766. /**
  1767. * @desc : 根据field获取
  1768. * @author : 周兴
  1769. * @date : 2022/6/16 8:44
  1770. */
  1771. getColumnIndexByField(columns, field) {
  1772. for (let i = 0; i < columns.length; i++) {
  1773. if (columns[i].property === field) {
  1774. return i;
  1775. }
  1776. }
  1777. },
  1778. /**
  1779. * @desc : 跳转到下一个可输入单元格
  1780. * @author : 周兴
  1781. * @date : 2022/4/7 15:36
  1782. */
  1783. nextInputCell(row, rowIndex, columnIndex, field, colItem) {
  1784. //如果下拉选择数据没有选择,点回车光标不离开 用columnIndex去查不对,因为位置可能会移动,并且column的列也可能跟列表的顺序不一致
  1785. let filterColumn = this.getColumnByField(field);
  1786. if (filterColumn && filterColumn.controlId && row[field] === null) {
  1787. this.$refs[this.name].setActiveCell(this.tableData[rowIndex], field);
  1788. this.handleFocus(filterColumn, rowIndex);
  1789. return;
  1790. }
  1791. let visibleColumns = this.$refs[this.name].getTableColumn().visibleColumn;
  1792. this.isScroll = true;
  1793. let currentColumnIndex = columnIndex;
  1794. //如果是最后一列那么跳转到下一行
  1795. if (field === visibleColumns[visibleColumns.length - 1].field) {
  1796. rowIndex = parseInt(rowIndex) + 1
  1797. columnIndex = 0;
  1798. } else {
  1799. // 根据field后去列的顺序
  1800. columnIndex = this.getColumnIndexByField(visibleColumns, field) + 1;
  1801. }
  1802. //设置单元格激活状态
  1803. for (let i = rowIndex; i < this.tableData.length; i++) {
  1804. for (let j = columnIndex; j < visibleColumns.length; j++) {
  1805. filterColumn = this.getColumnByField(visibleColumns[j].field);
  1806. //列只读以及行单元格只读
  1807. if (filterColumn && filterColumn.type !== 'disabled' && filterColumn.type !== 'switch') {
  1808. // 不能从同一个controlId的列跳转到下一列
  1809. if (filterColumn.controlId && colItem.controlId
  1810. && filterColumn.controlId === colItem.controlId) {
  1811. continue;
  1812. } else {
  1813. // console.log('bbb',rowIndex,visibleColumns[j].field,filterColumn)
  1814. this.$refs[this.name].setActiveRow(this.tableData[i])
  1815. this.$refs[this.name].setCurrentRow(this.tableData[i])
  1816. this.$refs[this.name].setActiveCell(this.tableData[i], visibleColumns[j].field);
  1817. // 焦点落在各控件上
  1818. this.handleFocus(filterColumn, i);
  1819. }
  1820. // 如果是第一列,需要设置滚动,不然不会回到第一个列的位置
  1821. if (j === 0) {
  1822. this.$refs[this.name].scrollTo(0, 0);
  1823. }
  1824. // 延迟更新不然就会让 滚动先执行
  1825. setTimeout(() => {
  1826. this.isScroll = false;
  1827. }, 500)
  1828. return;
  1829. }
  1830. }
  1831. }
  1832. this.isScroll = false;
  1833. },
  1834. /**
  1835. * @desc : 单元格编辑结束后,光标停留在对应的位置
  1836. * @author : 周兴
  1837. * @date : 2022/4/10 14:02
  1838. */
  1839. handleFocus(column, rowIndex) {
  1840. setTimeout(() => {
  1841. // console.log(e)
  1842. if (column.type) {
  1843. switch (column.type) {
  1844. case 'date':
  1845. this.$refs[column.field + rowIndex][0].focus()
  1846. this.$refs[column.field + rowIndex][0].$refs['input'].focus()
  1847. break;
  1848. case 'switch':
  1849. this.$refs[column.field + rowIndex][0].$el.focus()
  1850. break;
  1851. // case 'select':
  1852. // console.log('res',column.field + rowIndex)
  1853. // // this.$refs[column.field + rowIndex][0].focus()
  1854. // this.$refs[column.field + rowIndex][0].isActivated = true
  1855. // this.$refs[column.field + rowIndex][0].$refs.input.focus()
  1856. // break;
  1857. case 'tableSelect':
  1858. case 'select':
  1859. if (this.$refs[column.field + rowIndex] && this.$refs[column.field + rowIndex].length > 0) {
  1860. if (this.$refs[column.field + rowIndex][0].$refs['input']) {
  1861. this.$refs[column.field + rowIndex][0].$refs['input'].$el.click()
  1862. this.$refs[column.field + rowIndex][0].$refs['input'].focus()
  1863. }
  1864. if (this.$refs[column.field + rowIndex][0].$refs['table']) {
  1865. this.$refs[column.field + rowIndex][0].$refs['table'].focus()
  1866. }
  1867. }
  1868. break;
  1869. }
  1870. }
  1871. }, 200)
  1872. },
  1873. /**
  1874. * @desc : 计算
  1875. * @author : 周兴
  1876. * @date : 2022/4/6 13:59
  1877. */
  1878. calc(field, rowIndex) {
  1879. // console.log('e', e)
  1880. //获取当前列的名
  1881. let currentColumn = this.columns.filter(col => col.field === field);
  1882. if (!currentColumn) return;
  1883. let formula = currentColumn[0].formula;
  1884. //如果没有公式就推出
  1885. if (!formula || !Object.keys(formula)) {
  1886. return;
  1887. }
  1888. let row = this.tableData[rowIndex]
  1889. let key = Object.keys(formula)[0];
  1890. //获取需要计算的列
  1891. let countColumn = this.columns.filter(col => col.field === key);
  1892. if (!countColumn) return;
  1893. let values = formula[key];
  1894. // console.log('dd1', values)
  1895. if (values && values.length > 0) {
  1896. let formu = values[0];
  1897. for (let i = 1; i < values.length; i++) {
  1898. if (row[values[i]]) {
  1899. formu = formu.replace(values[i], row[values[i]]);
  1900. } else {
  1901. // 如果不是非标,面积要进行替换为1
  1902. if (values[i] == 'nonStandardArea') {
  1903. formu = formu.replace('nonStandardArea', 1);
  1904. } else {
  1905. formu = formu.replace(values[i], 0);
  1906. }
  1907. // formu = formu.replace(values[i], 0);
  1908. }
  1909. }
  1910. try {
  1911. //用eval进行计算
  1912. let tValue = eval(formu)
  1913. //说明除数为0,那么金额清空
  1914. if ('Infinity' == tValue || '-Infinity' == tValue || isNaN(tValue)) {
  1915. // row[key] = null;
  1916. } else {
  1917. // 获取小数位数
  1918. let digits = 0;
  1919. if (countColumn[0].digits) {
  1920. digits = countColumn[0].digits
  1921. }
  1922. // console.log('countColumn',countColumn,digits)
  1923. //对计算出来的数据进行处理
  1924. row[key] = XEUtils.toFixed(XEUtils.round(tValue, digits), digits);
  1925. }
  1926. } catch (err) {
  1927. // row[key] = 0
  1928. }
  1929. }
  1930. },
  1931. /**
  1932. * @desc : 表格向下{{ $t('filling') }}
  1933. * @author : 沈博
  1934. * @date : 2022/3/13 9:56
  1935. */
  1936. inputDown(column) {
  1937. let field = column.field
  1938. //获取当前列的名
  1939. let currentColumn = this.columns.filter(col => col.field === field);
  1940. if (!currentColumn) return;
  1941. //console.log('d',currentColumn,column.type)
  1942. // 数值
  1943. if (column.type === 'number') {
  1944. let formula = currentColumn[0].formula;
  1945. //如果没有公式就推出
  1946. if (formula && Object.keys(formula)) {
  1947. let key = Object.keys(formula)[0];
  1948. //获取需要计算的列
  1949. let countColumn = this.columns.filter(col => col.field === key);
  1950. if (countColumn) {
  1951. let values = formula[key];
  1952. if (values && values.length > 0) {
  1953. let formu = values[0];
  1954. //给你当前单元格赋值
  1955. this.tableData.forEach(it => {
  1956. // 如果有controlId,那么只能赋值有值的行
  1957. if (this.controlId && it[this.controlId]) {
  1958. formu = values[0];
  1959. for (let i = 1; i < values.length; i++) {
  1960. if (it[values[i]]) {
  1961. formu = formu.replace(values[i], it[values[i]]);
  1962. } else {
  1963. formu = formu.replace(values[i], 0);
  1964. }
  1965. }
  1966. try {
  1967. //用eval进行计算
  1968. let tValue = eval(formu)
  1969. //说明除数为0,那么金额清空
  1970. if ('Infinity' == tValue) {
  1971. this.$set(it, field, null)
  1972. } else {
  1973. // 获取小数位数
  1974. let digits = 0;
  1975. if (countColumn[0].digits) {
  1976. digits = countColumn[0].digits
  1977. } else {
  1978. //如果没有设置小数位数,那么就读取商品中的计量单位
  1979. if (it.decimalPlaces) {
  1980. digits = it.decimalPlaces;
  1981. }
  1982. }
  1983. //对计算出来的数据进行处理
  1984. this.$set(it, key, XEUtils.toFixed(XEUtils.round(tValue, digits), digits))
  1985. }
  1986. } catch (err) {
  1987. this.$set(it, key, 0)
  1988. }
  1989. }
  1990. })
  1991. }
  1992. }
  1993. } else {
  1994. //给你当前单元格赋值
  1995. // 获取小数位数
  1996. let digits = 0;
  1997. if (currentColumn[0].digits) {
  1998. digits = currentColumn[0].digits
  1999. }
  2000. this.tableData.forEach(it => {
  2001. // 如果有controlId,那么只能赋值有值的行
  2002. if ((this.controlId && it[this.controlId]) || !this.controlId) {
  2003. //如果没有设置小数位数,那么就读取商品中的计量单位
  2004. if (digits === 0) {
  2005. digits = it.decimalPlaces;
  2006. }
  2007. this.$set(it, field, XEUtils.toFixed(XEUtils.round(this.inputDownValue, digits), digits))
  2008. }
  2009. })
  2010. }
  2011. } else if (column.type === 'text') {
  2012. //文本
  2013. this.tableData.forEach(it => {
  2014. // 如果有controlId,那么只能赋值有值的行
  2015. if ((this.controlId && it[this.controlId]) || !this.controlId) {
  2016. this.$set(it, field, this.inputDownValue);
  2017. }
  2018. })
  2019. } else if (column.type === 'switch') {
  2020. this.tableData.forEach(it => {
  2021. // 如果有controlId,那么只能赋值有值的行
  2022. if ((this.controlId && it[this.controlId]) || !this.controlId) {
  2023. this.$set(it, field, this.inputDownBooleanValue);
  2024. }
  2025. })
  2026. } else if (column.type === 'select') {
  2027. this.tableData.forEach(it => {
  2028. // 如果有controlId,那么只能赋值有值的行
  2029. if ((this.controlId && it[this.controlId]) || !this.controlId) {
  2030. this.$set(it, field, this.inputDownValue);
  2031. let list = typeof column.options === 'function' ? column.options() : column.options
  2032. this.$set(it, field + '_Name', list.filter(it => it[column.valueKey] === this.inputDownValue)[0][column.labelKey]);
  2033. }
  2034. })
  2035. }
  2036. this.$refs['down-input-icon'][0].$el.click()
  2037. }
  2038. ,
  2039. /**
  2040. * @desc : 新增一行
  2041. * @author : 周兴
  2042. * @date : 2022/4/7 14:36
  2043. */
  2044. add(rowIndex) {
  2045. let obj = {};
  2046. this.columnsData.forEach(it => {
  2047. //开关时,默认是true,如果要是false需要在列中给其传值
  2048. if (it.type === 'switch') {
  2049. this.$set(obj, it.field, it.value === undefined || it.value === null ? true : it.value);
  2050. if (this.defaultField && it.field == this.defaultField) {
  2051. this.$set(obj, it.field, false);
  2052. }
  2053. } else {
  2054. // 如果是显示顺序需要自增
  2055. if (it.field === 'displayNo') {
  2056. let arr = this.tableData.map(m => m[it.field]);
  2057. let cMax = Math.max.apply(null, arr);
  2058. if (cMax && cMax != '-Infinity') {
  2059. this.$set(obj, it.field, cMax + 1);
  2060. } else {
  2061. this.$set(obj, it.field, 1);
  2062. }
  2063. } else {
  2064. //读取列的默认值,如果有设置,那么需要设置上
  2065. if (it.value && it.value != null) {
  2066. if (it.type === 'select') {
  2067. setTimeout(() => {
  2068. let options = it.options();
  2069. let filters = options.filter(item => item[it.valueKey] === it.value);
  2070. if (filters && filters.length > 0) {
  2071. this.$set(obj, it.field, it.value);
  2072. this.$set(obj, it.field + '_Name', filters[0][it.labelKey]);
  2073. }
  2074. }, rowIndex ? 0 : 300)
  2075. } else {
  2076. this.$set(obj, it.field, it.value);
  2077. }
  2078. } else {
  2079. this.$set(obj, it.field, null);
  2080. }
  2081. }
  2082. }
  2083. })
  2084. //说明是手动新增的
  2085. let curorRowIndex = 0;
  2086. if (rowIndex || rowIndex === 0) {
  2087. this.tableData.splice(rowIndex + 1, 0, obj)
  2088. curorRowIndex = rowIndex + 1;
  2089. } else {
  2090. this.tableData.push(obj)
  2091. curorRowIndex = 0;
  2092. }
  2093. //设置光标位置
  2094. if (this.focusFlag) {
  2095. this.$nextTick(() => {
  2096. for (let j in this.columnsData) {
  2097. if (this.columnsData[j].type !== 'disabled' && this.columnsData[j].type !== 'switch' &&
  2098. (!this.tableData[curorRowIndex].readOnly || this.tableData[curorRowIndex].readOnly.indexOf(this.columnsData[j].field) < 0)) {
  2099. this.$refs[this.name].setActiveCell(this.tableData[curorRowIndex], this.columnsData[j].field);
  2100. // console.log('ttt',this.focusFlag,)
  2101. this.handleFocus(this.columnsData[j], curorRowIndex);
  2102. break;
  2103. }
  2104. }
  2105. })
  2106. }
  2107. this.$emit('addRow', rowIndex)
  2108. },
  2109. /**
  2110. * @desc : 删除一行
  2111. * @author : 周兴
  2112. * @date : 2022/4/11 9:13
  2113. */
  2114. del(rowIndex) {
  2115. // console.log('del',rowIndex)
  2116. let row = {...this.tableData[rowIndex]}
  2117. // 记录删除的数据
  2118. if (this.majorField && row[this.majorField]) {
  2119. this.deleteData.push(row);
  2120. }
  2121. this.tableData = [...this.tableData.filter((it, index) => {
  2122. return index !== rowIndex
  2123. })]
  2124. //如果删完了,那么就要新增新行
  2125. if (this.operateFlag && this.newRowFlag && (!this.tableData || this.tableData.length === 0)) {
  2126. this.add(0);
  2127. }
  2128. this.$refs[this.name].reloadData(this.tableData)
  2129. this.$emit('delRow', row, rowIndex)
  2130. this.setDefaultFlag(row, true);
  2131. },
  2132. /**
  2133. * @desc : TableSelect输入框离开焦点事件
  2134. * @author : 周兴
  2135. * @date : 2022/5/26 16:40
  2136. */
  2137. handleSelectInputBlur(e, row, rowIndex, colItem, colIndex) {
  2138. // 把值恢复回去
  2139. if (this.$refs[colItem.field + rowIndex] && row[colItem.field]) {
  2140. this.$refs[colItem.field + rowIndex][0].$refs.input.$refs.input.value = row[colItem.field];
  2141. }
  2142. },
  2143. /**
  2144. * @desc : 清空选择
  2145. * @author : 周兴
  2146. * @date : 2022/9/23 17:39
  2147. */
  2148. clearSelect(e) {
  2149. if (this.tableData && this.tableData[e.rowIndex] && this.tableData[e.rowIndex][e.column.field]) {
  2150. this.tableData[e.rowIndex][e.column.field] = null;
  2151. this.tableData[e.rowIndex][e.column.field + '_Name'] = null;
  2152. }
  2153. this.$emit('clear-select', e)
  2154. },
  2155. /**
  2156. * @desc : desc
  2157. * @author : 周兴
  2158. * @date : 2022/4/10 14:22
  2159. */
  2160. inputChange(row, rowIndex, colItem, val) {
  2161. // 更新需要更新的列
  2162. this.columns.forEach((it, index) => {
  2163. //如果这里是controlId
  2164. if (this.controlId) {
  2165. if (colItem.type === 'tableSelect') {
  2166. if (colItem.controlId && it.field == colItem.field && this.controlId === colItem.controlId) {
  2167. this.tableData[rowIndex][it.field] = null;
  2168. }
  2169. }
  2170. } else {
  2171. //说明这列受管控
  2172. if (it.controlIds && it.controlIds.length > 0 && it.controlIds.includes(colItem.field)) {
  2173. this.tableData[rowIndex][it.field] = null;
  2174. if (this.tableData[rowIndex][it.field + '_Name']) {
  2175. this.tableData[rowIndex][it.field + '_Name'] = null;
  2176. }
  2177. }
  2178. }
  2179. })
  2180. this.$emit('changeValue', colItem.field, row, rowIndex, val);
  2181. },
  2182. /**
  2183. * @desc : 批量选择数据
  2184. * @author : 周兴
  2185. * @date : 2022/5/26 16:54
  2186. */
  2187. onBatchChoose(e, column, rowIndex, colIndex, fieldUpdate, colItem) {
  2188. // 单选
  2189. if (colItem.multiple !== undefined && !colItem.multiple) {
  2190. this.onChoose(e.batchRows[0], e.batchRows[0], column, rowIndex, colIndex, fieldUpdate, colItem)
  2191. } else {
  2192. // 多选
  2193. let {batchRows, batchKeys} = e;
  2194. if (batchRows && batchRows.length > 0) {
  2195. let deleteCurrentFlag = false;
  2196. let index = 0;
  2197. for (let i = 0; i < batchRows.length; i++) {
  2198. // 只有控制主键的列才会这么操作
  2199. if (colItem.controlId) {
  2200. // 判断是否重复
  2201. this.checkRepeat(batchRows[i], batchRows[i], rowIndex, colItem, false).then(isRepeat => {
  2202. // 如果重复就不进行后续
  2203. if (isRepeat) {
  2204. //只要重复就不允许删除当前行了
  2205. deleteCurrentFlag = true;
  2206. // index = index + 1;
  2207. } else {
  2208. // 如果第一行是新行,那么就要把deleteCurrentFlag设置为true,不然第二行就要删除掉之前的数据
  2209. if (i === 0 && this.controlId && !this.tableData[rowIndex][this.controlId]) {
  2210. deleteCurrentFlag = true;
  2211. }
  2212. //如果当前行有值,先删除当前行 (当前行只能删除一次)
  2213. if (!deleteCurrentFlag && this.controlId && this.tableData[rowIndex][this.controlId]) {
  2214. // 记录删除的数据
  2215. if (this.majorField && colItem.field == this.majorField && this.tableData[rowIndex][this.majorField]) {
  2216. this.deleteData.push(this.tableData[rowIndex]);
  2217. }
  2218. //删除当前数据
  2219. this.tableData = this.tableData.filter((it, cIndex) => {
  2220. return cIndex !== rowIndex
  2221. })
  2222. deleteCurrentFlag = true;
  2223. }
  2224. //新增行(判断行是否是新行,新行要新增
  2225. this.addNewRow(rowIndex + index);
  2226. // 设置需要更新的数据
  2227. this.setTableData(batchRows[i], batchRows[i], rowIndex + index, colItem, fieldUpdate);
  2228. index = index + 1;
  2229. }
  2230. });
  2231. }
  2232. }
  2233. if (this.autoNextFlag) {
  2234. // 跳转到第一行的可输入单元格
  2235. this.nextInputCell(batchRows[0], rowIndex, colIndex, colItem.field, colItem);
  2236. }
  2237. // this.$emit('onBatchChoose', batchRows, rowIndex)
  2238. this.$emit('chooseData', batchRows, rowIndex, colItem)
  2239. }
  2240. }
  2241. },
  2242. /**
  2243. * @desc : 批量赋值
  2244. * @author : 周兴
  2245. * @date : 2022/11/15 13:04
  2246. */
  2247. onBatchChooseCopy(e, colItem, type) {
  2248. let row = type === 'batch' ? e.batchRows[0] : e
  2249. this.tableData.forEach(it => {
  2250. colItem.fieldUpdate.forEach(it2 => {
  2251. if (it[this.controlId]) {
  2252. this.$set(it, it2.updateField, row[it2.valueFiled])
  2253. }
  2254. })
  2255. })
  2256. },
  2257. /**
  2258. * @desc : 增加新行(【批量选择数据后增加】)
  2259. * @author : 周兴
  2260. * @date : 2022/5/27 9:13
  2261. */
  2262. addNewRow(rowIndex) {
  2263. // console.log('ttt',rowIndex)
  2264. if (!this.tableData[rowIndex]) {
  2265. this.add(rowIndex);
  2266. } else {
  2267. // 如果多行增加时,这行有值,需要增加
  2268. if (this.controlId && this.tableData[rowIndex][this.controlId]) {
  2269. this.add(rowIndex);
  2270. }
  2271. }
  2272. },
  2273. /**
  2274. * @desc : 选择数据
  2275. * @author : 周兴
  2276. * @date : 2022/4/7 16:02
  2277. */
  2278. onChoose(e, row, column, rowIndex, columnIndex, fieldUpdate, colItem) {
  2279. this.chooseData(e, row, column, rowIndex, columnIndex, fieldUpdate, colItem, true);
  2280. setTimeout(() => {
  2281. if (this.autoNextFlag) {
  2282. this.$refs[this.name].clearActived()
  2283. // 跳转到下一个可输入单元格
  2284. this.nextInputCell(row, rowIndex, columnIndex, colItem.field, colItem);
  2285. }
  2286. }, 300)
  2287. },
  2288. /**
  2289. * @desc : 设置默认标识
  2290. * @author : 周兴
  2291. * @date : 2022/8/5 9:28
  2292. */
  2293. setDefaultFlag(row, deleteFlag) {
  2294. if (this.defaultField && row) {
  2295. // 删除
  2296. if (deleteFlag) {
  2297. // 如果删除的是默认行,那么需要指定第一行为默认
  2298. if (row[this.defaultField] && this.tableData && this.tableData.length > 0) {
  2299. this.$set(this.tableData[0], this.defaultField, true);
  2300. }
  2301. } else {
  2302. // 判断是否有标识已经勾选上了
  2303. let filterRows = this.tableData.filter(it => it[this.defaultField]);
  2304. if (!filterRows || filterRows.length == 0) {
  2305. this.$set(row, this.defaultField, true);
  2306. } else {
  2307. this.$set(row, this.defaultField, false);
  2308. }
  2309. }
  2310. }
  2311. },
  2312. /**
  2313. * @desc : 选择数据(私有方法)
  2314. * @author : 周兴
  2315. * @date : 2022/5/26 16:39
  2316. */
  2317. chooseData(e, row, column, rowIndex, columnIndex, fieldUpdate, colItem, showMessage) {
  2318. let eRow = e == null ? row : e;
  2319. // 判断是否重复
  2320. this.checkRepeat(eRow, row, rowIndex, colItem, showMessage).then(res => {
  2321. if (res) {
  2322. return;
  2323. }
  2324. //记录删除的行
  2325. if (this.majorField && colItem.field == this.majorField && row[this.majorField]) {
  2326. this.deleteData.push(row);
  2327. }
  2328. // 设置需要更新的数据
  2329. this.setTableData(eRow, row, rowIndex, colItem, fieldUpdate);
  2330. this.$emit('chooseData', row, rowIndex, colItem)
  2331. });
  2332. },
  2333. /**
  2334. * @desc : 设置需要更新的数据
  2335. * @author : 周兴
  2336. * @date : 2022/5/27 8:48
  2337. */
  2338. setTableData(eRow, row, rowIndex, colItem, fieldUpdate) {
  2339. //说明是下拉,需要赋值
  2340. if (colItem.valueKey) {
  2341. //先把labelKey赋值给field,方便显示
  2342. if (eRow[colItem.valueKey]) {
  2343. this.$set(this.tableData[rowIndex], colItem.field, eRow[colItem.valueKey].toString())
  2344. this.$set(this.tableData[rowIndex], colItem.field + '_Name', eRow[colItem.labelKey])
  2345. // this.tableData[rowIndex][colItem.field] = eRow[colItem.valueKey].toString();
  2346. // this.tableData[rowIndex][colItem.field + '_Name'] = eRow[colItem.labelKey];
  2347. }
  2348. } else {
  2349. // 更新需要更新的列
  2350. if (fieldUpdate) {
  2351. fieldUpdate.forEach(it => {
  2352. this.$set(this.tableData[rowIndex], it.updateField, eRow[it.valueFiled])
  2353. // this.tableData[rowIndex][it.updateField] = eRow[it.valueFiled]
  2354. })
  2355. }
  2356. // 设置默认值
  2357. this.setDefaultFlag(this.tableData[rowIndex]);
  2358. }
  2359. // 给id赋值
  2360. // this.tableData[rowIndex]['id'] = eRow['id']
  2361. //把改变的行加入到tableDataChange中
  2362. let rows = this.tableDataChange.filter(it => it['_X_ID'] === row['_X_ID']);
  2363. //说明已经存在
  2364. if (rows && rows.length > 0) {
  2365. this.tableDataChange = this.tableDataChange.filter(it => it['_X_ID'] !== row['_X_ID']).map(it => it);
  2366. }
  2367. this.tableDataChange.push(row);
  2368. },
  2369. /**
  2370. * @desc : 判断是否重复
  2371. * @author : 周兴
  2372. * @date : 2022/5/27 8:46
  2373. */
  2374. checkRepeat(eRow, row, rowIndex, colItem, showMessage) {
  2375. return new Promise(resolve => {
  2376. //如果需要进行唯一性判断
  2377. if (!this.enabledRepeat && this.enabledRepeatId) {
  2378. //不是数组
  2379. if (!Array.isArray(this.enabledRepeatId)) {
  2380. //判断是否有重复的数据
  2381. let tmpRows = this.tableData.filter(it => it[this.controlId] == eRow[this.enabledRepeatId]);
  2382. // console.log('3343', this.enabledRepeat, this.tableData, this.enabledRepeatId, eRow)
  2383. if (tmpRows && tmpRows.length > 0) {
  2384. //判断是否是同一行的
  2385. if (row['_X_ID'] != tmpRows[0]['_X_ID']) {
  2386. //提示信息重复
  2387. if (showMessage) {
  2388. this.$Message.error(vm.$t('W_018'))
  2389. }
  2390. resolve(true);
  2391. }
  2392. }
  2393. } else {
  2394. let data = [...this.tableData];
  2395. let currentRow = data[rowIndex];
  2396. for (let col = 0; col < this.enabledRepeatId.length; col++) {
  2397. // for (let col in this.enabledRepeatId) {
  2398. data = this.filterData(data, eRow, this.enabledRepeatId[col], currentRow, colItem);
  2399. // 说明没有重复
  2400. if (!data || data.length === 0) {
  2401. break;
  2402. }
  2403. }
  2404. if (data && data.length > 0) {
  2405. //提示信息重复
  2406. if (showMessage) {
  2407. this.$Message.error(vm.$t('W_018'))
  2408. }
  2409. resolve(true);
  2410. }
  2411. }
  2412. }
  2413. resolve(false);
  2414. })
  2415. },
  2416. /**
  2417. * @desc : 过滤数据
  2418. * @author : 周兴
  2419. * @date : 2022/5/13 16:27
  2420. */
  2421. filterData(data, eRow, col, currentRow, column) {
  2422. //说明是当前列,当前列从下拉中取值
  2423. if (column.field === col) {
  2424. if (column.valueKey) {
  2425. return data.filter(it => it[col] == eRow[column.valueKey]);
  2426. }
  2427. } else {
  2428. if (currentRow && currentRow[col]) {
  2429. return data.filter(it => it[col] == currentRow[col] && it['_X_ID'] != currentRow['_X_ID']);
  2430. }
  2431. }
  2432. return null;
  2433. },
  2434. /**
  2435. * @desc : 校验数据
  2436. * @author : 周兴
  2437. * @date : 2022/4/7 16:02
  2438. */
  2439. validCheck() {
  2440. return new Promise((resolve, reject) => {
  2441. const $table = this.$refs[this.name]
  2442. $table.fullValidate(true).then(() => {
  2443. resolve(true)
  2444. }).catch(errMap => {
  2445. resolve(false)
  2446. })
  2447. })
  2448. },
  2449. /**
  2450. * @desc : 日期格式
  2451. * @author : 周兴
  2452. * @date : 2022/4/24 17:13
  2453. */
  2454. dateFormat(val) {
  2455. if (!val) {
  2456. return ''
  2457. }
  2458. return val.format('yyyy-MM-dd')
  2459. },
  2460. /**
  2461. * @desc : 单元格编辑结束后,光标停留在对应的位置
  2462. * @author : 周兴
  2463. * @date : 2022/4/10 14:02
  2464. */
  2465. editMethod(e) {
  2466. },
  2467. /**
  2468. * @desc : 滚动就隐藏pop
  2469. * @author : 周兴
  2470. * @date : 2022/4/19 9:21
  2471. */
  2472. handleScroll(e) {
  2473. if (!this.isScroll) {
  2474. if (e.isY) {
  2475. this.$refs[this.name].clearActived(); //关闭行激活状态
  2476. }
  2477. this.$refs[this.name].$el.click(); //关闭表头的pop
  2478. }
  2479. },
  2480. /**
  2481. * @desc : 移动行
  2482. * @author : 周兴
  2483. * @date : 2022/12/15 16:28
  2484. */
  2485. rowDrop() {
  2486. this.$nextTick(() => {
  2487. let xTable = this.$refs[this.name];
  2488. this.sortable = Sortable.create(
  2489. xTable.$el.querySelector(".body--wrapper>.vxe-table--body tbody"),
  2490. {
  2491. handle: ".vxe-body--row",
  2492. animation: 150,
  2493. onEnd: ({newIndex, oldIndex}) => {
  2494. this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0]);
  2495. let newArray = this.tableData.slice(0);
  2496. this.tableData = [];
  2497. this.$nextTick(function () {
  2498. this.tableData = newArray;
  2499. });
  2500. },
  2501. }
  2502. );
  2503. });
  2504. },
  2505. /**
  2506. * @desc : 表格设置
  2507. * @author : 周兴
  2508. * @date : 2022/12/1 15:11
  2509. */
  2510. showSetting(e) {
  2511. const tableColumns = this.$refs[this.name].getTableColumn()
  2512. if (tableColumns) {
  2513. let columns = tableColumns.fullColumn
  2514. columns = columns.filter(it => it.type != 'seq' && it.type != 'blank' && it.type != 'info')
  2515. this.$TableFilter({tableData: columns},
  2516. {
  2517. ok: (val) => {
  2518. // this.unFreeze();
  2519. // 去掉选择的标题
  2520. let filterRows = val.filter(it => it.type == 'checkbox' || it.type == 'radio');
  2521. if (filterRows && filterRows.length > 0) {
  2522. filterRows.forEach(it => {
  2523. it.title = '';
  2524. })
  2525. }
  2526. // 加入序号
  2527. val.splice(0, 0, tableColumns.fullColumn[0])
  2528. // 加入校验信息
  2529. val.splice(1, 0, tableColumns.fullColumn[1])
  2530. // 加入blank
  2531. val.push(tableColumns.fullColumn[tableColumns.fullColumn.length - 1])
  2532. // 加载列
  2533. this.$refs[this.name].loadColumn(val);
  2534. },
  2535. })
  2536. }
  2537. },
  2538. /**
  2539. * @desc : 根据requiredColumn进行validRules设置
  2540. * @author : 周兴
  2541. * @date : 2022/4/25 15:13
  2542. */
  2543. setRequiredColumn() {
  2544. if (this.requiredColumn && this.requiredColumn.length > 0) {
  2545. let validRules = {}
  2546. this.requiredColumn.forEach(col => {
  2547. let tempCol = this.columns.filter(it => it.field === col);
  2548. if (tempCol) {
  2549. validRules[col] = [{
  2550. required: true,
  2551. // message: vm.$t('inputWords', {'search-name': tempCol[0].title ? tempCol[0].title : vm.$t(tempCol[0].field)})
  2552. }]
  2553. }
  2554. })
  2555. this.validRules = validRules;
  2556. }
  2557. },
  2558. /**
  2559. * @desc : 刷新表的列
  2560. * @author : 周兴
  2561. * @date : 2023/3/17 14:41
  2562. */
  2563. refreshTableColumns(columns) {
  2564. this.columnsData = []
  2565. //如果列中没有序号要加上
  2566. let filterColumns = columns.filter(it => it.field === 'seq');
  2567. if (!filterColumns || filterColumns.length === 0) {
  2568. columns.splice(0, 0, {type: 'seq', field: 'seq', fixed: 'left'})
  2569. columns.splice(1, 0, {type: 'info', field: 'info', fixed: 'left', width: '15'})
  2570. columns.splice(0, 0, {type: 'blank', field: 'blank', width: '1'})
  2571. }
  2572. this.columnsData = columns;
  2573. },
  2574. /**
  2575. * @desc : 初始化列
  2576. * @author : 周兴
  2577. * @date : 2022/6/11 10:42
  2578. */
  2579. initColumn() {
  2580. let columns = [...this.columns]
  2581. //如果列中没有序号要加上
  2582. let filterColumns = columns.filter(it => it.field === 'seq');
  2583. if (!filterColumns || filterColumns.length === 0) {
  2584. columns.splice(0, 0, {type: 'seq', field: 'seq', fixed: 'left'})
  2585. columns.splice(1, 0, {type: 'info', field: 'info', fixed: 'left', width: '15'})
  2586. columns.splice(0, 0, {type: 'blank', field: 'blank', width: '1'})
  2587. }
  2588. this.columnsData = columns;
  2589. },
  2590. /**
  2591. * @desc : 固定列
  2592. * @author : 周兴
  2593. * @date : 2022/6/8 20:29
  2594. */
  2595. toggleFixedColumn() {
  2596. const xTable = this.$refs[this.name]
  2597. const tableColumns = xTable.getTableColumn()
  2598. if (tableColumns && tableColumns.visibleColumn
  2599. && tableColumns.visibleColumn.length > 0) {
  2600. let refreshFlag = false;
  2601. for (let i in this.columnsData) {
  2602. // 查看是否有列设置了固定
  2603. if (this.columnsData[i].fixed) {
  2604. let col = tableColumns.visibleColumn.filter(it => it.property === this.columnsData[i].field)
  2605. if (col && col.length > 0) {
  2606. col[0].fixed = this.columnsData[i].fixed;
  2607. refreshFlag = true;
  2608. }
  2609. }
  2610. }
  2611. if (refreshFlag) {
  2612. // 刷新列
  2613. xTable.refreshColumn();
  2614. }
  2615. }
  2616. },
  2617. /**
  2618. * @desc : 改变设置的位置
  2619. * @author : 周兴
  2620. * @date : 2023/1/29 16:55
  2621. */
  2622. changeSettingLocation() {
  2623. let self = this;
  2624. this.$nextTick(() => {
  2625. setTimeout(() => {
  2626. self.settingLeft = self.$refs[self.name] ? (self.$refs[self.name].$el.offsetWidth + self.$refs[self.name].$el.offsetLeft - 26) + 'px' : 0// 设置图标居左的位置
  2627. self.settingTop = self.$refs[self.name] ? (self.$refs[self.name].$el.offsetTop + 1) + 'px' : 0; // 设置图标居上的位置
  2628. }, 700)
  2629. })
  2630. },
  2631. },
  2632. mounted() {
  2633. let self = this;
  2634. setTimeout(() => {
  2635. self.toggleFixedColumn(); // 冻结列
  2636. }, 300)
  2637. self.rowDrop(); //行移动
  2638. //监听窗口变化
  2639. window.addEventListener('resize', function (e) {
  2640. setTimeout(() => {
  2641. self.settingLeft = self.$refs[self.name] ? (self.$refs[self.name].$el.offsetWidth + self.$refs[self.name].$el.offsetLeft - 26) + 'px' : 0// 设置图标居左的位置
  2642. self.settingTop = self.$refs[self.name] ? (self.$refs[self.name].$el.offsetTop + 1) + 'px' : 0; // 设置图标居上的位置
  2643. }, 300)
  2644. })
  2645. self.handleSelectData()
  2646. self.changeSettingLocation();
  2647. },
  2648. beforeDestroy() {
  2649. if (this.sortable) {
  2650. this.sortable.destroy();
  2651. }
  2652. },
  2653. created() {
  2654. // 初始化列
  2655. this.initColumn();
  2656. this.tableData = this.data;
  2657. //设置新行
  2658. if (!this.data || this.data.length === 0 || this.newRowAlwaysFlag) {
  2659. this.tableData = []
  2660. this.tableDataChange = []
  2661. if (this.newRowFlag) {
  2662. //新建时增加行
  2663. this.add()
  2664. }
  2665. // 聚焦到第一行
  2666. if (this.autoFocus) {
  2667. setTimeout(() => {
  2668. this.$refs[this.name]?.setActiveRow(this.tableData[0])
  2669. }, 300)
  2670. }
  2671. }
  2672. // 根据列表是否只读,判断是否开启编辑模式
  2673. if (this.readonly) {
  2674. this.enabledEdit = false;
  2675. }
  2676. // 设置必须输入项
  2677. this.setRequiredColumn();
  2678. this.freezeData = this.freeze;
  2679. }
  2680. }
  2681. </script>
  2682. <style scoped>
  2683. /deep/ .vxe-cell {
  2684. display: flex;
  2685. align-items: center;
  2686. padding-left: 0 !important;
  2687. padding-right: 0 !important;
  2688. }
  2689. /deep/ .vxe-cell--label {
  2690. padding-left: 10px !important;
  2691. padding-right: 10px !important;
  2692. }
  2693. .edit-div-class {
  2694. padding-left: 10px !important;
  2695. padding-right: 10px !important;
  2696. overflow: hidden !important;
  2697. text-overflow: ellipsis !important;
  2698. white-space: nowrap !important;
  2699. }
  2700. /deep/ .vxe-footer--row .vxe-cell {
  2701. justify-content: left !important;
  2702. }
  2703. /deep/ .vxe-input {
  2704. width: 100%;
  2705. border: 1px solid #dcdee2;
  2706. border-radius: 2px;
  2707. }
  2708. /deep/ .vxe-input :hover {
  2709. border: 1px solid #57a3f3 !important;
  2710. border-radius: 2px;
  2711. }
  2712. /deep/ .vxe-input > input:focus {
  2713. border-color: #57a3f3 !important;
  2714. border-radius: 2px;
  2715. -webkit-box-shadow: 0 0 0 1px rgba(45, 140, 240, .2) !important;
  2716. box-shadow: 0 0 0 1px rgba(45, 140, 240, .2) !important;
  2717. }
  2718. /deep/ .col--valid-error .vxe-input > input {
  2719. border-color: red !important;
  2720. }
  2721. /deep/ .vxe-cell--title {
  2722. line-height: 0;
  2723. }
  2724. /*
  2725. * @desc : TODO 不好使
  2726. * @author : 周兴
  2727. * @date : 2022/4/5 16:35
  2728. */
  2729. .number-class .vxe-cell {
  2730. justify-content: right !important;
  2731. }
  2732. /deep/ .vxe-input--number-prev-icon {
  2733. display: none;
  2734. }
  2735. /deep/ .vxe-input--number-next-icon {
  2736. display: none;
  2737. }
  2738. /deep/ .vxe-input .vxe-input--extra-suffix {
  2739. display: none;
  2740. }
  2741. .setting-class {
  2742. position: absolute;
  2743. z-index: 300;
  2744. width: 25px;
  2745. height: 28px;
  2746. background: #F8F8F9;
  2747. justify-content: center;
  2748. display: flex;
  2749. align-items: center;
  2750. cursor: pointer;
  2751. }
  2752. .freeze-class {
  2753. margin-top: 4px;
  2754. margin-left: 5px;
  2755. cursor: pointer;
  2756. font-size: 14px;
  2757. color: #c0c4cc;
  2758. }
  2759. .freezed-class {
  2760. margin-top: 4px;
  2761. margin-left: 5px;
  2762. cursor: pointer;
  2763. font-size: 14px;
  2764. }
  2765. .table-title {
  2766. padding: 4px 0 4px 0;
  2767. font-size: 14px;
  2768. font-weight: bold;
  2769. }
  2770. .edit-button {
  2771. border-color: #57a3f3 !important;
  2772. }
  2773. </style>