ng-grid行模板中的日期格式

时间:2022-08-24 11:34:54

I've created a ng-grid with the following column definitions:

我创建了一个包含以下列定义的ng-grid:

columns: [
  { field: "CompanyPkid", visible: false },
  { field: "CompanyName", visible: false },
  { field: "StartDate", visible: false, cellFilter: "date:'yyyy-MM-dd'" },
  { field: "CompanyId", 
    displayName: "Company ID",
    cellTemplate: "<div class=\"ngCellText\" ng-class=\"col.colIndex()\">"+
                    "<a href=\"Company/Edit/{{row.getProperty('CompanyPkid')}}\">"+
                      "{{row.getProperty(col.field)}}"+
                    "</a><br />"+
                    "{{row.getProperty('CompanyName')}}<br />"+
                    "{{row.getProperty('StartDate')}}"+
                  "</div>",
  }],

One of the columns is an hidden date column.

其中一列是隐藏日期列。

One of the columns uses a template and includes the value from the hidden date column.

其中一列使用模板并包含隐藏日期列中的值。

I would like to format the date in the multiline column to yyyy-MM-dd. Is this possible? If so, how?

我想将多行列中的日期格式化为yyyy-MM-dd。这可能吗?如果是这样,怎么样?

2 个解决方案

#1


10  

Use the date filter, like this:

使用日期过滤器,如下所示:

{{row.entity.StartDate | date:'yyyy-MM-dd'}}

#2


1  

I ran into a problem with the approach above. If you're using filtering or sorting on the column, they still operate on the timestamp or Date object from the field value, not the results of using the filter inside the cellTemplate. So, users could enter "03" in the column filter but see a date like "12/31/2005" - the 03 was in the timestamp, which isn't shown.

我遇到了上述方法的问题。如果您对列使用过滤或排序,它们仍然对字段值中的时间戳或Date对象进行操作,而不是在cellTemplate中使用过滤器的结果。因此,用户可以在列过滤器中输入“03”,但会看到类似“12/31/2005”的日期 - 03位于时间戳中,未显示。

The solution I found is to use cellFilter: 'date:"MM/dd/yyyy"' in the columnDef object. This preserves both sorting and filtering while allowing you to display the date how you want. Credit to Victor: http://www.victorshi.com/blog/post/How-to-format-a-datetime-column-in-ng-grid

我找到的解决方案是在columnDef对象中使用cellFilter:'date:“MM / dd / yyyy”。这样可以保留排序和过滤,同时允许您显示日期。感谢Victor:http://www.victorshi.com/blog/post/How-to-format-a-datetime-column-in-ng-grid

#1


10  

Use the date filter, like this:

使用日期过滤器,如下所示:

{{row.entity.StartDate | date:'yyyy-MM-dd'}}

#2


1  

I ran into a problem with the approach above. If you're using filtering or sorting on the column, they still operate on the timestamp or Date object from the field value, not the results of using the filter inside the cellTemplate. So, users could enter "03" in the column filter but see a date like "12/31/2005" - the 03 was in the timestamp, which isn't shown.

我遇到了上述方法的问题。如果您对列使用过滤或排序,它们仍然对字段值中的时间戳或Date对象进行操作,而不是在cellTemplate中使用过滤器的结果。因此,用户可以在列过滤器中输入“03”,但会看到类似“12/31/2005”的日期 - 03位于时间戳中,未显示。

The solution I found is to use cellFilter: 'date:"MM/dd/yyyy"' in the columnDef object. This preserves both sorting and filtering while allowing you to display the date how you want. Credit to Victor: http://www.victorshi.com/blog/post/How-to-format-a-datetime-column-in-ng-grid

我找到的解决方案是在columnDef对象中使用cellFilter:'date:“MM / dd / yyyy”。这样可以保留排序和过滤,同时允许您显示日期。感谢Victor:http://www.victorshi.com/blog/post/How-to-format-a-datetime-column-in-ng-grid