<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号',
                  date:1448236800000
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号',
                  date:1479859200000
                }],
                
                columns : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                  align:'center'
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                  width:'180',
                  align:'left'
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                  align:'right'
                }],
                columns1 : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                  align:'center'
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                  width:'180',
                  align:'left'
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                  align:'right'
                }, {
                  title: '注册时间',
                  index: 'date',
                  key: 'date'
                }],
                tableData: [{
                  date: '2016-05-02',
                  name: '李依晓',
                  address: '上海市普陀区金沙江路 1518 弄'
                }, {
                  date: '2016-05-04',
                  name: '孙艺珊',
                  address: '上海市普陀区金沙江路 1517 弄'
                }, {
                  date: '2016-05-01',
                  name: '二嘎子',
                  address: '上海市普陀区金沙江路 1519 弄'
                }, {
                  date: '2016-05-03',
                  name: '张笑',
                  address: '上海市普陀区金沙江路 1516 弄'
                }],
                scrollData:(function(){
                    const data=[];
                    for (let i = 0; i < 100; i++) {
                      data.push({
                        key: i,
                        name: `Utear name ${i}`,
                        age: 32,
                        address: `London, No. ${i}`,
                      });
                    }
                    return data;
                })()
            }
        },
        methods:{
            deleteRow(idx,row){
                this.dataSource.splice(idx,1)
            },
            selection(data){
                console.log(data)
            },
            formatter(row,col){
                return row.age+'岁';
            }
        },
        filters:{
            format(row){
                return new Date(row.date).toLocaleDateString();
            }
        }
    }
</script>
# Table表格

展示行列数据。

## 何时使用

* 当有大量结构化的数据需要展现时；
* 当需要对数据进行排序、搜索、分页、自定义操作等复杂行为时。


### 基本使用

::: demo 当ut-table元素中注入data对象数组后，在ut-table-column中用index属性来对应对象中的键名即可填入数据，用title属性来定义表格的列名。可以使用width属性来定义列宽。
```html
<template>
    <ut-table :source='dataSource'>
        <ut-table-column v-for='col in columns' :title='col.title' :key='col.key' :index='col.index'>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号'
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号'
                }],
                
                columns : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                }],
                
            }
        }
    }
</script>
```
:::

### 带斑马纹表格

::: demo stripe属性可以创建带斑马纹的表格。它接受一个Boolean，默认为false
```html
<template>
    <ut-table :source='dataSource' stripe>
        <ut-table-column v-for='col in columns' :title='col.title' :key='col.key' :index='col.index'>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号'
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号'
                }],
                
                columns : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                }],
                
            }
        }
    }
</script>
```
:::

### 带边框表格

::: demo 默认情况下，ut-table 组件是不具有竖直方向的边框的，如果需要，可以使用border属性，它接受一个Boolean
```html
<template>
    <ut-table :source='dataSource' border>
        <ut-table-column v-for='col in columns' :title='col.title' :key='col.key' :index='col.index'>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号'
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号'
                }],
                
                columns : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                }],
                
            }
        }
    }
</script>
```
:::


### 固定表头

::: demo 定义了height属性，即可实现固定表头的表格，而不需要额外的代码。
```html
<template>
    <ut-table :source='scrollData' :height='250'>
        <ut-table-column v-for='col in columns' :title='col.title' :key='col.key' :index='col.index'>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                columns : [{
                   title: '姓名',
                   index: 'name',
                   key: 'name',
                   align:'center'
                 }, {
                   title: '年龄',
                   index: 'age',
                   key: 'age',
                   width:'180',
                   align:'left'
                 }, {
                   title: '住址',
                   index: 'address',
                   key: 'address',
                   align:'right'
                 }],
                 columns1 : [{
                   title: '姓名',
                   index: 'name',
                   key: 'name',
                   align:'center'
                 }, {
                   title: '年龄',
                   index: 'age',
                   key: 'age',
                   width:'180',
                   align:'left'
                 }, {
                   title: '住址',
                   index: 'address',
                   key: 'address',
                   align:'right'
                 }, {
                   title: '注册时间',
                   index: 'date',
                   key: 'date'
                 }],
                scrollData:(function(){
                    const data=[];
                    for (let i = 0; i < 100; i++) {
                      data.push({
                        key: i,
                        name: `Utear name ${i}`,
                        age: 32,
                        address: `London, No. ${i}`,
                      });
                    }
                    return data;
                })()
            }
        }
    }
</script>
```
:::

### 文字对齐

::: demo ut-table-column接受align属性，默认left,可选参数为left,center,right
```html
<template>
    <ut-table :source='dataSource' border>
        <ut-table-column :align='col.align' v-for='col in columns' :title='col.title' :key='col.key' :index='col.index' tooltip>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号'
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号'
                }],
                
                columns : [{
                      title: '姓名',
                      index: 'name',
                      key: 'name',
                      align:'center'
                    }, {
                      title: '年龄',
                      index: 'age',
                      key: 'age',
                      width:'180',
                      align:'left'
                    }, {
                      title: '住址',
                      index: 'address',
                      key: 'address',
                      align:'right'
                    }]               
            }
        }
    }
</script>
```
:::

### 可选择

::: demo 实现多选非常简单: 手动添加一个ut-table-column，设type属性为selection即可。<br>有两个事件<code>selection-change</code>和<code>selection-all</code>
```html
<template>
    <ut-table :source='dataSource' border @selection-change='selection' @select-all='selection'>
        <ut-table-column type='selection' :width='50' align='center'></ut-table-column>
        <ut-table-column v-for='col in columns' :title='col.title' :key='col.key' :index='col.index'>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号'
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号'
                }],
                
                columns : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                  align:'center'
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                  width:'180',
                  align:'left'
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                  align:'right'
                }]
            }
        },
        methods:{
            selection(data){
                console.log(data)
            }
        }
    }
</script>
```
:::

### 自定义列模板

::: demo 通过设置 inline-template 属性可以开启自定义模板功能，此时 ut-table-column 的上下文指的是 ut-table 所处的上下文，在 column 组件内部，可以获取到 row, $index的数据。formatter用来格式化内容	Function(row, column)
```html
<template>
    <ut-table :source='dataSource' border>
        <ut-table-column :title='columns1[0].title' :index='columns1[0].index'>
        </ut-table-column>
        <ut-table-column :title='columns1[1].title' :index='columns1[1].index' :formatter='formatter'>
        </ut-table-column>
        <ut-table-column :title='columns1[2].title' :index='columns1[2].index'>
        </ut-table-column>
        <ut-table-column :title='columns1[3].title' :index='columns1[3].index' inline-template>
            <ut-popover>
                <div slot='content'>我就是自定义的时间，不服来打我呀</div>
                <span>{{row|format}}<span>
            </ut-popover>
        </ut-table-column>
        <ut-table-column inline-template title='操作'>
            <div>
                <ut-button type='info' size='small' @click.native='deleteRow($index,row)'>操作</ut-button>
                <ut-popconfirm @confirm='deleteRow($index,row)' title='你确定要删除本行么？'>
                    <ut-button type='danger' size='small'>删除</ut-button>
                </ut-popconfirm>
            </div>
        </ut-table-column>
    </ut-table>
</template>
<script>
    export default {
        data(){
            return {
                dataSource : [{
                  key: '1',
                  name: '胡彦斌',
                  age: 32,
                  address: '人民广场解放路59号',
                  date:1448236800000
                }, {
                  key: '2',
                  name: '周小杰',
                  age: 42,
                  address: '人民广场解放路59号',
                  date:1479859200000
                }],
                
                columns1 : [{
                  title: '姓名',
                  index: 'name',
                  key: 'name',
                  align:'center'
                }, {
                  title: '年龄',
                  index: 'age',
                  key: 'age',
                  width:'180',
                  align:'left'
                }, {
                  title: '住址',
                  index: 'address',
                  key: 'address',
                  align:'right'
                }, {
                  title: '注册时间',
                  index: 'date',
                  key: 'date'
                }],      
            }
        },
        methods:{
            deleteRow(idx,row){
                this.dataSource.splice(idx,1)
            },
            formatter(row,col){
                return row.age+'岁';
            }
        },
        filters:{
            format(row){
                return new Date(row.date).toLocaleDateString();
            }
        }
    }
</script>
```
:::

### 排序

:::demo 对表格进行排序，可快速查找或对比数据。在列中设置sortable属性即可实现以该列为基准的排序，接受一个Boolean，默认为false。
```
<template>
  <ut-table
    :source="tableData"
    border
    style="width: 100%"
    >
    <ut-table-column
      index="date"
      title="日期"
      sortable
      :width="180">
    </ut-table-column>
    <ut-table-column
      index="name"
      title="姓名"
      sortable
      :width="180">
    </ut-table-column>
    <ut-table-column
      index="address"
      show-overflow-tooltip
      title="地址"
    >
    </ut-table-column>
  </ut-table>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [{
          date: '2016-05-02',
          name: '李依晓',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '孙艺珊',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '二嘎子',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '张笑',
          address: '上海市普陀区金沙江路 1516 弄'
        }]
      }
    }
  }
</script>
```
:::


### Table Attributes
| 参数      | 说明          | 类型      | 可选值                           | 默认值  |
|---------- |-------------- |---------- |--------------------------------  |-------- |
| source | 显示的数据 | array | — | — |
| height | Table 的高度，默认为自动高度。如果 height 为 number 类型，单位 px；如果 height 为 string 类型，则 Table 的高度受控于外部样式。  | string/number | — | — |
| stripe | 是否为斑马纹 table | boolean | — | false |
| border | 是否带有纵向边框 | boolean | — | false |
| fit | 列的宽度是否自撑开 | boolean | — | true |
| show-header | 是否显示表头 | boolean | - | true |
| highlight-current-row | 是否要高亮当前行 | boolean | — | false |
| row-class-name | 行的 className 的回调方法，也可以使用字符串为所有行设置一个固定的 className。 | Function(row, index)/String | — | — |
| row-style | 行的 style 的回调方法，也可以使用一个固定的 Object 为所有行设置一样的 Style。 | Function(row, index)/Object | — | — |
| row-key | 行数据的 Key，用来优化 Table 的渲染；在使用 reserve-selection 功能的情况下，该属性是必填的 | Function(row)/String | — | — |
| context | 设置上下文环境，例如设置当前上下文就是 `_self`，父级就是 `$parent`，根组件 `$root`。优先读取 column 的 context 属性。 | Object | - | Table 所处上下文 |

### Table Events
| 事件名 | 说明 | 参数 |
| ---- | ---- | ---- |
| select | 当用户手动勾选数据行的 Checkbox 时触发的事件 | selection, row |
| select-all | 当用户手动勾选全选 Checkbox 时触发的事件 | selection |
| selection-change | 当选择项发生变化时会触发该事件 | selection |
| cell-mouse-enter | 当单元格 hover 进入时会触发该事件 | row, column, cell, event |
| cell-mouse-leave | 当单元格 hover 退出时会触发该事件 | row, column, cell, event |
| cell-click | 当某个单元格被点击时会触发该事件 | row, column, cell, event |
| row-click | 当某一行被点击时会触发该事件 | row, event |
| row-dblclick | 当某一行被双击时会触发该事件 | row, event |
| header-click | 当某一列的表头被点击时会触发该事件 | column, event |
| sort-change | 当表格的排序条件发生变化的时候会触发该事件 | { column, prop, order } |
| current-change | 当表格的当前行发生变化的时候会触发该事件，如果要高亮当前行，请打开表格的 highlight-current-row 属性 | currentRow, oldCurrentRow |

### Table Methods
| 方法名 | 说明 | 参数 |
| ---- | ---- | ---- |
| clearSelection | 清空用户的选择，当使用 reserve-selection 功能的时候，可能会需要使用此方法 | selection |
| toggleRowSelection | 切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中） | row, selected |

### Table-column Attributes
| 参数      | 说明          | 类型      | 可选值                           | 默认值  |
|---------- |-------------- |---------- |--------------------------------  |-------- |
| type | 对应列的类型。如果设置了 `selection` 则显示多选框，如果设置了 `index` 则显示该行的索引（从 1 开始计算） | string | selection/index | — |
| title | 显示的标题 | string | — | — |
| index | 对应列内容的字段名，也可以使用 property 属性 | string | — | — |
| width | 对应列的宽度 | string | — | — |
| min-width | 对应列的最小宽度，与 width 的区别是 width 是固定的，min-width 会把剩余宽度按比例分配给设置了 min-width 的列 | string | — | — |
| fixed | 列是否固定在左侧或者右侧，true 表示固定在左侧 | string, boolean | true, left, right | — |
| render-header | 列标题 Label 区域渲染使用的 Function | Function(h, { column, $index }) | — | — |
| sortable | 对应列是否可以排序，如果设置为 'custom'，则代表用户希望远程排序，需要监听 Table 的 sort-change 事件 | boolean, string | true, false, 'custom' | false |
| sort-method | 对数据进行排序的时候使用的方法，仅当 sortable 设置为 true 的时候有效 | Function(a, b) | — | — |
| resizable | 对应列是否可以通过拖动改变宽度（需要在 ut-table 上设置 border 属性为真） | boolean | — | true |
| formatter | 用来格式化内容 | Function(row, column) | — | — |
| show-overflow-tooltip | 当内容过长被隐藏时显示 tooltip | Boolean | — | false |
| context | 设置上下文环境，例如设置当前上下文就是 `_self`，父级就是 `$parent`，根组件 `$root` | Object | - | Table 所处上下文 |
| inline-template | 指定该属性后可以自定义 column 模板，参考多选的时间列，通过 row 获取行信息。总共可以获取到 `{ row(当前行), column(当前列), $index(行数), store(table store) }` 以及 Table 所处的上下文环境。 | — | — |
| align | 对齐方式 | String | left, center, right | left |
| class-name | 列的 className | string | — | — |
| selectable | 仅对 type=selection 的列有效，类型为 Function，Function 的返回值用来决定这一行的 CheckBox 是否可以勾选 | Function(row, index) | — | — |
| reserve-selection | 仅对 type=selection 的列有效，类型为 Boolean，为 true 则代表会保留之前数据的选项，需要配合 Table 的 clearSelection 方法使用。 | Boolean | — | false |

