Blame view

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