【问题标题】:filter ng-repeat using md-datepicker使用 md-datepicker 过滤 ng-repeat
【发布时间】:2018-02-06 06:32:53
【问题描述】:

我必须使用uploadDate 过滤我的ng-repeat 项目,但它无法过滤。我尝试了一切,但总是失败。我希望有人可以帮助我。

控制器

ExtractionService.getUserUploadedData(d).then(function (msg) {
   angular.forEach(msg.data, function (val) {
          var src = val.uploadDate;
          src = src.replace(/[^0-9 +]/g, '');
          var uploadDate = new Date(parseInt(src));
          console.log(uploadDate); //it returns a ISO date string format
          var dd = {
              uploadDate: uploadDate,
              filename: val.filename,
              bch: val.bch,
              Id: val.edmId
          }
          $scope.datalistings.push(dd);
    })             
});

HTML

<md-datepicker ng-model="search.uploadDate" ng-change="search.uploadDate = (search.uploadDate.toISOString())" md-placeholder="Enter Date" md-open-on-focus></md-datepicker>

在上面的代码中,我尝试将 search.uploadDate 模型转换为 ISO 使用.toISOString() 的字符串日期格式,但无法过滤 导致 ng 重复

<tr ng-repeat="info in filtereddata = (datalistings | filter: {uploadDate:search.uploadDate})" class="animate-repeat">
      <td>{{$index+1}}</td>
      <td>{{info.uploadDate}}</td>
      <td>{{info.filename}}</td>
      <td>{{info.bch}}</td>
</tr>

我也尝试像{{info.uploadDate.toISOString()}} 那样转换info.uploadDate,但它也失败了:(谁能帮助我?

【问题讨论】:

    标签: javascript angularjs angularjs-ng-repeat angular-material


    【解决方案1】:

    创建一个自定义DateFilter,如下所示

    .filter('DateFilter', function() {
        return function(dataArray, uploadDate) {
            if (!dataArray) {
                return;
            } else if (!uploadDate) {
                return dataArray;
            } else {
                return dataArray.filter(function(item) {
                    var uploadDateInput = new Date(item.uploadDate);
                    if (uploadDate !== null) {
                        var term = (uploadDateInput.getDate() === uploadDate.getDate() && uploadDateInput.getMonth() === uploadDate.getMonth() && uploadDateInput.getYear() === uploadDate.getYear());
                        return term;
                    }
                });
            } 
        };
    });
    

    ng-repeat 中使用它作为| DateFilter : yourinputdate

    【讨论】:

    • 先生,我在var term = (uploadDateInput.getDate() === uploadDate.getDate() &amp;&amp; uploadDateInput.getMonth() === uploadDate.getMonth() &amp;&amp; uploadDateInput.getYear() === uploadDate.getYear()); 这一行出现错误,上面写着uploadDate.getDate is not a function
    • 好吧,可能uploadDate不是日期格式,多加一行var uploadDate1 = new Date(uploadDate);和新的var uploadDate1比较,根据你的名字命名。
    • 哦,现在好了,先生。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    相关资源
    最近更新 更多