浏览代码

Table: Add spanMethod to support rowspan and colspan

lirilsu 7 年之前
父节点
当前提交
8e09988918
共有 2 个文件被更改,包括 100 次插入10 次删除
  1. 97 9
      packages/table/src/table-body.js
  2. 3 1
      packages/table/src/table.vue

+ 97 - 9
packages/table/src/table-body.js

@@ -55,16 +55,74 @@ export default {
                 on-mouseleave={ _ => this.handleMouseLeave() }
                 class={ [this.getRowClass(row, $index)] }>
                 {
-                  this._l(this.columns, (column, cellIndex) =>
-                    <td
-                      class={ [column.id, column.align, column.className || '', columnsHidden[cellIndex] ? 'is-hidden' : '' ] }
-                      on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
-                      on-mouseleave={ this.handleCellMouseLeave }>
-                      {
-                        column.renderCell.call(this._renderProxy, h, { row, column, $index, store: this.store, _self: this.context || this.table.$vnode.context }, columnsHidden[cellIndex])
+                  this._l(this.columns, (column, cellIndex) => {
+                    const { rowspan, colspan } = this.getSpan(row, column, $index, cellIndex);
+                    if (!rowspan || !colspan) {
+                      return '';
+                    } else {
+                      if (rowspan === 1 && colspan === 1) {
+                        return (
+                          <td
+                            class={
+                              [
+                                column.id,
+                                column.align,
+                                column.className || '',
+                                columnsHidden[cellIndex] ? 'is-hidden' : ''
+                              ]
+                            }
+                            on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
+                            on-mouseleave={ this.handleCellMouseLeave }>
+                            {
+                              column.renderCell.call(
+                                this._renderProxy,
+                                h,
+                                {
+                                  row,
+                                  column,
+                                  $index,
+                                  store: this.store,
+                                  _self: this.context || this.table.$vnode.context
+                                },
+                                columnsHidden[cellIndex]
+                              )
+                            }
+                          </td>
+                        );
+                      } else {
+                        return (
+                          <td
+                            class={
+                              [
+                                column.id,
+                                column.align,
+                                column.className || '',
+                                columnsHidden[cellIndex] ? 'is-hidden' : ''
+                              ]
+                            }
+                            rowspan={ rowspan }
+                            colspan={ colspan }
+                            on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
+                            on-mouseleave={ this.handleCellMouseLeave }>
+                            {
+                              column.renderCell.call(
+                                this._renderProxy,
+                                h,
+                                {
+                                  row,
+                                  column,
+                                  $index,
+                                  store: this.store,
+                                  _self: this.context || this.table.$vnode.context
+                                },
+                                columnsHidden[cellIndex]
+                              )
+                            }
+                          </td>
+                        );
                       }
-                    </td>
-                  )
+                    }
+                  })
                 }
                 {
                   !this.fixed && this.layout.scrollY && this.layout.gutterWidth ? <td class="gutter" /> : ''
@@ -186,6 +244,36 @@ export default {
       }
     },
 
+    getSpan(row, column, rowIndex, columnIndex) {
+      let rowspan = 1;
+      let colspan = 1;
+
+      const fn = this.table.spanMethod;
+      if (typeof fn === 'function') {
+        const result = fn({
+          row,
+          column,
+          rowIndex,
+          columnIndex
+        });
+
+        console.log(result);
+
+        if (Array.isArray(result)) {
+          rowspan = result[0];
+          colspan = result[1];
+        } else if (typeof result === 'object') {
+          rowspan = result.rowspan;
+          colspan = result.colspan;
+        }
+      }
+
+      return {
+        rowspan,
+        colspan
+      };
+    },
+
     getRowStyle(row, index) {
       const rowStyle = this.rowStyle;
       if (typeof rowStyle === 'function') {

+ 3 - 1
packages/table/src/table.vue

@@ -217,7 +217,9 @@
 
       defaultSort: Object,
 
-      tooltipEffect: String
+      tooltipEffect: String,
+
+      spanMethod: Function
     },
 
     components: {