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

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.

分享给朋友:

相关文章

idea elementui

idea elementui

Element UI 简介 Element UI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如表格、表单、弹窗等),支持响应式布局和主题定制,适…

elementui message

elementui message

Element UI Message 组件使用指南 Element UI 的 Message 组件用于显示全局提示信息,常用于操作反馈、通知提醒等场景。以下为详细使用方法: 基础用法 引入 Mess…

elementui汉语

elementui汉语

Element UI 中文文档与资源 Element UI 是一套基于 Vue.js 的桌面端组件库,提供丰富的 UI 组件和友好的中文支持。以下是相关中文资源和使用方法: 官方中文文档 Ele…

elementui 2.0

elementui 2.0

Element UI 2.0 是 Vue.js 的一个流行组件库版本,发布于 2018 年,提供了丰富的 UI 组件和工具,适用于快速构建企业级中后台产品。以下是关于 Element UI 2.0 的…

elementui type=

elementui type=

elementui type 属性 在 Element UI 中,type 属性用于定义组件的样式或行为,具体作用因组件而异。以下是常见组件中 type 属性的用法: Button 按钮 type…

elementui打钩

elementui打钩

使用 ElementUI 实现打钩功能 在 ElementUI 中,实现打钩功能通常涉及复选框(Checkbox)或表格中的多选功能。以下是几种常见的实现方式: 复选框(Checkbox)实现打钩…