Are you an LLM? You can read better optimized documentation at /front/component/ma-pro-table/examples/cell-render-plugins.md for this page in Markdown format
Cell Render Plugins β
Demonstrates the usage of various cell render plugins, including built-in and custom plugins.
Features β
- Plugin Mechanism: Extend cell rendering capabilities through a plugin system
- Built-in Plugins: Provides commonly used render plugins (e.g., tags, progress bars, etc.)
- Custom Plugins: Supports registering custom render plugins
- Flexible Configuration: Supports dynamic property configuration and conditional rendering
- Code Reusability: Avoids repetitive rendering logic
Built-in Plugins β
Tag Plugin β
javascript
{
label: 'Status',
prop: 'status',
cellRenderTo: {
name: 'tag',
props: (data) => ({
type: data.row.status === 1 ? 'success' : 'danger'
})
},
formatter: (row) => row.status === 1 ? 'Active' : 'Inactive'
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Custom Plugins β
Registering a Plugin β
javascript
import { useProTableRenderPlugin } from '@mineadmin/pro-table'
import { h } from 'vue'
const { addPlugin } = useProTableRenderPlugin()
// Register progress bar plugin
addPlugin({
name: 'progress',
render: (data, props) => {
return h(ElProgress, {
percentage: data.row[data.column.property] || 0,
color: props?.color || '#409eff',
strokeWidth: props?.strokeWidth || 10,
textInside: props?.textInside !== false,
...props
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Using Custom Plugin β
javascript
{
label: 'Work Progress',
prop: 'progress',
cellRenderTo: {
name: 'progress',
props: (data) => ({
color: data.row.progress >= 90 ? '#67c23a' : '#e6a23c',
strokeWidth: 12,
textInside: true
})
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Common Plugin Examples β
1. Progress Bar Plugin β
javascript
addPlugin({
name: 'progress',
render: (data, props) => {
return h(ElProgress, {
percentage: data.row[data.column.property] || 0,
color: props?.color || '#409eff',
strokeWidth: props?.strokeWidth || 10,
textInside: props?.textInside !== false,
...props
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
2. Rating Plugin β
javascript
addPlugin({
name: 'rate',
render: (data, props) => {
return h(ElRate, {
modelValue: data.row[data.column.property] || 0,
disabled: true,
showScore: props?.showScore !== false,
scoreTemplate: props?.scoreTemplate || '{value} points',
...props
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
3. Image Plugin β
javascript
addPlugin({
name: 'image',
render: (data, props) => {
const src = data.row[data.column.property]
if (!src) return 'No Image'
return h(ElImage, {
src,
style: { width: '60px', height: '40px' },
fit: 'cover',
previewSrcList: [src],
...props
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4. Switch Plugin β
javascript
addPlugin({
name: 'switch',
render: (data, props, proxy) => {
return h(ElSwitch, {
modelValue: !!data.row[data.column.property],
onChange: (value) => {
// Handle switch change
console.log(`${data.row.name} status changed to ${value ? 'on' : 'off'}`)
},
...props
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
5. Link Plugin β
javascript
addPlugin({
name: 'link',
render: (data, props) => {
const text = data.row[data.column.property]
return h(ElLink, {
type: props?.type || 'primary',
href: props?.href || '#',
target: props?.target || '_blank',
onClick: () => {
if (props?.onClick) {
props.onClick(data)
}
},
...props
}, {
default: () => text
})
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
6. Multi-Tag Plugin β
javascript
addPlugin({
name: 'tags',
render: (data, props) => {
const tags = data.row[data.column.property] || []
if (!Array.isArray(tags)) return 'No Tags'
return h('div', tags.map((tag, index) =>
h('el-tag', {
key: index,
size: 'small',
type: props?.type || 'primary',
style: 'margin-right: 4px; margin-bottom: 2px;',
...props
}, {
default: () => tag
})
))
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Plugin Usage Examples β
Avatar Display β
javascript
{
label: 'Avatar',
prop: 'avatar',
width: 100,
cellRenderTo: {
name: 'image',
props: {
style: { width: '50px', height: '50px', borderRadius: '50%' }
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Skill Tags β
javascript
{
label: 'Skills',
prop: 'skills',
width: 200,
cellRenderTo: {
name: 'tags',
props: {
type: 'info'
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Rating Display β
javascript
{
label: 'Rating',
prop: 'rating',
width: 150,
cellRenderTo: {
name: 'rate',
props: {
showScore: true,
scoreTemplate: '{value} points'
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Personal Website Link β
javascript
{
label: 'Website',
prop: 'website',
width: 120,
cellRenderTo: {
name: 'link',
props: (data) => ({
type: 'primary',
href: data.row.website,
target: '_blank',
onClick: (linkData) => {
console.log(`Visiting ${linkData.row.name}'s website`)
}
})
},
formatter: () => 'Visit Website'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Plugin Management β
Getting Plugin Information β
javascript
import { useProTableRenderPlugin } from '@mineadmin/pro-table'
const { getPlugins, getPluginByName, removePlugin } = useProTableRenderPlugin()
// Get all plugins
const allPlugins = getPlugins()
// Get specific plugin
const tagPlugin = getPluginByName('tag')
// Remove plugin
removePlugin('custom-plugin')
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Plugin Parameter Specifications β
javascript
// Plugin definition
interface MaProTableRenderPlugin {
name: string // Plugin name (unique identifier)
render: ( // Render function
data: TableColumnRenderer, // Table data and column info
props: any, // Plugin parameters
proxy: MaProTableExpose // Table instance
) => VNode | string
}
// Using plugins
{
cellRenderTo: {
name: 'plugin-name', // Plugin name
props: any | any[] | (data) => any // Plugin parameters (supports dynamic calculation)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Best Practices β
1. Plugin Naming β
- Use descriptive names like
progress
,image
,tags
- Avoid conflicts with built-in plugin names
- Add prefixes if publishing to app marketplace
2. Performance Optimization β
- Avoid complex computations in render functions
- Use props functions for conditional logic and property calculations
- Properly utilize Vue's reactivity system
3. Error Handling β
javascript
addPlugin({
name: 'safe-plugin',
render: (data, props) => {
try {
// Plugin logic
return h(SomeComponent, props)
} catch (error) {
console.error('Plugin render error:', error)
return 'Render Error'
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
4. Type Safety β
typescript
// TypeScript plugin definition
import type { MaProTableRenderPlugin } from '@mineadmin/pro-table'
const myPlugin: MaProTableRenderPlugin = {
name: 'my-plugin',
render: (data, props) => {
// Type-safe plugin logic
return h('div', data.row[data.column.property])
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
The cell render plugin system provides powerful extensibility, enabling you to easily build rich table display effects.