byTable.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div class="by-table">
  3. <vxe-table
  4. ref="table"
  5. :data="value"
  6. :size="attrs.size?attrs.size:'small'"
  7. resizable
  8. :height="attrs.height"
  9. :max-height="attrs.maxHeight"
  10. :stripe="attrs.stripe"
  11. :border="attrs.border"
  12. :align="attrs.align"
  13. :tree-config="{transform: attrs.transform, rowField: attrs.rowField, parentField: attrs.parentField}"
  14. :row-config="{isHover: true}"
  15. :tooltip-config="{showAll: true}"
  16. style="width: 100%">
  17. <vxe-column v-if="attrs.checkbox" type="checkbox" width="50" fixed="left" />
  18. <vxe-column v-if="attrs.radio" type="radio" width="50" fixed="left" />
  19. <vxe-column v-if="attrs.seq" type="seq" title="序号" :width="attrs.seqWidth?attrs.seqWidth:50" fixed="left" :tree-node="attrs.treeNodeSeq" />
  20. <template v-for="(item,index) in columns">
  21. <vxe-column
  22. v-if="item.action"
  23. :key="'action_'+index"
  24. :title="item.title"
  25. :width="item.width"
  26. fixed="right"
  27. >
  28. <template #default="{ row }">
  29. <template v-if="item.plugins">
  30. <template v-for="(pluginsItem,_index) of item.plugins" >
  31. <span class="col-action"
  32. :key="'plu'+_index"
  33. v-hasPermi="[pluginsItem.audit]"
  34. v-if="showPlugin(pluginsItem,row)"
  35. @click="pluginClick(pluginsItem,row)">
  36. <i v-if="pluginsItem.icon" :class="pluginsItem.icon" ></i>
  37. {{ pluginsItem.name }}
  38. </span>
  39. </template>
  40. </template>
  41. </template>
  42. </vxe-column>
  43. <vxe-column
  44. v-if="!item.action"
  45. :key="item.field?item.field:index"
  46. :field="item.field"
  47. :title="item.title"
  48. :width="item.width"
  49. :fixed="item.fixed"
  50. :align="item.align"
  51. :tree-node="item.treeNode"
  52. >
  53. <template #default="{ row }">
  54. <slot v-if="item.slot" :name='item.field' :row='row'></slot>
  55. <component v-else-if="item.component" :is="item.component" :ref="item.prop+'Comp'" :propConfig="item.compConfig" :parentValue="row"
  56. :propValue="row[item.field]" @onChange="onChange($event, row,item.field)" />
  57. <template v-else>
  58. <div v-if="item.isDetail" :class="{'ellipsis':item.ellipsis}" :title="row[item.field]" :style="{ 'text-align': item.align }" class="tdCol detail" @click="detail(row)">{{ row[item.field] }}</div>
  59. <div class="tdCol" :class="{'ellipsis':item.ellipsis}" :title="row[item.field]" :style="{ 'text-align': item.align }" v-else>{{ row[item.field] }}</div>
  60. </template>
  61. </template>
  62. </vxe-column>
  63. </template>
  64. </vxe-table>
  65. <div class="page" v-if="page.total > 0">
  66. <el-pagination
  67. @size-change="handleSizeChange"
  68. @current-change="handleCurrentChange"
  69. :current-page="page.pageNo"
  70. :page-sizes="attrs.pageSizes"
  71. :page-size="page.pageSize?page.pageSize:20"
  72. :layout="attrs.layoutPage?attrs.layoutPage:'total, sizes, prev, pager, next, jumper'"
  73. :total="page.total">
  74. </el-pagination>
  75. </div>
  76. </div>
  77. </template>
  78. <script lang="ts">
  79. /**
  80. config:{
  81. attr:{
  82. size: medium / small / mini //尺寸
  83. height: '' //高度
  84. maxHeight:'' //最大高度
  85. stripe: true/false //是否为斑马纹
  86. border: true/false //是否带有纵向边框
  87. seq:true/false //显示序号
  88. seqWidth:'' //序号宽度
  89. checkbox: true/false //显示复选框
  90. radio:true/false //显示单选框
  91. align: left/center/right //对齐方式
  92. transform: true/false //开启自动将列表转成树结构
  93. rowField:'' row对应id
  94. parentField:'' row父id
  95. pageSize:'' //分页条数
  96. pageSizes:[] //每页显示个数选择器的选项设置
  97. layoutPage:'' //分页组件布局
  98. },
  99. columns:[{
  100. ellipsis:true/false //超出省略号表示
  101. title:'' //标题
  102. field:'' //字段名
  103. align: left/center/right //对齐方式
  104. width:'' //列宽
  105. fixed: true, left, right // 固定列
  106. action: true/false 是否操作列
  107. plugins:操作列执行
  108. component:'' //组件
  109. compConfig:{} //组件配置
  110. slot: true/false //是否插槽
  111. isDetail:true/false //点击详情
  112. tree-node: true/false //展开节点
  113. }],
  114. request:{}
  115. }
  116. */
  117. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  118. import VueViews from '@/benyun/compVue/VueViews'
  119. interface Page{
  120. pageNo?:number,
  121. pageSize?:number,
  122. total?:number
  123. }
  124. @Component
  125. export default class ByTable extends VueViews {
  126. value:Array<any>=[];
  127. key_id="_X_ROW_KEY"
  128. page = {
  129. pageNo: 1, //当前页
  130. pageSize: 20, //每页条数
  131. total: 0 //总条数
  132. }
  133. get columns(){
  134. return this.config?.columns ? this.config.columns : [];
  135. }
  136. created(){
  137. if(this.propConfig){
  138. this.setConfig(this.propConfig)
  139. }
  140. }
  141. mounted(){
  142. this.$nextTick(()=>{
  143. if(this.requestConfig){
  144. this.request();
  145. }
  146. })
  147. }
  148. loadTableData(data:Array<any>){
  149. if(this.$refs.table){
  150. (this.$refs.table as any).loadData(data);
  151. }
  152. }
  153. //点击详情
  154. detail(row:any){
  155. let data = (this as any).$lodash.cloneDeep(row);
  156. delete data[this.key_id];
  157. this.$emit('detail',data)
  158. }
  159. setConfigAfter(){
  160. if(this.attrs.pageSize){
  161. this.page.pageSize = this.attrs.pageSize;
  162. }
  163. }
  164. //设置分页
  165. setPage(page:Page){
  166. if(page.pageNo){
  167. this.page.pageNo = page.pageNo;
  168. }
  169. if(page.pageSize){
  170. this.page.pageSize = page.pageSize;
  171. }
  172. this.page.total = page.total?page.total:0;
  173. }
  174. getPage(){
  175. return this.page
  176. }
  177. //每页条数发生变化
  178. handleSizeChange(val:number) {
  179. this.page.pageSize = val;
  180. if (this.page.pageSize * val > this.page.total) {
  181. this.page.pageNo = 1
  182. }
  183. this.$emit('pagination',{pageNum:this.page.pageNo,pageSize:this.page.pageSize})
  184. }
  185. //当前页发生变化
  186. handleCurrentChange(val:number) {
  187. this.page.pageNo = val;
  188. this.$emit('pagination',{pageNum:this.page.pageNo,pageSize:this.page.pageSize})
  189. }
  190. //操作列点击操作
  191. pluginClick(config:any,row:any){
  192. if(config?.event?.click){
  193. let data = (this as any).$lodash.cloneDeep(row)
  194. delete data[this.key_id];
  195. config.event.click(data)
  196. }
  197. }
  198. //操作按钮是否显示
  199. showPlugin(config:any,row:any){
  200. if(config?.event?.show){
  201. let data = (this as any).$lodash.cloneDeep(row)
  202. delete data[this.key_id];
  203. return config.event.show(data)
  204. }
  205. return true
  206. }
  207. //组件值的变化
  208. onChange(val:any,row:any,code:string){
  209. if(val && (val as any).constructor == Object){
  210. for (const key in val) {
  211. if (!row[key]) {
  212. Vue.set(row, key, val[key])
  213. } else {
  214. row[key] = val[key]
  215. }
  216. }
  217. }else{
  218. if(!row[code]){
  219. Vue.set(row, code, val)
  220. }else{
  221. row[code] = val
  222. }
  223. }
  224. this.$emit('onChangeRow',row);
  225. }
  226. //获取表格选中的数据
  227. getSelectData() {
  228. let data: Array<any> = []
  229. if (this.$refs.table) {
  230. if (this.attrs.radio && (this.$refs.table as any).getRadioRecord()) {
  231. data = (this as any).$lodash.cloneDeep([(this.$refs.table as any).getRadioRecord()]);
  232. }
  233. if(this.attrs.checkbox && (this.$refs.table as any).getCheckboxRecords()){
  234. data = (this as any).$lodash.cloneDeep((this.$refs.table as any).getCheckboxRecords());
  235. }
  236. }
  237. for(let item of data){
  238. delete item[this.key_id];
  239. }
  240. return data
  241. }
  242. //清楚选中
  243. clearCheckboxRow(){
  244. if(this.attrs.checkbox){
  245. (this.$refs.table as any).clearCheckboxRow();
  246. }
  247. if(this.attrs.radio){
  248. (this.$refs.table as any).clearRadioRow();
  249. }
  250. }
  251. setValue(data:Array<any>){
  252. // setTimeout(()=>{
  253. this.value = data ? data : [];
  254. // },100)
  255. this.$forceUpdate()
  256. }
  257. getValue(){
  258. let d = (this as any).$lodash.cloneDeep(this.value);
  259. for(let item of d){
  260. delete item[this.key_id];
  261. }
  262. return d;
  263. }
  264. request(){
  265. if(!this.requestConfig || !this.requestConfig.url){
  266. return
  267. }
  268. let parame = (this as any).$lodash.cloneDeep(this.requestConfig);
  269. parame.success = (res:any) => {
  270. if(res.data){
  271. this.setValue(res.data);
  272. }
  273. }
  274. parame.fail = (err:any) => {}
  275. this.requestHandle(parame);
  276. }
  277. }
  278. </script>
  279. <style lang="scss" scoped>
  280. .by-table{
  281. width: 100%;
  282. .page{
  283. width: 100%;
  284. display: flex;
  285. padding-top: 16px;
  286. justify-content: flex-end;
  287. }
  288. .tdCol{
  289. width: 100%;
  290. }
  291. .ellipsis{
  292. width: 100%;
  293. overflow: hidden;
  294. white-space: nowrap;
  295. text-overflow: ellipsis;
  296. }
  297. }
  298. .col-action{
  299. color: #0089ff;
  300. cursor: pointer;
  301. padding: 0 4px;
  302. }
  303. .detail{
  304. color: #0089ff;
  305. cursor: pointer;
  306. }
  307. </style>