简体   繁体   English

Knockout排序方法在chrome中不起作用

[英]Knockout sort method doesn't work in chrome

I have an application which creates and manages activities. 我有一个创建和管理活动的应用程序。 I use Knockout.js to hold activities in observable array. 我使用Knockout.js来保存可观察数组中的活动。 Whenever new activity is created it is inserted into an array. 每当创建新活动时,它都会插入到数组中。 One of the activity properties is date. 其中一个活动属性是日期。 I want to order activities by date after creating new one, to display it properly in UI. 我想在创建新活动之后按日期订购活动,以便在UI中正确显示它。 This is a function I use for it: 这是我用它的一个功能:

self.Activities.unshift(activity);
self.Activities.sort(function(a, b) {
    var dateA = new Date(a.date() + " 00:00:00");
    var dateB = new Date(b.date() + " 00:00:00");
    return dateA > dateB;
});

And it works perfectly in Firefox (v 16.0.2) but doesn't work in Chrome (v 23.0.1...), Safari or IE 它在Firefox(v 16.0.2)中完美运行,但在Chrome(v 23.0.1 ...),Safari或IE中不起作用

Why? 为什么? What is the workaround? 解决方法是什么? If any? 如果有的话?

The comparer function that you pass needs to sort needs to return a number. 您传递的需要进行sort的比较器函数需要返回一个数字。 Some browsers are forgiving and work with a boolean. 有些浏览器宽容并使用布尔值。

Generally you would return -1 or 1. Something like: 通常你会返回-1或1.像:

return dateA > dateB ? 1 : -1;

I was using a wrong Date format. 我使用了错误的日期格式。 For some reason Chrome doesn't like: d/MM/yyyy, when I used yyyy/MM/d everything works fine 出于某种原因,Chrome不喜欢:d / MM / yyyy,当我使用yyyy / MM / d时一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM