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

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.

<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.

<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.

formatter elementui

分享给朋友:

相关文章

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 在…

vue elementui

vue elementui

Vue 中使用 Element UI Element UI 是一套基于 Vue 2.0 的桌面端组件库,提供丰富的 UI 组件和交互功能。以下是如何在 Vue 项目中集成和使用 Element UI…

elementui iview

elementui iview

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

elementui下载

elementui下载

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

elementui键盘

elementui键盘

Element UI 键盘事件处理 Element UI 是基于 Vue.js 的组件库,处理键盘事件通常结合 Vue 的原生事件绑定或自定义指令实现。 常用键盘事件绑定方式 在 Elemen…