I'm working on a table in Meteor template, where the is a boolean field "emergency" I would like to display in the table the cells where there is the "emergency" flag FIRST, and then the others ...
How can I do that please ?
here is the find, I tried to sort(), find and sort inside but It doesn't work .. :/
Template.actionsList.helpers({
actions: function() {
return Actions.find();
}
});
Thanks in advance :)
I get the error: Exception in template helper: TypeError: Cannot read property 'hasOwnProperty' of null
My code is:
Session.set('emergency', false);
Template.actionForm.onRendered(function () {
var $elem = this.$('.emergency');
$elem.checkbox('set ' + (Session.get('emergency') ? 'checked' : 'unchecked'));
$elem.checkbox({
onChange: function () {
Session.set('emergency', !Session.get('emergency'));
}
});
});
Template.actionForm.events({
'submit .new-action': function(event) {
event.preventDefault();
var emergency = Session.get('emergency');
...
Actions.insert({
emergency: emergency
....
Thanks for the help
Use underscore's sortBy()
method to sort on objects checking if the 'emergency' field exists via the hasOwnProperty()
native method:
Template.actionsList.helpers({
actions: function() {
var actions = Actions.find().fetch();
return _.sortBy(actions, function (a) { return !a.hasOwnProperty('emergency'); });
}
});
Check the demo below.
var actions = [ { "_id" : "ukn9MLo3hRYEpCCty", "field" : "foo" }, { "_id" : "ukn9MLo3hRYEpCCty", "field" : "bar", "emergency": true }, { "_id" : "WMHWxeymY4ATWLXjz", "field" : "abc", "emergency": false }, { "_id" : "5SXRXraariyhRQACe", "field" : "xyz" } ]; var result = _.sortBy(actions, function (a) { return !a.hasOwnProperty('emergency'); }); pre.innerHTML = JSON.stringify(result, undefined, 4);
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script> <pre id="pre"></pre>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.