Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用空单元格进行Vue排序,结果不同_Javascript_Vue.js_Vue Component - Fatal编程技术网

Javascript 使用空单元格进行Vue排序,结果不同

Javascript 使用空单元格进行Vue排序,结果不同,javascript,vue.js,vue-component,Javascript,Vue.js,Vue Component,我创建了一个根据asc和desc顺序排序的表。当单元格不为空时,它可以正常工作,但当单元格为空时,每次在升序和降序之间切换时,结果都会发生变化 这是我的分类代码: methods:{ sort:function(filter) { //if filter == current sort, reverse if(filter === this.currentSort) { this.currentSortDir = this.currentSortD

我创建了一个根据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;
      });
    }
    }
计算:{
sortedPeople:function(){//按当前排序方向排序
返回此.people.sort((a,b)=>{
设修饰符=1;
if(this.currentSortDir=='desc')修饰符=-1;
如果(a[this.currentSort]==null | | b[this.currentSort]==null){
返回-1;
}
if(a[this.currentSort]b[this.currentSort]){
返回1*修饰符;
}
返回0;
}).filter((行,索引)=>{//限制视图,由于分页而不重复
让start=(this.currentPage-1)*this.pageSize;
让end=this.currentPage*this.pageSize;
如果(索引>=start&&index
我试图将空单元格排序到最后,但我的方法无法100%工作,而且我真的不明白为什么它们会在开关之间发生变化

编辑:我调整了我的代码,所以现在它们都被排序在一起,但是它们在开始时排序在“a”之前,而不是在结束时,我不确定如何将其排序到结尾

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;
  });
}
sortedPeople:function(){//按当前排序方向排序
返回此.people.sort((a,b)=>{
设修饰符=1;
if(this.currentSortDir=='desc')修饰符=-1;
如果(a[this.currentSort]==null){//更改了代码
a[this.currentSort]=“”;
}else if(b[this.currentSort]==null){
b[this.currentSort]=“”;
}
如果(a[this.currentSort]b[this.currentSort]){
返回1*修饰符;
}
返回0;
}).filter((行,索引)=>{//限制视图,由于分页而不重复
让start=(this.currentPage-1)*this.pageSize;
让end=this.currentPage*this.pageSize;
如果(索引>=start&&index
您需要明确检查空字符串(或
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;
})
.sort((a,b)=>{
const aValue=a[this.currentSort];
const bValue=b[this.currentSort];
if(aValue==bValue)返回0;
如果(!aValue)返回1;
如果(!bValue)返回-1;
设方向=(aValue
您的意思是希望空单元格(我想是字符串)出现在排序列表的末尾,而不管排序方向如何?啊,是的,理想情况下是这样!或者至少放在一起,而不是分散在按字母顺序正确排序的列表中。