Blame view

src/components/table/cell.vue 3.54 KB
a3547c1b   梁灏   update Table
1
  <template>
3ef4dfb9   梁灏   update Table
2
      <div :class="classes">
741b987a   梁灏   update Table
3
          <template v-if="renderType === 'index'">{{naturalIndex + 1}}</template>
3ef4dfb9   梁灏   update Table
4
          <template v-if="renderType === 'selection'">
0dcc9482   leonine   itable 添加禁用某行选中的功能
5
              <Checkbox :checked="checked" @on-change="toggleSelect" :disabled="disabled"></Checkbox>
3ef4dfb9   梁灏   update Table
6
          </template>
a3547c1b   梁灏   update Table
7
8
9
10
          <template v-if="renderType === 'normal'">{{{ row[column.key] }}}</template>
      </div>
  </template>
  <script>
3ef4dfb9   梁灏   update Table
11
12
      import Checkbox from '../checkbox/checkbox.vue';
  
a3547c1b   梁灏   update Table
13
      export default {
3ef4dfb9   梁灏   update Table
14
          components: { Checkbox },
a3547c1b   梁灏   update Table
15
16
17
18
          props: {
              prefixCls: String,
              row: Object,
              column: Object,
741b987a   梁灏   update Table
19
20
              naturalIndex: Number,    // index of rebuildData
              index: Number,           // _index of data
7f34c510   梁灏   update Table
21
              checked: Boolean,
87379c82   leonine   去掉禁用行的 tr>td 的dis...
22
              disabled: Boolean,
5d0499ce   梁灏   update Table
23
24
25
26
              fixed: {
                  type: [Boolean, String],
                  default: false
              }
a3547c1b   梁灏   update Table
27
28
29
30
          },
          data () {
              return {
                  renderType: '',
d0e206c5   梁灏   Table add content...
31
32
                  uid: -1,
                  content: this.$parent.$parent.content
b0893113   jingsam   :art: add eslint
33
              };
a3547c1b   梁灏   update Table
34
          },
3ef4dfb9   梁灏   update Table
35
36
37
38
39
          computed: {
              classes () {
                  return [
                      `${this.prefixCls}-cell`,
                      {
eedcba58   Rijn   Added ellipsis pr...
40
41
                          [`${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
42
                      }
b0893113   jingsam   :art: add eslint
43
                  ];
3ef4dfb9   梁灏   update Table
44
45
              }
          },
a3547c1b   梁灏   update Table
46
47
48
          methods: {
              compile () {
                  if (this.column.render) {
d0e206c5   梁灏   Table add content...
49
                      const $parent = this.content;
a3547c1b   梁灏   update Table
50
51
52
                      const template = this.column.render(this.row, this.column, this.index);
                      const cell = document.createElement('div');
                      cell.innerHTML = template;
7f34c510   梁灏   update Table
53
                      const _oldParentChildLen = $parent.$children.length;
b89a982e   梁灏   fixed #205
54
                      $parent.$compile(cell);    // todo 这里无法触发 ready 钩子
7f34c510   梁灏   update Table
55
                      const _newParentChildLen = $parent.$children.length;
a3547c1b   梁灏   update Table
56
57
  
                      if (_oldParentChildLen !== _newParentChildLen) {    // if render normal html node, do not tag
7f34c510   梁灏   update Table
58
                          this.uid = $parent.$children[$parent.$children.length - 1]._uid;    // tag it, and delete when data or columns update
a3547c1b   梁灏   update Table
59
60
61
62
63
64
                      }
                      this.$el.innerHTML = '';
                      this.$el.appendChild(cell);
                  }
              },
              destroy () {
d0e206c5   梁灏   Table add content...
65
                  const $parent = this.content;
7f34c510   梁灏   update Table
66
67
68
                  for (let i = 0; i < $parent.$children.length; i++) {
                      if ($parent.$children[i]._uid === this.uid) {
                          $parent.$children[i].$destroy();
a3547c1b   梁灏   update Table
69
70
                      }
                  }
3ef4dfb9   梁灏   update Table
71
              },
741b987a   梁灏   update Table
72
73
              toggleSelect () {
                  this.$parent.$parent.toggleSelect(this.index);
a3547c1b   梁灏   update Table
74
75
76
77
78
              }
          },
          compiled () {
              if (this.column.type === 'index') {
                  this.renderType = 'index';
3ef4dfb9   梁灏   update Table
79
80
              } else if (this.column.type === 'selection') {
                  this.renderType = 'selection';
a3547c1b   梁灏   update Table
81
82
83
84
85
86
87
88
89
90
91
92
93
              } else if (this.column.render) {
                  this.renderType = 'render';
              } else {
                  this.renderType = 'normal';
              }
          },
          ready () {
              this.compile();
          },
          beforeDestroy () {
              this.destroy();
          },
          watch: {
741b987a   梁灏   update Table
94
              naturalIndex () {
a3547c1b   梁灏   update Table
95
96
97
98
                  this.destroy();
                  this.compile();
              }
          }
b0893113   jingsam   :art: add eslint
99
100
      };
  </script>