当前位置:首页 > 前端教程

formatter elementui

2026-03-05 21:55:42前端教程

Formatter in ElementUI

ElementUI provides a formatter property in certain components like el-table-column to customize the display format of data. The formatter function allows modifying the raw data before rendering.

Basic Usage of Formatter

The formatter property accepts a function with three parameters: row, column, and cellValue. The function should return the formatted value.

<el-table-column
  prop="date"
  label="Date"
  :formatter="formatDate">
</el-table-column>
methods: {
  formatDate(row, column, cellValue) {
    return cellValue.toLocaleDateString();
  }
}

Formatting Numbers

For numeric data, the formatter can be used to add currency symbols, decimal places, or other numeric formatting.

formatter elementui

formatCurrency(row, column, cellValue) {
  return '$' + cellValue.toFixed(2);
}

Conditional Formatting

The formatter function can include conditional logic to display different formats based on the data value.

formatStatus(row, column, cellValue) {
  return cellValue === 1 ? 'Active' : 'Inactive';
}

Date Formatting

When dealing with date fields, libraries like moment.js can be used within the formatter for more sophisticated date formatting.

formatter elementui

formatDateTime(row, column, cellValue) {
  return moment(cellValue).format('YYYY-MM-DD HH:mm:ss');
}

Combining Multiple Fields

The formatter can combine values from multiple fields in the row data to create composite displays.

formatFullName(row, column, cellValue) {
  return `${row.firstName} ${row.lastName}`;
}

Performance Considerations

While formatters are powerful, complex formatting logic may impact performance in large tables. Consider pre-formatting data in the backend when possible.

Limitations

The formatter only affects display and doesn't modify the underlying data. For actual data transformation, use computed properties or methods before passing data to the table.

分享给朋友:

相关文章

elementui中文网

elementui中文网

Element UI 中文网相关信息 Element UI 是一款基于 Vue.js 的开源 UI 组件库,由饿了么前端团队开发和维护。以下是关于 Element UI 中文网的相关信息: 官方网站…

elementui下载

elementui下载

下载Element UI的方法 Element UI可以通过npm或yarn进行安装,也可以直接下载文件引入到项目中。 npm安装方式 npm install element-ui -S yarn…

elementui ico

elementui ico

ElementUI 图标使用指南 ElementUI 提供了一套内置的图标库,可以通过 el-icon- 前缀直接使用。这些图标基于字体图标(iconfont)实现,支持灵活的颜色、大小调整。 内…

elementui sass

elementui sass

在 Element UI 中使用 Sass Element UI 支持通过 Sass 进行样式定制,可以通过修改变量或覆盖样式来实现主题定制或组件样式调整。 安装 Sass 相关依赖: npm…

导入elementui

导入elementui

安装 Element UI 通过 npm 安装 Element UI,适用于 Vue.js 项目。在项目根目录下运行以下命令: npm install element-ui --save 完整引入…

elementui title

elementui title

ElementUI 的 Title 相关组件及用法 ElementUI 提供了多种与标题(Title)相关的组件和配置选项,适用于不同场景的需求。以下是常见的使用方法和组件介绍: 对话框(Di…