【问题标题】:angular slickgrid date format change only display in grid角度 slickgrid 日期格式更改仅显示在网格中
【发布时间】:2021-01-10 04:33:22
【问题描述】:
  this.columnDefinitions = [
          {
            id: 'edit',
            field: 'id',
            excludeFromColumnPicker: true,
            excludeFromGridMenu: true,
            excludeFromHeaderMenu: true,
            formatter: Formatters.editIcon,
            minWidth: 30,
            maxWidth: 30,
            onCellClick: (e: Event, args: OnEventArgs) => {
              this.router.navigate(['/transaction/transaction-detail/' + args.dataContext.id]);
            }
          },
         
      { id: 'cin', field: 'customer.cin', name: 'CIN', filterable: true, sortable: true, formatter: Formatters.complexObject },
      { id: 'branch', field: 'branch.name', name: 'Branch', filterable: true, sortable: true, formatter: Formatters.complexObject },
      { id: 'valueDate', field: 'valueDate', name: 'Transaction Date ',  dateFormat: 'DD-MM-YYYY'},
      { id: 'amount', field: 'amount', name: 'Amount', sortable: true, maxWidth: 200, type: FieldType.number, filter: { model: Filters.inputNumber }  },  
  ];
<div class="card-body">
    <div class="form-group row" id="grid-container">
        <angular-slickgrid gridId="transactionGrid" [columnDefinitions]="columnDefinitions"
                                [gridOptions]="gridOptions" [dataset]="dataset"
                                (onAngularGridCreated)="angularGridReady($event)">
        </angular-slickgrid>
    </div>
</div>

我需要将日期格式更改为 DD-MM-YYYY 或 DD/MM/YYYY(删除时间)

如何在 angular slickgrid 中做到这一点

现在显示如下...日期以日期格式出现..我们可以使用管道将日期转换为字符串

【问题讨论】:

    标签: angular angular-slickgrid


    【解决方案1】:

    一般来说,你可能对 Angular-Slickgrid 和 SlickGrid 不熟悉,我之所以这么说是因为没有这样的 dateFormat 属性(我很惊讶 TypeScript 没有警告你),但是 formatter 确实存在,所以答案很简单,你只需要使用合适的 Formatter。有一些内置的格式化程序,如果您找不到您想要的确切格式,您也可以选择创建自己的自定义格式化程序。

    我强烈建议您浏览所有 Wiki,例如,Formatters - Wiki 会通过查看所有内置格式化程序的列表为您提供答案,还有一个部分向您展示如何创建自定义如果需要,格式化程序。

    在你的情况下,我认为你可以通过做两件事来实现你想要的格式,首先调用Formatters.dateEuro,它会给你这个格式DD/MM/YYYY,如果你想用@987654328替换/ @ 那么你可以通过使用formatterOptions 网格选项来实现这一点

    this.columnDefinitions: Column[] = [
       // ...
       { id: 'valueDate', field: 'valueDate', name: 'Transaction Date ',  formatter: Formatters.dateEuro },
    ];
    
    this.gridOptions: GridOption = {
        // you can customize the date separator through "formatterOptions"
        formatterOptions: {
          // What separator to use to display a Date, for example using "." it could be "2002.01.01"
          dateSeparator: '-', // can be any of '/' | '-' | '.' | ' ' | ''
        }
    };
    

    我想再次强调所有Wikis,我花了很多时间写它们来回答大多数问题,包括你的问题。另请注意,Angular-Slickgrid 是 jQuery 库之上的包装器,因此 Angular 管道无法解决您的问题。

    【讨论】:

      【解决方案2】:

      以角度操作数据的最佳和最简单的方法是通过管道。

      
      @Pipe({
        name: 'dateAgo',
        pure: true
      })
      export class DateAgoPipe implements PipeTransform {
      
        transform(value: any, args?: any): any {
          if (value) {
            const seconds = Math.floor((+new Date() - +new Date(value)) / 1000);
            if (seconds < 29) // less than 30 seconds ago will show as 'Just now'
                return 'Just now';
            const intervals = {
                'year': 31536000,
                'month': 2592000,
                'week': 604800,
                'day': 86400,
                'hour': 3600,
                'minute': 60,
                'second': 1
            };
            let counter;
            for (const i in intervals) {
                counter = Math.floor(seconds / intervals[i]);
                if (counter > 0)
                    if (counter === 1) {
                        return counter + ' ' + i + ''; // singular (1 day ago)
                    } else {
                        return counter + ' ' + i + 's'; // plural (2 days ago)
                    }
            }
        }
          return value;
        }
      
      }
      

      并在html中使用:

      {{conversation.createdAt | dateAgo}}

      你也可以对 js 和 angular 使用reges

        formatDate(date: string): string {
          return (new Date(date)).toString().replace(/\S+\s(\S+)\s(\d+)\s(\d+)\s.*/, '$2-$1-$3');
        }
      

      【讨论】:

      • 感谢您的回答...我已经有了日期管道..我需要在 slickgrid 中使用管道
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多