byTable.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <div class="by-table">
  3. <vxe-table
  4. ref="table"
  5. :data="value"
  6. row-id="key_Id"
  7. :size="attrs.size"
  8. resizable
  9. :height="attrs.height"
  10. :max-height="attrs.maxHeight"
  11. :stripe="attrs.stripe"
  12. :border="attrs.border"
  13. :align="attrs.align"
  14. :tree-config="{transform: attrs.transform, rowField: attrs.rowField, parentField: attrs.parentField}"
  15. :row-config="{isHover: 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="index"
  24. :title="item.title"
  25. :width="item.width"
  26. fixed="right"
  27. >
  28. <template #default="{ row }">
  29. <template v-if="item.plugins">
  30. <span class="col-action"
  31. v-for="(pluginsItem,_index) of item.plugins"
  32. :key="'plu'+_index"
  33. v-hasPermi="[item.audit]"
  34. @click="pluginClick(pluginsItem,row)">
  35. <i v-if="pluginsItem.icon" :class="pluginsItem.icon" ></i>
  36. {{ pluginsItem.name }}
  37. </span>
  38. </template>
  39. </template>
  40. </vxe-column>
  41. <vxe-column
  42. v-if="!item.action"
  43. :key="index"
  44. :field="item.field"
  45. :title="item.title"
  46. :width="item.width"
  47. :fixed="item.fixed"
  48. :align="item.align"
  49. :tree-node="item.treeNode"
  50. >
  51. <template #default="{ row }">
  52. <slot v-if="item.slot" :name='item.field' :row='row'></slot>
  53. <component v-else-if="item.component" :is="item.component" :propConfig="item.compConfig" :propValue="row" />
  54. <span v-else>{{ row[item.field] }}</span>
  55. </template>
  56. </vxe-column>
  57. </template>
  58. </vxe-table>
  59. <div class="page">
  60. <el-pagination
  61. v-if="page.total > 0"
  62. @size-change="handleSizeChange"
  63. @current-change="handleCurrentChange"
  64. :current-page="page.pageNo"
  65. :page-sizes="attrs.pageSizes"
  66. :page-size="attrs.pageSize?attrs.pageSize:20"
  67. :layout="attrs.layoutPage?attrs.layoutPage:'total, sizes, prev, pager, next, jumper'"
  68. :total="page.total">
  69. </el-pagination>
  70. </div>
  71. </div>
  72. </template>
  73. <script lang="ts">
  74. /**
  75. config:{
  76. attr:{
  77. size: medium / small / mini //尺寸
  78. height: '' //高度
  79. maxHeight:'' //最大高度
  80. stripe: true/false //是否为斑马纹
  81. border: true/false //是否带有纵向边框
  82. seq:true/false //显示序号
  83. seqWidth:'' //序号宽度
  84. checkbox: true/false //显示复选框
  85. radio:true/false //显示单选框
  86. align: left/center/right //对齐方式
  87. transform: true/false //开启自动将列表转成树结构
  88. rowField:'' row对应id
  89. parentField:'' row父id
  90. pageSize:'' //分页条数
  91. pageSizes:[] //每页显示个数选择器的选项设置
  92. layoutPage:'' //分页组件布局
  93. },
  94. columns:[{
  95. title:'' //标题
  96. field:'' //字段名
  97. width:'' //列宽
  98. fixed: true, left, right // 固定列
  99. action: true/false 是否操作列
  100. plugins:操作列执行
  101. component:'' //组件
  102. compConfig:{} //组件配置
  103. slot: true/false //是否插槽
  104. tree-node: true/false //展开节点
  105. }],
  106. request:{}
  107. }
  108. */
  109. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  110. import VueViews from '@/benyun/compVue/VueViews'
  111. interface Page{
  112. pageNo?:number,
  113. pageSize?:number,
  114. total?:number
  115. }
  116. @Component
  117. export default class ByTable extends VueViews {
  118. value:Array<any>=[];
  119. page = {
  120. pageNo: 1, //当前页
  121. pageSize: 20, //每页条数
  122. total: 0 //总条数
  123. }
  124. get columns(){
  125. return this.config?.columns ? this.config.columns : [];
  126. }
  127. created(){
  128. if(this.propConfig){
  129. this.setConfig(this.propConfig)
  130. }
  131. }
  132. mounted(){
  133. this.$nextTick(()=>{
  134. if(this.requestConfig){
  135. this.request();
  136. }
  137. })
  138. }
  139. setConfigAfter(){
  140. if(this.attrs.pageSize){
  141. this.page.pageSize = this.attrs.pageSize;
  142. }
  143. }
  144. //设置分页
  145. setPage(page:Page){
  146. if(page.pageNo){
  147. this.page.pageNo = page.pageNo;
  148. }
  149. if(page.pageSize){
  150. this.page.pageSize = page.pageSize;
  151. }
  152. this.page.total = page.total?page.total:0;
  153. }
  154. getPage(){
  155. return this.page
  156. }
  157. //每页条数发生变化
  158. handleSizeChange(val:number) {
  159. if (this.page.pageSize * val > this.page.total) {
  160. this.page.pageNo = 1
  161. }
  162. this.$emit('pagination',{pageNum:this.page.pageNo,pageSize:this.page.pageSize})
  163. }
  164. //当前页发生变化
  165. handleCurrentChange(val:number) {
  166. this.page.pageNo = val;
  167. this.$emit('pagination',{pageNum:this.page.pageNo,pageSize:this.page.pageSize})
  168. }
  169. //操作列点击操作
  170. pluginClick(config:any,row:any){
  171. if(config?.event?.click){
  172. delete row.key_Id;
  173. config.event.click(row)
  174. }
  175. }
  176. //获取表格选中的数据
  177. getSelectData() {
  178. let data: Array<any> = []
  179. if (this.$refs.table) {
  180. if (this.attrs.radio && (this.$refs.table as any).getRadioRecord()) {
  181. data = [(this.$refs.table as any).getRadioRecord()]
  182. }
  183. if(this.attrs.checkbox && (this.$refs.table as any).getCheckboxRecords()){
  184. data = (this.$refs.table as any).getCheckboxRecords()
  185. }
  186. }
  187. for(let item of data){
  188. delete item.key_Id;
  189. }
  190. return data
  191. }
  192. setValue(data:Array<any>){
  193. this.value = data ? data : [];
  194. }
  195. getValue(){
  196. let d = (this as any).$lodash.cloneDeep(this.value);
  197. for(let item of d){
  198. delete item.key_Id;
  199. }
  200. return d;
  201. }
  202. request(){
  203. if(!this.requestConfig || !this.requestConfig.url){
  204. return
  205. }
  206. let parame = (this as any).$lodash.cloneDeep(this.requestConfig);
  207. parame.success = (res:any) => {
  208. if(res.data){
  209. this.setValue(res.data);
  210. }
  211. }
  212. parame.fail = (err:any) => {}
  213. this.requestHandle(parame);
  214. }
  215. }
  216. </script>
  217. <style lang="scss" scoped>
  218. .by-table{
  219. width: 100%;
  220. .page{
  221. width: 100%;
  222. display: flex;
  223. padding-top: 16px;
  224. justify-content: flex-end;
  225. }
  226. }
  227. .col-action{
  228. color: #0089ff;
  229. cursor: pointer;
  230. padding: 0 4px;
  231. }
  232. </style>