html
<el-table
ref="singleTable"
:data="tableData"
highlight-current-row
style="width: 100%"
size="medium"
border
@cell-click="transferId"
~~:default-sort="{prop: 'id_purchase', order: 'descending'}"~~
>
<el-table-column type="index" width="50"></el-table-column>
<el-table-column property="id" label="产品" width="120"></el-table-column>
<el-table-column property="id_pageview" label="pageview" width="120" sortable></el-table-column>
<el-table-column property="id_checkout" label="checkout" width="120" sortable></el-table-column>
<el-table-column
property="id_purchase"
label="purchase"
width="120"
sortable
~~:sort-method="sortByPurchase"~~
></el-table-column>
<el-table-column property="belong" label="belong" width="120"></el-table-column>
<el-table-column property="pimg" label="pimg" width="240">
<template slot-scope="scope">
<img :src="scope.row.pimg" alt style="width:50px;height:50px;" />
</template>
</el-table-column>
<el-table-column property="url" label="url" width="240">
<template slot-scope="scope">
<a
:href="scope.row.url"
style="text-decoration:none;font-size:14px;"
target="_blank"
>{{scope.row.url}}</a>
</template>
</el-table-column>
<el-table-column property="nums" label="访问IP"></el-table-column>
</el-table>
可以通过 Table 的default-sort
属性设置默认的排序列和排序顺序。在上面的代码已用~~~标示出。
如果想让哪列进行排序直接加sortable
即可。
如果要设置默认排序,需要设置sort-method
,设置sort-method
的前提是sortable=“true”
方法跟Array.sort(function(a,b))基本一样.js代码
如下:
methods: {
sortByPurchase(obj1, obj2) {
var a = obj1.id_purchase;
var b = obj2.id_purchase;
return a - b;
}
}