目前我正在使用此代码段为我的firebase数据库中的每一天设置6天和一些时间段。
fb.$set({
monday: {
name: 'Monday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
},
tuesday: {
name: 'Tuesday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
},
wednesday: {
name: 'Wednesday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
},
thursday: {
name: 'Thursday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
},
friday: {
name: 'Friday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
},
saturday: {
name: 'Saturday',
slots: {
900: {
time: '9:00am',
booked: false
},
0110: {
time: '11:00am',
booked: false
},
100: {
time: '1:00pm',
booked: false
},
300: {
time: '3:00pm',
booked: false
},
500: {
time: '5:00pm',
booked: false
},
700: {
time: '7:00pm',
booked: false
}
}
}
});
请注意订单。那为什么按字母顺序存储呢?
当我返回此数据并在页面上迭代它时,这会成为一个问题,因为日期不是您期望的顺序,而是按字母顺序返回。
有没有办法可以对此有一定程度的控制?
答案 0 :(得分:0)
您需要将一个函数传递给将执行自定义排序的orderBy过滤器。
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.days = [
{ name: 'Monday' },
{ name: 'Tuesday' },
{ name: 'Wednesday' },
{ name: 'Thursday' },
{ name: 'Friday' },
{ name: 'Saturday' },
{ name: 'Sunday' }
]
$scope.sortByWeekday = function (day) {
var weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
return weekdays.indexOf(day.name.toLowerCase());
}
}

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<div ng-repeat="day in days | orderBy:sortByWeekday">{{day.name}}</div>
</div>
</body
</html>
&#13;
答案 1 :(得分:0)
我认为你正在寻找优先权。 firebase中的订购数据首先按$优先级进行,然后按密钥进行。 Firebase中有how sorting works的大量文档可供您阅读。如果您尝试以预定顺序专门存储数据(并将其取回),我将使用$ priority。
答案 2 :(得分:0)
Firebase数据库存储JSON数据。 JSON对象中的键本质上是无序的。在大多数情况下,Firebase SDK默认会按字母顺序返回密钥。
一般情况下,Firebase建议不要使用数组,但如果这种情况是一般建议的例外,您可以这样做:
set({
1: {
name: 'Monday',
slots: {
900: { time: '9:00am', booked: false },
1100: { time: '11:00am', booked: false },
1300: { time: '1:00pm', booked: false },
1500: { time: '3:00pm', booked: false },
1700: { time: '5:00pm', booked: false },
1900: { time: '7:00pm', booked: false }
}
},
2: {
name: 'Tuesday',
slots: {
900: { time: '9:00am', booked: false },
1100: { time: '11:00am', booked: false },
1300: { time: '1:00pm', booked: false },
1500: { time: '3:00pm', booked: false },
1700: { time: '5:00pm', booked: false },
1900: { time: '7:00pm', booked: false }
}
},
3: {
name: 'Wednesday',
slots: {
...
在上面的代码片段中,我也更改了slots
的密钥。通过使用24小时表示法,他们将按照您希望的方式自动订购。
答案 3 :(得分:0)
我在上面发表评论,如果不知道项目的范围,很难知道推荐的确切方向。但是,使用日期名称作为键并将时间存储为字符串可能无法长期运行。
简单的答案是将时隙/事件的日期和时间存储为时间戳,格式为YYYYmmddHHMMSS,作为插槽的子节点。
所以例如
training_classes
training_id_1234
timestamp: 20151031130000
trainer: trainer_id1234
client: client_id1234
comment: "Algebra 1"
training_id_3445
timestamp: 20151031150000
trainer: trainer_id1234
client: client_id1234
comment: "Algebra 2"
通过这种结构,您可以在10月31日星期六轻松查询所有课程。在上述情况下,有两个,一个在下午1点,一个在下午3点。
其他时间没有数据,因此可以在firebase中预订(无需存储预订:false)。
您也可以轻松对它们进行排序,因为时间戳始终按时间顺序排列 - 存储在Firebase中的顺序并不重要。
此外,它使培训活动变得非常容易。只需更新时间戳和whammo,它就会被重新安排。