您现在的位置:首页 >> 网页设计 >> JavaScript >> 内容

vue+render+jsx实现可编辑动态多级表头table的实例代码

时间:2020-4-3 21:59:07

  核心提示:最近项目需要实现可编辑的动态多级表头表格,看了两天的文章,始终没有找到我想要的效果,在了解了render+jsx的基础用法后,自己基于element-ui封装了一个,数据格式参考element-ui...

最近项目需要实现可编辑的动态多级表头表格,看了两天的文章,始终没有找到我想要的效果,在了解了render+jsx的基础用法后,自己基于element-ui封装了一个,数据格式参考element-ui table的数据。实现如下:

1.scoresTable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<script>
  import scoresColumn from "./scoresColumn";
  export default {
    components: {
      scoresColumn
    },
    render: function(h) {
      return <div className="table-control">
        <el-table ref="table"
             size="small"
             {...{attrs: {data:this.tableData}}}
             border
        >
          {
            this.tableTitles.map(title => {
              return <scoresColumn on-dataChange={this.dataChange} {...{attrs: {column:title,unitScores: this.unitScores}}}></scoresColumn>
            })
          }
 
        </el-table>
      </div>;
    },
    props: {
      tableTitles: {
        type: Array,
        default: () => []
      },
      tableData: {
        type: Array,
        default: () => []
      },
      unitScores: {
        type: Object,
        default: () => {}
      }
    },
    methods: {
      dataChange(id) {
        this.$emit('dataChange', id);
      }
    },
  }
</script>
<style>
 .el-table th, .el-table td {
  text-align: center;
 }
</style>

2.scoresColumn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<script>
  export default {
    data() {
     return {
       style: {
         'min-width': "70",
         'resizable': true,
         'show-overflow-tooltip': true
       },
     }
    },
    props: {
     column: {
       type: Object
     },
     unitScores: {
       type: Object,
       default: () => {}
     }
    },
    name: "scoresColumn",
    render: function (h) {
      let scopedSlots = {
        default: (scope) => {
          let col = scope.column.property;
          let value = scope.row[col];
          return <div id={col+scope.$index} >
            <p onClick={this.clickHandle}>{value}</p>
          </div>;
        }
      };
      if (this.column.children === undefined)
        if (this.column.label == '序号' || this.column.label == '姓名') {
          return <el-table-column fixed
            {...{style: this.style, scopedSlots: {
                default: (scope) => {
                  let value = scope.row[scope.column.property];
                  return <p>{value}</p>;
                }
              }}}
            prop={this.column.prop} label={this.column.label}>
          </el-table-column>
        }else {
          return <el-table-column
             {...{style: this.style, scopedSlots: {
                default: (scope) => {
                  let value = scope.row[scope.column.property];
                  if (/\((?=\d)|(^总计$)/g.test(this.column.label)) {
                    let col = scope.column.property;
                    return <div id={col+scope.$index} >
                      <p onClick={this.clickHandle}>{value}</p>
                     </div>;
                  }else return <p>{value}</p>;
                }
               }}}
             prop={this.column.prop} label={this.column.label}>
          </el-table-column>
        }
      let buildTitles = (childList) => {
        let children = [];
        childList.map(child => {
          if (child.children != undefined && child.children.length > 0) {
            children.push(<el-table-column {...{style: this.style}} label={child.label}>
              {buildTitles(child.children)}
            </el-table-column>)
          } else {
            children.push(
              <el-table-column {...{style: this.style, scopedSlots: scopedSlots}}
                       label={child.label} prop={child.prop}>
              </el-table-column>)
          }
        });
        return children;
      };
      return <el-table-column
        {...{style: this.style}}
        label={this.column.label}
        prop={this.column.prop}>
        {buildTitles(this.column.children)}
      </el-table-column>;
    },
    methods: {
      blurHandler(e) {
        let parent = e.target.parentNode;
        let child = parent.firstElementChild;
        let p = document.createElement('p');
        let value = child.value.match(/^\d*(\.{1}\d+)?/)[0];
        if (value == '' || value == null) {
          value = 0;
        }
        p.innerHTML = value;
        p.addEventListener('click', this.clickHandle, false);
        child.replaceWith(p);
        this.$emit('dataChange', parent.id);
      },
      clickHandle(e) {
        let parent = e.target.parentNode;
        let child = parent.firstElementChild;
        let input = document.createElement('input');
        input.style.lineHeight = '23px';
        input.style.textAlign = 'center';
        input.style.fontSize = '12px';
        input.style.height = '23px'
        input.style.width = '100%';
        input.value = child.innerHTML;
        input.addEventListener('blur', this.blurHandler, true);
        input.addEventListener('keyup', this.keyUpHandler, false);
        child.replaceWith(input);
        input.focus();
      },
      keyUpHandler(e) {
        let input = e.target;
        let parent = input.parentNode;
        let property = parent.id.replace(/\d/g, '');
        let value = input.value.replace(/[^\d\.]/g,'');
        if (Math.min(this.unitScores[property],value) != value) {
          value = this.unitScores[property];
        }
        input.value = value;
      }
    }
  }
</script>
<style scoped>
</style>

3.实现效果


总结

到此这篇关于vue+render+jsx实现可编辑动态多级表头table的文章

Java免费学习   Java自学网 http://www.javalearns.com

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java

作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号