Blame view

src/components/table/cell.vue 4.27 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,
486d4fda   梁灏   update Table
34
                  content: this.$parent.$parent.currentContent
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) {
d0e206c5   梁灏   Table add content...
51
                      const $parent = this.content;
a3547c1b   梁灏   update Table
52
53
54
                      const template = this.column.render(this.row, this.column, this.index);
                      const cell = document.createElement('div');
                      cell.innerHTML = template;
87a400d8   梁灏   update Table
55
56
57
58
59
  //                    const _oldParentChildLen = $parent.$children.length;
  //                    const _newParentChildLen = $parent.$children.length;
  //                    if (_oldParentChildLen !== _newParentChildLen) {    // if render normal html node, do not tag
  //                        this.uid = $parent.$children[$parent.$children.length - 1]._uid;    // tag it, and delete when data or columns update
  //                    }
a3547c1b   梁灏   update Table
60
                      this.$el.innerHTML = '';
486d4fda   梁灏   update Table
61
                      let methods = {};
c0b2524d   梁灏   update Table cell...
62
                      Object.keys($parent).forEach(key => {
3c38e4f7   梁灏   update Table cell
63
                          const func = $parent[`${key}`];
87a400d8   梁灏   update Table
64
                          if (typeof(func) === 'function' && func.name  === 'boundFn') {
c0b2524d   梁灏   update Table cell...
65
66
67
                              methods[`${key}`] = func;
                          }
                      });
486d4fda   梁灏   update Table
68
                      const res = Vue.compile(cell.outerHTML);
87a400d8   梁灏   update Table
69
                      const component = new Vue({
486d4fda   梁灏   update Table
70
71
72
73
                          render: res.render,
                          staticRenderFns: res.staticRenderFns,
                          methods: methods
                      });
87a400d8   梁灏   update Table
74
75
                      const Cell = component.$mount();
                      this.$refs.cell.appendChild(Cell.$el);
a3547c1b   梁灏   update Table
76
77
78
                  }
              },
              destroy () {
d0e206c5   梁灏   Table add content...
79
                  const $parent = this.content;
7f34c510   梁灏   update Table
80
81
82
                  for (let i = 0; i < $parent.$children.length; i++) {
                      if ($parent.$children[i]._uid === this.uid) {
                          $parent.$children[i].$destroy();
a3547c1b   梁灏   update Table
83
84
                      }
                  }
3ef4dfb9   梁灏   update Table
85
              },
741b987a   梁灏   update Table
86
87
              toggleSelect () {
                  this.$parent.$parent.toggleSelect(this.index);
a3547c1b   梁灏   update Table
88
89
              }
          },
486d4fda   梁灏   update Table
90
          created () {
a3547c1b   梁灏   update Table
91
92
              if (this.column.type === 'index') {
                  this.renderType = 'index';
3ef4dfb9   梁灏   update Table
93
94
              } else if (this.column.type === 'selection') {
                  this.renderType = 'selection';
a3547c1b   梁灏   update Table
95
96
97
98
99
100
              } else if (this.column.render) {
                  this.renderType = 'render';
              } else {
                  this.renderType = 'normal';
              }
          },
486d4fda   梁灏   update Table
101
102
103
104
          mounted () {
              this.$nextTick(() => {
                  this.compile();
              });
a3547c1b   梁灏   update Table
105
106
107
108
109
          },
          beforeDestroy () {
              this.destroy();
          },
          watch: {
741b987a   梁灏   update Table
110
              naturalIndex () {
a3547c1b   梁灏   update Table
111
112
113
114
                  this.destroy();
                  this.compile();
              }
          }
b0893113   jingsam   :art: add eslint
115
116
      };
  </script>