Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element UI Table #3

Open
Holybasil opened this issue Dec 25, 2018 · 0 comments
Open

Element UI Table #3

Holybasil opened this issue Dec 25, 2018 · 0 comments
Labels

Comments

@Holybasil
Copy link
Owner

ElementUI Table 使用

version 2.1.0

问题描述

有这样一例数据 需要用el-table渲染

const data = [
  {
    name: "Cake",
    hit: "30.12%"
  },
  {
    name: "Puff",
    hit: "2.13%"
  },
  {
    name: "Chocolate",
    hit: "67.3%"
  }
]

对每一个el-table-column均需设置sortable属性 使其能够排序
由于hit是字符串而要根据百分数的大小排序 所以设置sort-method

sortMethod(a, b){
  return parseFloat(a.hit) > parseFloat(b.hit)
}

此时出现第一个问题
首先 官方承认
WechatIMG55.png
然而 在我用的2.1.0版本里 parseFloat(a.hit) > parseFloat(b.hit)是不起作用的 parseFloat(a.hit) - parseFloat(b.hit)才能达到效果

具体问题描述和效果可参考 ElementUI issue 1

以上 第一个问题解决
然而 每次请求拿到的数据一开始都不要受到前端排序的干扰 所以每一次需要先调用el-table的clearSort()方法
那么第二个问题来了
clearSort()针对自定义排序方法不起作用 如果原数据没有用el-table的排序 那么这个方法没有副作用 如果已经用了el-table的排序 那么这个方法会将将数据升序排序

具体问题描述和效果可参考 ElementUI issue 2

解决办法

现阶段(2018.12.25) 采用在给el-table传递data之前
data.map(item=>({...item, hit: parseFloat(item.hit)})) 预处理
然后对于相应的el-table-column 设置formatter ${cellValue}%

@Holybasil Holybasil added js Something isn't working http produce from somewhere note and removed http produce from somewhere labels Dec 25, 2018
@Holybasil Holybasil removed note js Something isn't working labels Jul 15, 2019
@Holybasil Holybasil added the bug label Dec 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant