Сортировка Vue с пустыми ячейками, приводящая к различным результатам - PullRequest
0 голосов
/ 18 октября 2018

У меня есть таблица, которая сортируется в зависимости от порядка asc и desc.Он отлично работает, когда ячейки не пусты, но когда он пуст, результаты меняются каждый раз, когда вы переключаетесь между восходящим и нисходящим.

Вот мой код сортировки:

methods:{
    sort:function(filter) {
      //if filter == current sort, reverse
      if(filter === this.currentSort) {
        this.currentSortDir = this.currentSortDir==='asc'?'desc':'asc';
      }
      this.currentSort = filter;
    },

И вычислено:

  computed:{
    sortedPeople:function() { //sort by current sorting direction
       return this.people.sort((a,b) => {

        let modifier = 1;
        if(this.currentSortDir === 'desc') modifier = -1;

        if(a[this.currentSort] == null || b[this.currentSort] == null){
            return -1;
        }

        if(a[this.currentSort] <= b[this.currentSort]){ 

            return -1 * modifier;
        }
        if(a[this.currentSort] > b[this.currentSort]) {

            return 1 * modifier; 
        }
        return 0;
      }).filter((row, index) => { //limit view and not repeat due to pagination
        let start = (this.currentPage-1)*this.pageSize;
        let end = this.currentPage*this.pageSize;
        if(index >= start && index < end) return true;
      });
    }
    }

Я пытался отсортировать пустые ячейки до конца, но мой метод нене работает на 100%, и я не совсем понимаю, почему они переключаются между коммутаторами.

Редактировать: я изменил свой код так, что теперь они все отсортированы вместе, однако они сортируются перед «а» в начале, а не в конце, и я не уверен, как отсортировать его вконец.

sortedPeople:function() { //sort by current sorting direction
   return this.people.sort((a,b) => {

    let modifier = 1;
    if(this.currentSortDir === 'desc') modifier = -1;

    if(a[this.currentSort] == null){ //CHANGED CODE 
        a[this.currentSort] = "";
    } else if (b[this.currentSort] == null){
        b[this.currentSort] = "";
    }

    if(a[this.currentSort] < b[this.currentSort]){ 

        return -1 * modifier;
    }

    if(a[this.currentSort] > b[this.currentSort]) {

        return 1 * modifier; 
    }
    return 0;
  }).filter((row, index) => { //limit view and not repeat due to pagination
    let start = (this.currentPage-1)*this.pageSize;
    let end = this.currentPage*this.pageSize;
    if(index >= start && index < end) return true;
  });
}

1 Ответ

0 голосов
/ 18 октября 2018

Вам необходимо явно проверить наличие пустых (или null) строк без учета направления сортировки;что-то вроде следующего

.sort((a,b) => {
    const aValue = a[this.currentSort];
    const bValue = b[this.currentSort];

    if (aValue === bValue) return 0;

    if (!aValue) return 1;
    if (!bValue) return -1;

    let direction = (aValue < bValue) ? -1 : 1;
    if (this.currentSortDir === 'desc') direction *= -1;
    return direction;
})
...