vue.js - vue js 2 对表格进行排序

标签 vue.js vuejs2

我有 2 个关于 vue.js 2 的问题,这里有一个 fiddle :https://jsfiddle.net/tmun9cxa/1/

  1. 当您单击列标题时,为什么我的排序不起作用?解决方法是什么?

  2. 如何让搜索输入字段只搜索 pn 列?

我发现的很多示例都使用 vue1 并且已过时。

<input type="text" value="" v-model="search" placeholder="Search">

<table style="text-align: center;">
    <thead>
        <tr>
            <th v-for="column in columns">
                <a 
                    href="#"
                    v-on:click="sort(column.shortcode)">{{column.label}}
                </a>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr v-for="(product) in products">
            <td>{{product.pn}}</td>
            <td>{{product.od}}</td>
            <td>{{product.id}}</td>
            <td>{{product.thickness}}</td>
            <td>{{product.lo}}</td>
            <td>{{product.weight}}</td>
        </tr>
    </tbody>
</table>

这里是javascript

var vm = new Vue({
        el: '#app',
        data: {
            currentSort: 'pn',
            currentSortDir: 'asc',
            search: '',
            columns: [
                { label: 'P/N', shortcode: 'pn' },
                { label: 'OD (De,mm)', shortcode: 'od' },
                { label: 'ID (De,mm)', shortcode: 'id' },
                { label: 'Thickness (De,mm)', shortcode: 'thickness' },
                { label: 'LO', shortcode: 'lo' },
                { label: 'Weight (kg/1000)', shortcode: 'weight' },
            ], // columns
            products: [
                { 
                    pn: 170158,
                    od: 13,
                    id: .44,
                    thickness: 1,
                    lo: .45,
                    weight: .7
                },{ 
                    pn: 1803561,
                    od: 12,
                    id: .8,
                    thickness: .7,
                    lo: .11,
                    weight: .5
                },{ 
                    pn: 170149,
                    od: 9,
                    id: .64,
                    thickness: .6,
                    lo: .75,
                    weight: .3
                },{ 
                    pn: 150149,
                    od: 15,
                    id: .22,
                    thickness: .3,
                    lo: .55,
                    weight: .9
                },
            ], // products
        },
        methods: {
            sort:function(col) {
                //console.log( 'current: '+this.currentSort );
                //console.log( 'col: '+col );
                //var colthing = col;

                // if you click the same label twice
                if(this.currentSort == col){
                    console.log( 'same col: '+col );
                    // sort by asc
                    this.products = this.products.sort((a, b) => {
                        return a.col >= b.col;
                    });
                }else{
                    this.currentSort = col;
                    console.log( 'diff col: '+col );
                    // sort by desc
                    this.products = this.products.sort((a, b) => {
                        return a.col <= b.col;
                    });
                } // end if

            }, // sort

        }, // methods
    }); // vue

最佳答案

如前所述,列排序不起作用,因为您需要使用 a[col]而不是 a.col

此外,您应该考虑使用计算值而不是修改原始数据。这也使过滤更加容易。

这是更新后的脚本(注意 <tr v-for="(product) in products"> 需要是 <tr v-for="(product) in showProducts"> 才能工作)

var vm = new Vue({
  el: '#app',
  data: {
    currentSort: 'pn',
    currentSortDir: 'asc',
    search: '',
    columns: [
      { label: 'P/N', shortcode: 'pn' },
      /// more columns ...
    ], // columns
    products: [
      //.... objects
    ], // products
  },
  computed: {
    showProducts() {
      return this.products.filter(a => {
        console.log(a.pn)
        return (a.pn + '').includes(this.search)
      })
        .sort((a, b) => {
        if (this.currentSortDir === 'asc') {
	        return a[this.currentSort] >= b[this.currentSort];      
        }
        return a[this.currentSort] <= b[this.currentSort];
      })
    },
  },
  methods: {
    sort:function(col) {
      // if you click the same label twice
      if(this.currentSort == col){
        this.currentSortDir = this.currentSortDir === 'asc' ? 'desc' : 'asc';
      }else{
        this.currentSort = col;
        console.log( 'diff col: '+col );
      } // end if

    }, // sort

  }, // methods
}); // vue

最后, fiddle :https://jsfiddle.net/tmun9cxa/2/

关于vue.js - vue js 2 对表格进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374892/

相关文章:

spring-boot - Vue/SpringBoot : Why does my JSESSIONID keeps changing

javascript - 在 Vue.config.js 中配置 Vue-SVG-Loader

css - 不完全确定我应该在 Vue SPA 中的什么地方写我的 CSS

javascript - Vue.js 中 v-model 指令的安全导航

javascript - 如何在 vue-tables-2 服务器端添加范围过滤器

javascript - TypeError : state. categoriesState.push 不是函数VUEX

vuejs2 - 将 v-btn-toggle 扩展至容器宽度的 100%

javascript - ePub 未在 futurepress/epub.js 中定义

vue.js - 如何在 Vue Chart.js 中使用 API 数据

php - 如何将 vue Prop 数据附加到图像源的基本路径