【发布时间】:2018-04-03 06:46:04
【问题描述】:
我正在尝试对“MM-DD-YYYY HH:mm:ss”格式的日期列进行排序。但是排序顺序不正确。看起来它是根据月份和日期而不是年份排序的。
Codepen - https://codepen.io/anon/pen/QmxJzw
HTML 片段:
<md-table-container>
<table md-table md-row-select="options.rowSelection" multiple="{{options.multiSelect}}" ng-model="selected" md-progress="promise">
<thead ng-if="!options.decapitate" md-head md-order="query.order" md-on-reorder="logOrder">
<tr md-row>
<th md-column md-order-by="name"><span>Dessert (100g serving)</span></th>
<!-- <th md-column md-order-by="type"><span>Type</span></th> -->
<th md-column md-order-by="dateTime"><span>Date</span></th>
<th md-column md-numeric md-order-by="calories.value" md-desc><span>Calories</span></th>
<th md-column md-numeric md-order-by="fat.value"><span>Fat (g)</span></th>
<!-- <th md-column md-numeric md-order-by="carbs.value"><span>Carbs (g)</span></th>
<th md-column md-numeric md-order-by="protein.value"><span>Protein (g)</span></th> -->
<th md-column md-numeric md-order-by="sodium.value" hide-gt-xs show-gt-md><span>Sodium (mg)</span></th>
<th md-column md-numeric md-order-by="calcium.value" hide-gt-xs show-gt-lg><span>Calcium (%)</span></th>
<th md-column md-numeric md-order-by="iron.value" hide-gt-xs show-gt-lg><span>Iron (%)</span></th>
<th md-column md-order-by="comment">
<md-icon>comments</md-icon>
<span>Comments</span>
</th>
</tr>
</thead>
<tbody md-body>
<tr md-row md-select="dessert" md-on-select="logItem" md-auto-select="options.autoSelect" ng-disabled="dessert.calories.value > 400" ng-repeat="dessert in desserts.data | filter: filter.search | orderBy: query.order | limitTo: query.limit : (query.page -1) * query.limit">
<td md-cell>{{dessert.name}}</td>
<!-- <td md-cell>
<md-select ng-model="dessert.type" placeholder="Other">
<md-option ng-value="type" ng-repeat="type in getTypes()">{{type}}</md-option>
</md-select>
</td> -->
<td md-cell>{{dessert.dateTime}}</td>
<td md-cell>{{dessert.calories.value}}</td>
<td md-cell>{{dessert.fat.value | number: 2}}</td>
<!-- <td md-cell>{{dessert.carbs.value}}</td>
<td md-cell>{{dessert.protein.value | number: 2}}</td> -->
<td md-cell hide-gt-xs show-gt-md>{{dessert.sodium.value}}</td>
<td md-cell hide-gt-xs show-gt-lg>{{dessert.calcium.value}}%</td>
<td md-cell hide-gt-xs show-gt-lg>{{dessert.iron.value}}%</td>
<td md-cell ng-click="editComment($event, dessert)" ng-class="{'md-placeholder': !dessert.comment}">
{{dessert.comment || 'Add a comment'}}
</td>
</tr>
</tbody>
</table>
</md-table-container>
【问题讨论】:
-
我认为您需要对时间戳值进行排序,而不是对字符串日期值进行排序。也许添加一个新字段 datetimeValue
-
@Zooly,它适用于时间戳。这种格式的解决方法是什么?
-
您可以存储一个日期值(
new Date('xxx').valueOf()),而不是注册日期字符串
标签: javascript angularjs angular-material