【问题标题】:Emberjs sort content by date using sortPropertiesEmberjs 使用 sortProperties 按日期对内容进行排序
【发布时间】:2013-11-11 09:55:03
【问题描述】:

我正在尝试使用 Emberjs sortProperties 在此 jsfiddle 中按日期对内容进行排序。我的模型有一个 startTime 属性,我尝试对其进行排序,但它不起作用。然后,我在控制器中创建了一个名为 todayEvent 的计算属性,该属性返回与传入日期匹配的事件,我尝试对其进行排序,但它也不起作用。我要做的只是列出在同一天发生的事件,但是以这样一种方式,即发生事件的一天中的时间按升序排序,例如,应该列出 10am 发生的事件在那些发生在 12pm 之前。

这是jsfiddle

型号:

App.TimeSlot = DS.Model.extend( {
  startTime: DS.attr('date'),
  endTime: DS.attr('date'),
  allDay: DS.attr('boolean'),
  soldOut: DS.attr('boolean')
});

控制器

App.TimeSlotController = Ember.ArrayController.extend({
 content: [ ],

 sortProperties: ['todayEvent'],
 sortAscending: true,

 day: Ember.A(['2013-10-25']),

 todayEvent: function(){
  self = this;
  u = self.get('content'); 
  console.log('u', u);
  kl =  u.filter(function(availableSlot) {
   console.log ('a', availableSlot.get('startTime') );

 return (moment(availableSlot.get('startTime')).format("YYYY-MM-DD") ==  self.get('day').toString() );
  }); 

   return kl;  
 }.property('day', 'content@each'),


});

fixtureAdapter

App.TimeSlot.FIXTURES = [

  {
    id: 3,
    startTime: moment.utc('2013-10-25T12:30:00+01:00',"YYYY-MM-DD HH:mm:ss" ),    
    allDay: true
  },

 {
   id: 4,
   startTime: moment.utc('2013-10-25T10:10:00+01:00',"YYYY-MM-DD HH:mm:ss" ),    
   allDay: true
},

 {
  id: 5,    
  startTime: moment.utc('2013-10-23 00:00 +01:00', "YYYY-MM-DD HH:mm"),    
  allDay: true
 }
];

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    我设法解决了@edu,但感谢您提供帮助。你可以看到一个工作的jsfiddle

    有两种方法,都使用 Ember.computed.sort

     sortedTime: Ember.computed.sort('todayEvent',       function(a, b){
    
        if (
          moment(Ember.get(a, 'startTime') ) >  moment(Ember.get(b, 'startTime') ) 
        ){
    
       //returns morning timings before afternoon timings
       //api doc says return 1 when 'a' should come after 'b'
       //http://emberjs.com/api/#method_computed_sort
    
       return 1;
    
       } else if ( 
          moment(Ember.get(a, 'startTime') ) <   moment(Ember.get(b, 'startTime') ) 
       )
       {
        //returns afternoon timings before morning timings
       //api docs says return -1, when 'a' should come before 'b'
        return -1;
       }
         return 0;
    
      })
    

    第二种方法

      //adapted from http://jsbin.com/OjoXOqE/9/edit
    
      sortedContent: Ember.computed.sort('content.@each.startTime', function(a, b){
    
        //the this keyword does not work here, 
        //throws "Object #<Object> has no method 'get'"
        //so we use the Ember keyword to get it working
    
       var ap = moment(Ember.get(a, 'startTime')),
           bp = moment(Ember.get(b, 'startTime'))
    
      //we return morning before afternoon times 
        if(ap !== bp) {
          return ap - bp;
        }
    
      })
    

    【讨论】:

      【解决方案2】:

      使用.sortBy() 对包含日期的对象数组进行排序。

      在您的模型中:

      fooDate: DS.attr('date')
      

      在您的组件/控制器/路由中:

      dateSortedFoos: function() {
        return this.get('model.foos').sortBy('fooDate');
      }.property('model.foos.@each'),
      

      无需遍历或使用 Moment JS。

      适用于"2015-09-11T08:15:00+10:00"这种格式的日期。

      供参考:我使用的是 Ember 1.13.10 和 Ember Data 1.13.9。

      【讨论】:

        【解决方案3】:

        您可以简单地在您的ArrayController 上覆盖sortFunction。见Sort Function

        App.TimeSlotController = Ember.ArrayController.extend({
          sortProperties: ['startTime'],
          sortAscending: true,
          sortFunction: function (dateX, dateY) {
            return Ember.compare(dateX.getTime(), dateY.getTime());
          }
        

        【讨论】:

        • 除了日期值存储为字符串而不是对象之外,这将起作用。
        【解决方案4】:

        控制器的排序内容存在于名为arrangementContent的属性中,所以在控制器内部你应该调用

        this.get('arrangedContent')
        

        或在车把中

        {{#each arrangedContent}}
        

        希望有帮助

        【讨论】:

        • 感谢您的浏览。在发布问题之前或之前,我在车把中尝试过 {{#each mappedContent}} 并且它没有对日期进行排序。所以这一次,我尝试了你的第一个建议,在 todayEvent 中使用 self.get('arrangedContent') 而不是 self.get('content')属性,但它没有工作。这是 jsfiddle incorporating both of your suggestions
        • todayEvent 应该是 arrayController 内容中每个项目的属性,而不是 arrayController 的属性,就像它现在在您的代码中一样
        • 对不起,您介意解释一下您的意思,todayEvent 应该是 arrayController 内容中每个项目的属性。您是说 todayEvent 应该移至 itemcontroller,因为这就是我理解您的评论的方式,并且现在将尝试这样做。谢谢。
        • 是的应该移到一个itemController
        • 谢谢。我将其移至 itemController in this jsfiddle,但仍未按日期排序。
        【解决方案5】:

        我知道这是一个老问题,但我想我会发布一个新的、更“最新”的答案。

        要按日期对可迭代对象进行排序,您可以按以下方式使用 Ember.computed.sort:

        export default Ember.Component.extend({
        //...stuff
        eventsArray: [
          {eventName: 'Event One', date: dateObject1},
          {eventName: 'Event One', date: dateObject2},
        ],
        eventsArraySortDefinition: ['date:ascending],
        sortedStuff: Ember.computed.sort('eventsArray', 'eventsArraySortDefinition'),
        //...stuff
        })
        

        值得注意的是,当您定义类型为“日期”(date: DS.attr('date'))的模型属性时,字符串格式的日期(如“2016-10-17”)将被很好地转换并存储在模型中Date 对象。排序时,日期会显式转换为时间戳,无需自行转换。

        【讨论】:

        • Greg,您的代码有效,但需要在 ['date:ascending'] 中使用短 'asc' 或 'desc'。
        猜你喜欢
        • 2011-12-06
        • 1970-01-01
        • 2012-02-22
        • 2020-10-30
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多