Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using jsx in el-table-column's formatter function #194

Open
youthug opened this issue Apr 23, 2021 · 1 comment
Open

Using jsx in el-table-column's formatter function #194

youthug opened this issue Apr 23, 2021 · 1 comment

Comments

@youthug
Copy link

youthug commented Apr 23, 2021

Hi there.
I defined a variable: columns in column-config.js :

const columns = [
    {
        label: 'First Name',
        prop: 'firstName',
        defaultShow: true,
        minWidth: '180px',
        // TODO return el-tag
        formatter(row, column, cellValue) {
            return (
                <el-tag>{ cellValue }</el-tag>
            )
        }
    },
    {
        label: 'Last Name',
        prop: 'lastName',
        defaultShow: true,
        minWidth: '180px'
    }
]

export {
    columns
}

And use it in App.vue:

<template>
    <div id="app">
        <el-table :data="tableData">
            <!-- bind all attrs -->
            <el-table-column v-for="(col, i) in columns" :key="i" v-bind="col"/>
        </el-table>
    </div>
</template>

<script>
import { columns } from "@/config/column-config";

...

Then I got errors in console: TypeError: h is not a function
image

Here is my code: jsx-demo

Is there any way to return templates in .js files outside render and data function?

Thx.

@youthug youthug changed the title Using jsx in el-table-column Using jsx in el-table-column's formatter function Apr 23, 2021
@HawtinZeng
Copy link

HawtinZeng commented Jul 25, 2023

I have extracted the JSX template into another SFC, it is pretty complicated, but it works! I found something interesting when I used function declaration directly in the script tag of a SFC outside of the render function, I got an error that prompted me "h is not defined", but when I used the function declaration inside an Object, like
const testTemplateGenerator = { renderColFormItem() { return ( <h1>123</h1> ) } } // call in render function: return testTemplateGenerator.renderColFormItem.call(this),
The error is gone! I guess that curly braces may create a scope that provides the "h" function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants