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

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 hover

elementui hover

ElementUI Hover 效果实现方法 ElementUI 提供了多种组件支持 hover 交互效果,主要通过 CSS 伪类和组件内置事件实现。 按钮 hover 效果 <el-but…

elementui引用

elementui引用

安装 Element UI 在项目中通过 npm 或 yarn 安装 Element UI: npm install element-ui --save # 或 yarn add element…

elementui模板

elementui模板

ElementUI 模板使用指南 ElementUI 是一个基于 Vue.js 的组件库,提供丰富的 UI 组件,适用于快速开发中后台管理系统。以下是 ElementUI 模板的常见用法和资源推荐。…

elementui增删

elementui增删

ElementUI 表格的增删操作 ElementUI 提供了强大的表格组件,支持数据的增删改查操作。以下是实现增删功能的详细方法: 安装 ElementUI 确保项目中已安装 ElementUI,…

elementui npm

elementui npm

安装 Element UI 的步骤 使用 npm 安装 Element UI 是最常见的方式,适用于基于 Vue.js 的项目。 npm install element-ui -S 安装完成后,可以…

elementui字号

elementui字号

Element UI 默认字号设置 Element UI 的默认字号基于 CSS 变量和预设样式,主要依赖以下规则: 基础字号为 14px,适用于大部分组件(如表格、表单、按钮等)。 标题类…