当前位置:   article > 正文

关于element-ui table简单的封装_el-table-column传入row-key

el-table-column传入row-key
  1. <template>
  2. <div>
  3. <el-table
  4. :data="data"
  5. border
  6. stripe
  7. fit
  8. style="width: 100%">
  9. <el-table-column v-for="(value, key) in header" :key='key' //循环生成header
  10. :prop="key"
  11. :label="value"
  12. :width="columnWidths[key]" //指定某一行的具体宽度
  13. align = "center"
  14. >
  15. <template slot-scope='scope'> //使用element-ui table 里边的scope
  16. <slot v-if='slotColumns.indexOf(key) > -1' :name = 'key v-bind='scope'> //判断slotColumns传进来的值key里边是否存在 给调用他的父级传scope
  17. <template v-else>{{{{scope.row[key]}}}}</template> //用于某一列需要特殊处理
  18. </template>
  19. </el-table-column>
  20. <slot name="oprate"><slot> //预留一个空的插槽可以添加在父级上边调用比较多的组件方法
  21. </el-table>
  22. </div>
  23. </template>
  24. <script>
  25. import ElementUI from 'element-ui';
  26. Vue.use(ElementUI);
  27. export default{
  28. props:{
  29. header:{
  30. type: Object
  31. default:()=>({})
  32. },
  33. data:{
  34. type: Array,
  35. default:()=>[]
  36. },
  37. columnWidths:{
  38. type: Array,
  39. default:()=>[]
  40. },
  41. slotColumns:{
  42. type: Array,
  43. default:()=>[]
  44. }
  45. }
  46. }
  47. </script>

父组件调用

  1. <template>
  2. <div class='tables'>
  3. <Tables :header='header' :data='data' :columnWidths='columnWidths' :slotColumns='slotColumns'>
  4. <template slot='status_name' slot-scope="scope"> //拿到slot传过来的scope
  5. <span :class = 'setStatusColor(scope.row)'>{{scope.row.status_name}} </span>
  6. </template>
  7. <template slot='create_time' slot-scope="scope"> //拿到slot传过来的scope
  8. <span>{{new Date(scope.row.create_time).getTime()}}</span>
  9. </template>
  10. <template slot='oprate'> //可以插入tables里边的固定栏如
  11. <el-table-column
  12. fixed="right"
  13. label="人群运算"
  14. width="100"
  15. align = "center"
  16. >
  17. <template slot-scope="scope">
  18. <el-button @click="handleClick(scope)" type="text" size="small">组合</el-button>
  19. <el-button type="text" size="small">拓展</el-button>
  20. </template>
  21. </el-table-column>
  22. </template>
  23. </Tables>
  24. </div>
  25. </template>
  26. <script>
  27. import Tables from './tables'
  28. export default {
  29. components:{
  30. Tables
  31. },
  32. data() {
  33. return {
  34. header:{status_name:'状态', create_time: '创建时间' , crowd_name: '名称'}
  35. data:[
  36. {
  37. create_time: "2019-06-12 10:37:37"
  38. crowd_name: "Game-拓展"
  39. status: 2
  40. status_name: ""
  41. },
  42. {
  43. create_time: "2019-06-12 10:37:37"
  44. crowd_name: "Game-拓展"
  45. status: 1
  46. status_name: "构建中"
  47. }
  48. ]
  49. columnWidths: {
  50. crowd_name: 180
  51. },
  52. slotColumns: ['status_name', 'create_time'] //多个灵活调用插槽
  53. }
  54. },
  55. methods: {
  56. setStatusColor(row) {
  57. return ['is-building', 'is-builded'][row.status-1]
  58. }
  59. }
  60. }
  61. </script>

 

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