admin管理员组文章数量:1399260
I'm trying to make a simple sort function, but it seems not work.
var Main = {
data() {
return {
tableData: [{
value: '1.799,37',
name: 'Tom 3'
}, {
value: '2.183,88',
name: 'Tom 2'
}, {
value: '3.837,05',
name: 'Tom 4'
}, {
value: '769,8',
name: 'Tom 6'
}, {
value: '111,8',
name: 'Tom 6'
}, {
value: '999,8',
name: 'Tom 6'
}]
}
},
methods: {
test: function(a, b) {
return a.value - b.value;
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
@import url("//unpkg/[email protected]/lib/theme-default/index.css");
<script src="//unpkg/vue/dist/vue.js"></script>
<script src="//unpkg/[email protected]/lib/index.js"></script>
<div id="app">
<template>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="value" label="Value" sortable :sort-method=test width="180"></el-table-column>
<el-table-column prop="name" sortable label="Name" width="180"></el-table-column>
</el-table>
</template>
</div>
I'm trying to make a simple sort function, but it seems not work.
var Main = {
data() {
return {
tableData: [{
value: '1.799,37',
name: 'Tom 3'
}, {
value: '2.183,88',
name: 'Tom 2'
}, {
value: '3.837,05',
name: 'Tom 4'
}, {
value: '769,8',
name: 'Tom 6'
}, {
value: '111,8',
name: 'Tom 6'
}, {
value: '999,8',
name: 'Tom 6'
}]
}
},
methods: {
test: function(a, b) {
return a.value - b.value;
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
@import url("//unpkg./[email protected]/lib/theme-default/index.css");
<script src="//unpkg./vue/dist/vue.js"></script>
<script src="//unpkg./[email protected]/lib/index.js"></script>
<div id="app">
<template>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="value" label="Value" sortable :sort-method=test width="180"></el-table-column>
<el-table-column prop="name" sortable label="Name" width="180"></el-table-column>
</el-table>
</template>
</div>
Share
Improve this question
edited Jun 16, 2020 at 18:21
Alessio Cantarella
5,2113 gold badges29 silver badges36 bronze badges
asked Jun 16, 2020 at 18:13
jarwinjarwin
6683 gold badges11 silver badges29 bronze badges
2 Answers
Reset to default 4 sortName(a,b) {
if (a.name.toUpperCase() < b.name.toUpperCase()) return -1;
if (a.name.toUpperCase() > b.name.toUpperCase()) return 1;
return 0;
}
In you array tableData
, each object has a value
who is a string.
So your sort method in paring strings. But here I think you want to sort them by number.
What you need to do is update your method like that :
methods: {
test: function(a, b) {
return this.toFloat(a.value) - this.toFloat(b.value)
},
toFloat: function(num) {
return parseFloat(num.replace('.','').replace(',','.'))
}
}
Note the replace
is to make your strings valid numbers
var Main = {
data() {
return {
tableData: [{
value: '1.799,37',
name: 'Tom 3'
}, {
value: '2.183,88',
name: 'Tom 2'
}, {
value: '3.837,05',
name: 'Tom 4'
}, {
value: '769,8',
name: 'Tom 6'
}, {
value: '111,8',
name: 'Tom 6'
}, {
value: '999,8',
name: 'Tom 6'
}]
}
},
methods: {
test: function(a, b) {
return this.toFloat(a.value) - this.toFloat(b.value)
},
toFloat: function(num) {
return parseFloat(num.replace('.','').replace(',','.'))
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
@import url("//unpkg./[email protected]/lib/theme-default/index.css");
<script src="//unpkg./vue/dist/vue.js"></script>
<script src="//unpkg./[email protected]/lib/index.js"></script>
<div id="app">
<template>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="value" label="Value" sortable :sort-method=test width="180"></el-table-column>
<el-table-column prop="name" sortable label="Name" width="180"></el-table-column>
</el-table>
</template>
</div>
本文标签: javascriptElement UI sort values in a table not workingStack Overflow
版权声明:本文标题:javascript - Element UI sort values in a table not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744121968a2591782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论