vue.js - 带有用于排序的 Bootstrap 图标的 VueJS

标签 vue.js

我通过单击表格标题对列数据进行排序。

我已经根据 v-show 条件添加了 Bootstrap 图标。

之前,排序是通过单击表头来进行的,但现在它应该通过单击“当前”列右侧现在不可见的图标来进行。

需要这方面的帮助。问题的根源在 HTML 的第 13 行:

<span class="glyphicon glyphicon-sort" v-show="toolAttribute != activeColumn"></span>

https://jsfiddle.net/L5p0ngdu/2/

new Vue({
el: '#app',
data: {
results: {
  toolAttribute: [{attributeName: "Present", order: 1},{attributeName: "Present", order: 1},{attributeName: "Present", order: 1}],
  device: [
    {deviceName: "Device Name 1",
      info: [{value: true}, {value: false}, {value: true}]},
    {deviceName: "Device Name 2",
      info: [{value: false}, {value: false}, {value: false}]},
    {deviceName: "Device Name 3",
      info: [{value: true}, {value: true}, {value: true}]},
    {deviceName: "Device Name 4",
      info: [{value: false}, {value: true}, {value: false}]},
    {deviceName: "Device Name 5",
      info: [{value: true}, {value: true}, {value: true}]}
     ]
},
activeColumn: {},
currentSort:['deviceName'],
currentSortDir:'asc'
},
computed:{
    sortedResults:function() {
      return this.results.device.sort(function(a,b){
        let modifier = 1;
        if(this.currentSortDir === 'desc') modifier = -1;
        this.currentSort.forEach(x => {
        
           a = a[x];
           b = b[x];
        })
        
        if(a< b) return -1 * modifier;
        if(a > b) return 1 * modifier;
        return 0;
      }.bind(this));
    }
  },
  methods:{
  flasecond(index){
    let res = false
     this.results.device[index].info.forEach(info=> {
        if(!info.value) res = true
    })
      return res
  },
  sort:function(s) {
      //if s == current sort, reverse
      if(s.join('') === this.currentSort.join('')) {
        this.currentSortDir = this.currentSortDir==='asc'?'desc':'asc';
      }
      this.currentSort = s;
    },
}
})
.falseclass{
  background:red;
  color:white;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div id="app">
<table >
 <tr>
 <th rowspan="2" @click="sort(['deviceName'])">Device Info</th>
 </tr>
 <tr>
 <th v-for="(toolAttribute, index) in results.toolAttribute" :key="index" @click="activeColumn = toolAttribute" :class="{active: toolAttribute == activeColumn}">{{toolAttribute.attributeName}}
 <span @click="sort(['info', index, 'value']); toolAttribute.order = toolAttribute.order * (-1)" :class="toolAttribute.order > 0 ? 'glyphicon glyphicon-chevron-down' : 'glyphicon glyphicon-chevron-up'" v-show="toolAttribute == activeColumn"></span>
 <span class="glyphicon glyphicon-sort" v-show="toolAttribute != activeColumn"></span></th>
 </tr>
 <tr v-for="(device, index) in sortedResults"  >
  <td :class="{'falseclass' : flasecond(index)}">{{ device.deviceName }}</td>
  <td v-for="info in device.info" :class="{'falseclass' : !info.value}">{{info.value}}</td>
</tr>
</table>
</div>

最佳答案

在您的 jsfiddle 中,我看到您正在使用 Bootstrap 4。当 Bootstrap 迁移到 v4 时,Glyphicons 图标字体已被删除。我建议您看看这些免费替代品之一:

关于vue.js - 带有用于排序的 Bootstrap 图标的 VueJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55761832/

相关文章:

vue.js - 将 track-by 属性与 v-for 一起使用有什么意义?

javascript - vsCode 对 vue.js 文档中明显有效的代码发出警告

javascript - 如何处理 Vue js 中的样式类?

javascript - 如何在加载/显示页面之前使用 Vue(路由器)获取 GET 请求变量

javascript - Vue 有类似 Svelte 的命运操作符的东西吗?

webpack - 如何使用 webpack 导入 min 文件

vuejs2 - VueJs 开发工具面板未显示

laravel - 我的应用架构还是SOA吗?

javascript - 如何在 Vue JS 观察器中更改对象的属性而不产生无限循环?

vue.js - 使用 Vee Validate 创建自定义验证时如何使用 Vuex 状态