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

formatter elementui

2026-01-15 18:37:52前端教程

Formatter in Element UI

Element UI provides a formatter function in various components like Table, Select, and DatePicker to customize the display format of data. The formatter allows transforming raw data into a more readable or desired format before rendering.

Using Formatter in Table Column

In the Table component, the formatter property can be used within a column definition to format cell content. It takes a function with parameters (row, column, cellValue, index) and returns the formatted value.

formatter elementui

<el-table :data="tableData">
  <el-table-column
    prop="date"
    label="Date"
    :formatter="formatDate">
  </el-table-column>
</el-table>

methods: {
  formatDate(row, column, cellValue, index) {
    return new Date(cellValue).toLocaleDateString();
  }
}

Using Formatter in Select Options

For the Select component, the formatter can be used to customize the display of options. It is often combined with value-format to handle data transformations.

formatter elementui

<el-select v-model="selected" :formatter="formatOption">
  <el-option
    v-for="item in options"
    :key="item.value"
    :label="item.label"
    :value="item.value">
  </el-option>
</el-select>

methods: {
  formatOption(value) {
    return this.options.find(opt => opt.value === value)?.label || value;
  }
}

Using Formatter in DatePicker

The DatePicker component allows formatting the displayed date using the format property. This is not exactly a formatter function but serves a similar purpose.

<el-date-picker
  v-model="date"
  type="date"
  format="yyyy/MM/dd">
</el-date-picker>

Custom Formatter Logic

For more complex formatting, a custom function can be implemented. For example, formatting numbers with currency symbols or adding conditional styling.

methods: {
  formatCurrency(row, column, cellValue) {
    return `$${cellValue.toFixed(2)}`;
  }
}

Key Considerations

  • The formatter function should be pure and avoid side effects.
  • Ensure the formatted output is compatible with the component's expectations (e.g., strings for display).
  • Performance-intensive formatting should be memoized or optimized if used in large datasets.

By leveraging the formatter functionality, Element UI components can display data in a more user-friendly and context-appropriate manner.

分享给朋友:

相关文章

elementui中文网

elementui中文网

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

elementui升级plus

elementui升级plus

Element UI 升级到 Element Plus 的方法 Element Plus 是 Element UI 的升级版本,专为 Vue 3 设计,提供了更好的性能和更多新特性。以下是升级的具体步…

elementui使用

elementui使用

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

elementui iview

elementui iview

Element UI 和 iView 对比 Element UI 和 iView 都是基于 Vue.js 的 UI 组件库,广泛应用于企业级中后台系统的开发。以下是两者的主要特点和差异: Elem…

elementui面板

elementui面板

ElementUI 面板组件 ElementUI 提供了 el-collapse 和 el-collapse-item 组件来实现面板(折叠面板)功能。以下是如何使用这些组件的详细说明: 安装 E…

elementui sass

elementui sass

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