Skip to content

Commit

Permalink
Merge pull request ElemeFE#68 from eleme/feat/next-table
Browse files Browse the repository at this point in the history
Feat/next table
  • Loading branch information
furybean authored Aug 17, 2016
2 parents 6014c4d + cae2767 commit 62e3e76
Show file tree
Hide file tree
Showing 11 changed files with 736 additions and 673 deletions.
2 changes: 1 addition & 1 deletion examples/components/table-filter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
ready() {
mounted() {
console.log('popup ready');
}
};
Expand Down
57 changes: 42 additions & 15 deletions examples/docs/table.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script>
import Vue from 'vue';
let popup = Vue.extend(require('examples/components/table-filter.vue'));
export default {
data() {
return {
Expand Down Expand Up @@ -81,6 +80,16 @@
};
},

methods: {
handleSelectionChange(val) {
this.singleSelection = val;
},

handleMultipleSelectionChange(val) {
this.multipleSelection = val;
}
},

watch: {
singleSelection(val) {
console.log('selection: ', val);
Expand Down Expand Up @@ -287,7 +296,7 @@
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
</template>

<script>
export default {
data() {
Expand Down Expand Up @@ -427,16 +436,16 @@

## 单选

<el-table :data="tableData" selection-mode="single" :selection.sync="singleSelection" style="width: 520px" allow-no-selection>
<el-table :data="tableData" selection-mode="single" @selectionchange="handleSelectionChange" style="width: 520px" allow-no-selection>
<el-table-column property="date" label="日期" width="120"></el-table-column>
<el-table-column property="name" label="姓名" width="120"></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
<p>{{ singleSelection | json }}</p>
<p>{{ singleSelection }}</p>

```html
<template>
<el-table :data="tableData" selection-mode="single" :selection.sync="singleSelection">
<el-table :data="tableData" selection-mode="single" @selectionchange="handleSelectionChange">
<el-table-column property="date" label="日期" width="120"></el-table-column>
<el-table-column property="name" label="姓名" width="120"></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
Expand Down Expand Up @@ -466,26 +475,36 @@
}],
singleSelection: {}
}
},
methods: {
handleSelectionChange(val) {
this.singleSelection = val;
}
}
}
</script>
```

## 多选

<el-table :data="tableData3" selection-mode="multiple" :selection.sync="multipleSelection" style="width: 520px">
<el-table :data="tableData3" selection-mode="multiple" style="width: 520px" @selectionchange="handleMultipleSelectionChange">
<el-table-column type="selection" width="50"></el-table-column>
<el-table-column property="date" label="日期" width="120"></el-table-column>
<el-table-column inline-template property="date" label="日期" width="120">
<div>{{ row.date }}</div>
</el-table-column>
<el-table-column property="name" label="姓名" width="120"></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
<p>{{ multipleSelection | json }}</p>
<p>{{ multipleSelection }}</p>

```html
<template>
<el-table :data="tableData3" selection-mode="multiple" :selection.sync="multipleSelection">
<el-table :data="tableData3" selection-mode="multiple" @selectionchange="handleSelectionChange">
<el-table-column type="selection" width="50"></el-table-column>
<el-table-column property="date" label="日期" width="120"></el-table-column>
<el-table-column inline-template property="date" label="日期" width="120">
<div>{{ row.date }}</div>
</el-table-column>
<el-table-column property="name" label="姓名" width="120"></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
Expand Down Expand Up @@ -526,6 +545,12 @@
}],
multipleSelection: []
}
},
methods: {
handleSelectionChange(val) {
this.multipleSelection = val;
}
}
}
</script>
Expand Down Expand Up @@ -584,16 +609,15 @@
| border | 是否带有纵向边框 | boolean | | false |
| selectionMode | 列表项选择模式 | string | 'single', 'multiple', 'none' | 'none' |
| allowNoSelection | 单选模式是否允许选项为空 | boolean | | false |
| selection | 多选模式下返回数组,单选模式下返回选中的元素。 | array/object | | |
| fixedColumnCount | 固定列的个数 | number | | 0 |

## el-table 事件
| 事件名 | 说明 | 参数 |
| ---- | ---- | ---- |
| selection-change | 当选择项发生变化时会触发该事件 | selected |
| cell-mouse-enter | 当单元格 hover 进入时会触发该事件 | row, column, cell, event |
| cell-mouse-leave | 当单元格 hover 退出时会触发该事件 | row, column, cell, event |
| cell-click | 当某个单元格被点击时会触发该事件 | row, column, cell, event |
| selectionchange | 当选择项发生变化时会触发该事件 | selected |
| cellmouseenter | 当单元格 hover 进入时会触发该事件 | row, column, cell, event |
| cellmouseleave | 当单元格 hover 退出时会触发该事件 | row, column, cell, event |
| cellclick | 当某个单元格被点击时会触发该事件 | row, column, cell, event |

## el-table-column API
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
Expand All @@ -604,3 +628,6 @@
| sortable | 对应列是否可以排序 | boolean | | false |
| type | 对应列的类型。如果设置了 `selection` 则显示多选按钮,如果设置了 `index` 则显示该行的索引(从 1 开始计算) | string | 'selection', 'index' | 0 |
| formatter | 用来格式化内容,在 formatter 执行的时候,会传入 row 和 column | function | | |
| show-tooltip-when-overflow | 当过长被隐藏时显示 tooltip | Boolean | | false |
| inline-template | 指定该属性后可以自定义 column 模板,参考多选的时间列,通过 row 获取行信息。此时不需要配置 property 属性 | | |

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"q": "^1.4.1",
"uppercamelcase": "^1.1.0",
"vue-loader": "^9.3.2",
"vue": "^2.0.0-rc.1",
"vue": "^2.0.0-rc.2",
"vue-markdown-loader": "^0.4.0",
"vue-popup": "^0.2.2",
"vue-router": "^2.0.0-beta.2"
Expand Down
155 changes: 155 additions & 0 deletions packages/table/src/table-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const getColumnById = function(grid, columnId) {
let column = null;
grid.columns.forEach(function(item) {
if (item.id === columnId) {
column = item;
}
});
return column;
};

const getColumnByCell = function(grid, cell) {
const matches = (cell.className || '').match(/grid_[^\s]+/gm);
if (matches) {
return getColumnById(grid, matches[0]);
}
return null;
};

import { getValueByPath, getCell, orderBy, getChild } from './util';

export default {
props: {
columns: {},
data: {},
fixed: {},
selection: {
default() {
return [];
}
}
},

render(h) {
return (
<table
class="el-table__body"
cellspacing="0"
cellpadding="0"
border="0">
<tbody>
{
this._l(this.data, (row, $index) =>
<tr
on-click={ ($event) => this.handleClick($event, row) }
on-mouseenter={ _ => this.handleMouseEnter($index) }
class={{
'current-row': row === this.$parent.$parent.selected,
'hover': this.$parent.$parent.hoverRowIndex === $index,
'positive-row': row.$positive,
'info-row': row.$info,
'warning-row': row.$warning,
'negative-row': row.$negative
}}>
{
this._l(this.columns, (column) =>
<td
class={ column.id }
on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) }
on-mouseleave={ this.handleCellMouseLeave }>
{
column.template
? column.template.call(this._renderProxy, h, { row, column, $index })
: <div class="cell">{ this.$getPropertyText(row, column.property, column.id) }</div>
}
</td>
).concat(this.fixed ? <td class="gutter" /> : '')
}
</tr>
)
}
</tbody>
</table>
);
},

data() {
return {
tooltipDisabled: true
};
},

filters: {
orderBy
},

methods: {
handleCellMouseEnter(event, row) {
let grid = this.$parent;
const cell = getCell(event);

if (cell) {
const column = getColumnByCell(grid, cell);
const hoverState = grid.hoverState = { cell: cell, column: column, row: row };
grid.$emit('cellmouseenter', hoverState.row, hoverState.column, hoverState.cell, event);
}

// 判断是否text-overflow, 如果是就显示tooltip
const cellChild = getChild(event);

if (cellChild.scrollWidth > cellChild.offsetWidth) {
this.tooltipDisabled = false;
} else {
this.tooltipDisabled = true;
}
},

handleCellMouseLeave(event) {
let grid = this.$parent;
const cell = getCell(event);

if (cell) {
const oldHoverState = grid.hoverState;
grid.$emit('cellmouseleave', oldHoverState.row, oldHoverState.column, oldHoverState.cell, event);
}
},

handleMouseEnter(index) {
this.$parent.hoverRowIndex = index;
},

handleClick(event, row) {
let grid = this.$parent;
const cell = getCell(event);

if (cell) {
const column = getColumnByCell(grid, cell);
if (column) {
grid.$emit('cellclick', row, column, cell, event);
}
}

if (grid.selectionMode === 'single') {
grid.selected = row;
grid.$emit('selectionchange', row);
}

grid.$emit('rowclick', row, event);
},

handleCreate(vm) {
document.body.appendChild(vm.$refs.popper);
vm.updatePopper();
},

$getPropertyText(row, property, columnId) {
let grid = this.$parent;
const column = getColumnById(grid, columnId);
if (column && column.formatter) {
return column.formatter(row, column);
}

return getValueByPath(row, property);
}
}
};
Loading

0 comments on commit 62e3e76

Please sign in to comment.