Blame view

src/components/table/cell.vue 6.45 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>
12fd2dc5   anykno   Merge branch '2.0...
7
  
b3dbdba9   anykno   impove table rend...
8
9
          <template v-if="renderType === 'html'"><span v-html="row[column.key]"></span></template>
          <template v-if="renderType === 'normal'"><span>{{row[column.key]}}</span></template>
08fd628d   Aresn   Table support expand
10
11
12
13
14
          <template v-if="renderType === 'expand'">
              <div :class="expandCls" @click="toggleExpand">
                  <Icon type="ios-arrow-right"></Icon>
              </div>
          </template>
a3547c1b   梁灏   update Table
15
16
17
      </div>
  </template>
  <script>
486d4fda   梁灏   update Table
18
      import Vue from 'vue';
08fd628d   Aresn   Table support expand
19
      import Icon from '../icon/icon.vue';
3ef4dfb9   梁灏   update Table
20
      import Checkbox from '../checkbox/checkbox.vue';
1e20ac5c   Aresn   #958
21
      import { findComponentUpward } from '../../utils/assist';
3ef4dfb9   梁灏   update Table
22
  
a3547c1b   梁灏   update Table
23
      export default {
486d4fda   梁灏   update Table
24
          name: 'TableCell',
08fd628d   Aresn   Table support expand
25
          components: { Icon, Checkbox },
a3547c1b   梁灏   update Table
26
27
28
29
          props: {
              prefixCls: String,
              row: Object,
              column: Object,
741b987a   梁灏   update Table
30
31
              naturalIndex: Number,    // index of rebuildData
              index: Number,           // _index of data
7f34c510   梁灏   update Table
32
              checked: Boolean,
87379c82   leonine   去掉禁用行的 tr>td 的dis...
33
              disabled: Boolean,
08fd628d   Aresn   Table support expand
34
              expanded: Boolean,
5d0499ce   梁灏   update Table
35
36
37
38
              fixed: {
                  type: [Boolean, String],
                  default: false
              }
a3547c1b   梁灏   update Table
39
40
41
42
          },
          data () {
              return {
                  renderType: '',
d0e206c5   梁灏   Table add content...
43
                  uid: -1,
d8892603   梁灏   Table prop: conte...
44
                  context: this.$parent.$parent.currentContext
b0893113   jingsam   :art: add eslint
45
              };
a3547c1b   梁灏   update Table
46
          },
3ef4dfb9   梁灏   update Table
47
48
49
50
51
          computed: {
              classes () {
                  return [
                      `${this.prefixCls}-cell`,
                      {
eedcba58   Rijn   Added ellipsis pr...
52
                          [`${this.prefixCls}-hidden`]: !this.fixed && this.column.fixed && (this.column.fixed === 'left' || this.column.fixed === 'right'),
08fd628d   Aresn   Table support expand
53
54
                          [`${this.prefixCls}-cell-ellipsis`]: this.column.ellipsis || false,
                          [`${this.prefixCls}-cell-with-expand`]: this.renderType === 'expand'
3ef4dfb9   梁灏   update Table
55
                      }
b0893113   jingsam   :art: add eslint
56
                  ];
08fd628d   Aresn   Table support expand
57
58
59
60
61
62
              },
              expandCls () {
                  return [
                      `${this.prefixCls}-cell-expand`,
                      {
                          [`${this.prefixCls}-cell-expand-expanded`]: this.expanded
3ef4dfb9   梁灏   update Table
63
                      }
b0893113   jingsam   :art: add eslint
64
                  ];
3ef4dfb9   梁灏   update Table
65
66
              }
          },
a3547c1b   梁灏   update Table
67
68
          methods: {
              compile () {
08fd628d   Aresn   Table support expand
69
                  if (this.column.render && this.renderType === 'render') {
87b6ef9d   梁灏   Table support rea...
70
                      // 兼容真 Render,后期废弃旧用法
1e20ac5c   Aresn   #958
71
72
73
                      let isRealRender = true;
                      const Table = findComponentUpward(this, 'Table');
                      if (Table.context) isRealRender = false;
99d0429e   梁灏   update Button & T...
74
  
87b6ef9d   梁灏   Table support rea...
75
                      if (isRealRender) {
10d3a323   Aresn   fixed #1011
76
                          this.$el.innerHTML = '';
87b6ef9d   梁灏   Table support rea...
77
                          const component = new Vue({
28587238   梁灏   update Table func...
78
                              functional: true,
87b6ef9d   梁灏   Table support rea...
79
                              render: (h) => {
78d8ea4d   梁灏   release 2.0.0-rc.13
80
81
82
83
84
                                  return this.column.render(h, {
                                      row: this.row,
                                      column: this.column,
                                      index: this.index
                                  });
87b6ef9d   梁灏   Table support rea...
85
86
87
88
89
90
91
92
93
                              }
                          });
                          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...
94
  
87b6ef9d   梁灏   Table support rea...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                          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...
109
  
87b6ef9d   梁灏   Table support rea...
110
111
112
113
114
115
116
117
118
                          const component = new Vue({
                              render: res.render,
                              staticRenderFns: res.staticRenderFns,
                              methods: methods,
                              data () {
                                  return $parent._data;
                              },
                              components: components
                          });
8f69e63c   吕庆安   修正table render的组件...
119
120
121
                          if ($parent.$store != undefined) {
                              component.$store = $parent.$store;
                          }
87b6ef9d   梁灏   Table support rea...
122
123
                          component.row = this.row;
                          component.column = this.column;
99d0429e   梁灏   update Button & T...
124
  
87b6ef9d   梁灏   Table support rea...
125
126
127
                          const Cell = component.$mount();
                          this.$refs.cell.appendChild(Cell.$el);
                      }
a3547c1b   梁灏   update Table
128
129
130
                  }
              },
              destroy () {
99d0429e   梁灏   update Button & T...
131
  
3ef4dfb9   梁灏   update Table
132
              },
741b987a   梁灏   update Table
133
134
              toggleSelect () {
                  this.$parent.$parent.toggleSelect(this.index);
08fd628d   Aresn   Table support expand
135
136
137
              },
              toggleExpand () {
                  this.$parent.$parent.toggleExpand(this.index);
a3547c1b   梁灏   update Table
138
139
              }
          },
486d4fda   梁灏   update Table
140
          created () {
a3547c1b   梁灏   update Table
141
142
              if (this.column.type === 'index') {
                  this.renderType = 'index';
3ef4dfb9   梁灏   update Table
143
144
              } else if (this.column.type === 'selection') {
                  this.renderType = 'selection';
b3dbdba9   anykno   impove table rend...
145
146
              } else if (this.column.type === 'html') {
                  this.renderType = 'html';
08fd628d   Aresn   Table support expand
147
148
              } else if (this.column.type === 'expand') {
                  this.renderType = 'expand';
a3547c1b   梁灏   update Table
149
150
151
152
153
154
              } else if (this.column.render) {
                  this.renderType = 'render';
              } else {
                  this.renderType = 'normal';
              }
          },
486d4fda   梁灏   update Table
155
156
157
158
          mounted () {
              this.$nextTick(() => {
                  this.compile();
              });
a3547c1b   梁灏   update Table
159
160
161
162
163
          },
          beforeDestroy () {
              this.destroy();
          },
          watch: {
741b987a   梁灏   update Table
164
              naturalIndex () {
a3547c1b   梁灏   update Table
165
166
167
168
                  this.destroy();
                  this.compile();
              }
          }
b0893113   jingsam   :art: add eslint
169
170
      };
  </script>