Blame view

src/components/table/cell.vue 5.09 KB
a3547c1b   梁灏   update Table
1
  <template>
87a400d8   梁灏   update Table
2
      <div :class="classes" ref="cell">
741b987a   梁灏   update Table
3
          <template v-if="renderType === 'index'">{{naturalIndex + 1}}</template>
3ef4dfb9   梁灏   update Table
4
          <template v-if="renderType === 'selection'">
486d4fda   梁灏   update Table
5
              <Checkbox :value="checked" @on-change="toggleSelect" :disabled="disabled"></Checkbox>
3ef4dfb9   梁灏   update Table
6
          </template>
486d4fda   梁灏   update Table
7
          <template v-if="renderType === 'normal'"><span v-html="row[column.key]"></span></template>
a3547c1b   梁灏   update Table
8
9
10
      </div>
  </template>
  <script>
486d4fda   梁灏   update Table
11
      import Vue from 'vue';
3ef4dfb9   梁灏   update Table
12
13
      import Checkbox from '../checkbox/checkbox.vue';
  
a3547c1b   梁灏   update Table
14
      export default {
486d4fda   梁灏   update Table
15
          name: 'TableCell',
3ef4dfb9   梁灏   update Table
16
          components: { Checkbox },
a3547c1b   梁灏   update Table
17
18
19
20
          props: {
              prefixCls: String,
              row: Object,
              column: Object,
741b987a   梁灏   update Table
21
22
              naturalIndex: Number,    // index of rebuildData
              index: Number,           // _index of data
7f34c510   梁灏   update Table
23
              checked: Boolean,
87379c82   leonine   去掉禁用行的 tr>td 的dis...
24
              disabled: Boolean,
5d0499ce   梁灏   update Table
25
26
27
28
              fixed: {
                  type: [Boolean, String],
                  default: false
              }
a3547c1b   梁灏   update Table
29
30
31
32
          },
          data () {
              return {
                  renderType: '',
d0e206c5   梁灏   Table add content...
33
                  uid: -1,
d8892603   梁灏   Table prop: conte...
34
                  context: this.$parent.$parent.currentContext
b0893113   jingsam   :art: add eslint
35
              };
a3547c1b   梁灏   update Table
36
          },
3ef4dfb9   梁灏   update Table
37
38
39
40
41
          computed: {
              classes () {
                  return [
                      `${this.prefixCls}-cell`,
                      {
eedcba58   Rijn   Added ellipsis pr...
42
43
                          [`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right'),
                          [`${this.prefixCls}-cell-ellipsis`]: this.column.ellipsis || false
3ef4dfb9   梁灏   update Table
44
                      }
b0893113   jingsam   :art: add eslint
45
                  ];
3ef4dfb9   梁灏   update Table
46
47
              }
          },
a3547c1b   梁灏   update Table
48
49
50
          methods: {
              compile () {
                  if (this.column.render) {
87b6ef9d   梁灏   Table support rea...
51
52
53
54
55
56
57
58
                      // 兼容真 Render,后期废弃旧用法
                      let isRealRender = false;
                      try {
                          this.column.render(this.row, this.column, this.index);
                      }
                      catch (err) {
                          isRealRender = true;
                      }
99d0429e   梁灏   update Button & T...
59
  
87b6ef9d   梁灏   Table support rea...
60
61
                      if (isRealRender) {
                          const component = new Vue({
28587238   梁灏   update Table func...
62
                              functional: true,
87b6ef9d   梁灏   Table support rea...
63
64
65
66
67
68
69
70
71
72
73
                              render: (h) => {
                                  return this.column.render(h, this.row, this.column, this.index);
                              }
                          });
                          const Cell = component.$mount();
                          this.$refs.cell.appendChild(Cell.$el);
                      } else {
                          const $parent = this.context;
                          const template = this.column.render(this.row, this.column, this.index);
                          const cell = document.createElement('div');
                          cell.innerHTML = template;
64f99c05   梁灏   Table support ren...
74
  
87b6ef9d   梁灏   Table support rea...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
                          this.$el.innerHTML = '';
                          let methods = {};
                          Object.keys($parent).forEach(key => {
                              const func = $parent[key];
                              if (typeof(func) === 'function' && (func.name  === 'boundFn' || func.name === 'n')) {
                                  methods[key] = func;
                              }
                          });
                          const res = Vue.compile(cell.outerHTML);
                          // 获取父组件使用的局部 component
                          const components = {};
                          Object.getOwnPropertyNames($parent.$options.components).forEach(item => {
                              components[item] = $parent.$options.components[item];
                          });
64f99c05   梁灏   Table support ren...
89
  
87b6ef9d   梁灏   Table support rea...
90
91
92
93
94
95
96
97
98
99
100
                          const component = new Vue({
                              render: res.render,
                              staticRenderFns: res.staticRenderFns,
                              methods: methods,
                              data () {
                                  return $parent._data;
                              },
                              components: components
                          });
                          component.row = this.row;
                          component.column = this.column;
99d0429e   梁灏   update Button & T...
101
  
87b6ef9d   梁灏   Table support rea...
102
103
104
                          const Cell = component.$mount();
                          this.$refs.cell.appendChild(Cell.$el);
                      }
a3547c1b   梁灏   update Table
105
106
107
                  }
              },
              destroy () {
99d0429e   梁灏   update Button & T...
108
  
3ef4dfb9   梁灏   update Table
109
              },
741b987a   梁灏   update Table
110
111
              toggleSelect () {
                  this.$parent.$parent.toggleSelect(this.index);
a3547c1b   梁灏   update Table
112
113
              }
          },
486d4fda   梁灏   update Table
114
          created () {
a3547c1b   梁灏   update Table
115
116
              if (this.column.type === 'index') {
                  this.renderType = 'index';
3ef4dfb9   梁灏   update Table
117
118
              } else if (this.column.type === 'selection') {
                  this.renderType = 'selection';
a3547c1b   梁灏   update Table
119
120
121
122
123
124
              } else if (this.column.render) {
                  this.renderType = 'render';
              } else {
                  this.renderType = 'normal';
              }
          },
486d4fda   梁灏   update Table
125
126
127
128
          mounted () {
              this.$nextTick(() => {
                  this.compile();
              });
a3547c1b   梁灏   update Table
129
130
131
132
133
          },
          beforeDestroy () {
              this.destroy();
          },
          watch: {
741b987a   梁灏   update Table
134
              naturalIndex () {
a3547c1b   梁灏   update Table
135
136
137
138
                  this.destroy();
                  this.compile();
              }
          }
b0893113   jingsam   :art: add eslint
139
140
      };
  </script>