当前位置:   article > 正文

封装el-table的使用_el-table html写法

el-table html写法

封装此公共组件通过具名插槽的方式来达到自定义table的效果

模板的html的写法:

  1. <div>
  2. <el-table
  3. :cell-style="{'text-align':'center'}"
  4. :data="tableData"
  5. border
  6. :header-cell-style="headerCellStyle"
  7. style="width: 100%"
  8. v-loading="loading">
  9. <template v-for="(col,index) in fieldList">
  10. <!--是否出现多选框的列-->
  11. <slot v-if="col.slot" :name="col.slot">
  12. </slot>
  13. <!--普通列-->
  14. <el-table-column v-else :key="index" :prop="col.prop" :label="col.label" :width="col.width"
  15. align="center">
  16. </el-table-column>
  17. </template>
  18. </el-table>
  19. <!-- 是否调用分页 -->
  20. <el-pagination v-if="pageObj"
  21. layout="total, prev, pager, next, jumper"
  22. :page-size="pageObj.size"
  23. :total="pageObj.total"
  24. :current-page="pageObj.currentPage"
  25. @current-change="handlerPage()">
  26. </el-pagination>
  27. </div>

js的写法:

  1. <script>
  2. export default {
  3. name: "commonTable",
  4. props:{
  5. //显示数据
  6. tableData:Array,
  7. //设置整个table表格的宽度
  8. width:String,
  9. //
  10. fieldList:Array,
  11. //设置多级表头的list
  12. setMultiLevelHeaderList:Array,
  13. loading:Boolean,
  14. //是否加上分页
  15. pageObj:Object,
  16. headerCellStyle:headerCellStyle
  17. },
  18. methods:{
  19. handlerPage(val){
  20. this.$emit('handlerPage',val);
  21. }
  22. }
  23. }
  24. </script>

如何使用html的写法:

  1. <!--使用封装之后的公共表格-->
  2. <common-table :tableData="classroomTableData" @handlerPage="handlerPage" :headerCellStyle="headerCellStyle" :loading="loading" :pageObj="pageObj" width="100%" :fieldList="fieldList">
  3. <el-table-column slot="多选" type="selection" width="55">
  4. </el-table-column>
  5. <el-table-column
  6. label="操作"
  7. slot="操作"
  8. >
  9. <template slot-scope="scope">
  10. <el-button type="text">详情</el-button>
  11. <el-button type="text">修改名称</el-button>
  12. </template>
  13. </el-table-column>
  14. <el-table-column
  15. prop="studentAddClassroomSetting"
  16. label="学生加班设置"
  17. slot="下拉选框"
  18. >
  19. <template slot-scope="scope">
  20. <el-select size="small" v-model="classroomValue" @change="selectClassType" placeholder="请选择" >
  21. <el-option
  22. v-for="item in classroomList"
  23. :key="item.value"
  24. :label="item.label"
  25. :value="item.value">
  26. </el-option>
  27. </el-select>
  28. </template>
  29. </el-table-column>
  30. <el-table-column
  31. prop="studentChangeName"
  32. label="允许学生改名"
  33. slot="switch"
  34. >
  35. <template slot-scope="scope">
  36. <el-switch
  37. v-model="nameChangeAllowed"
  38. active-text="开"
  39. inactive-text="关">
  40. </el-switch>
  41. </template>
  42. </el-table-column>
  43. </common-table>

js的写法:

  1. <script>
  2. import {EventBus} from "../../../assets/static/global/event-bus";
  3. import dayjs from 'dayjs'
  4. import coassRoomManagementSon from './coassRoomManagementSon'
  5. export default {
  6. name: "classRoomManagement",
  7. components:{
  8. coassRoomManagementSon
  9. },
  10. data(){
  11. return{
  12. headerCellStyle:{
  13. background: '#faf6fa',
  14. color: '#fff6f9',
  15. fontWeight: 'bolder',
  16. textAlign: 'center',
  17. },
  18. fieldList:[
  19. {slot:'多选'},
  20. {label:'班级名称',prop:'classroom'},
  21. {label:'班级类型',prop:'classType'},
  22. {label:'所属年级',prop:'grade'},
  23. {label:'学生人数',prop:'studentCount'},
  24. {label:'班主任',prop:'headMaster'},
  25. {slot:'操作'},
  26. {slot:'下拉选框'},
  27. {slot:'switch'}
  28. ],
  29. pageObj:{
  30. size:40,
  31. totla:0,
  32. currentPgae:1,
  33. func:'func'
  34. },
  35. loading:false,
  36. form:{
  37. schoolName:'',
  38. grade:[],
  39. gradeValue:'',
  40. subjectList:[],
  41. nameMode:[],
  42. subjectValue:'',
  43. jbSettingList:[],
  44. studentChangeAllowed:'开'
  45. },
  46. //班级表格的数据
  47. classroomTableData:[
  48. {
  49. classroom:'2班',
  50. classType:'教学班',
  51. grade:'高一',
  52. studentCount:233,
  53. headMaster:'高老师',
  54. },
  55. {
  56. classroom:'2班',
  57. classType:'教学班',
  58. grade:'高一',
  59. studentCount:233,
  60. headMaster:'高老师',
  61. }
  62. ]
  63. },
  64. methods:{
  65. handlerPage(val){
  66. console.log('获取页数'+val);
  67. }
  68. }
  69. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/965250
推荐阅读
相关标签
  

闽ICP备14008679号