el-table列字段遍历添加插槽

问题:每次使用el-table列字段直接v-for直接遍历使用,但是有时候需要给指定列添加特殊内容显示。例如我遇到的问题是给指定列添加输入框。

原代码:

            <el-table-column
                v-for="(item, index) in columns"
                :key="index"
                :prop="item.prop"
                :label="item.label"
                :align="item.align"
                :min-width="item.minWidth"
                />

思路:给指定的列中添加插槽,通过插槽自定义列的内容。

<el-table-column
                v-for="(item, index) in columns"
                :key="index"
                :prop="item.prop"
                :label="item.label"
                :align="item.align"
                :min-width="item.minWidth"
                >
                <template slot-scope="{row}" v-if="item.solt">
                    <el-input type="text" v-model="row.delivery" clearable placeholder="本次数量"/>
                </template>
            </el-table-column>

这样操作会导致回显的数据全部为空,说明只要是在<el-table-column></el-table-column>标签内添加内容prop属性就会失效,直接页面回显标签内的内容。所以我们这里就需要做一个逻辑判断,当满足条件的时候显示自定义内容,当不满足条件的时候直接显示数据。

更改后的代码:

<el-table-column
                v-for="(item, index) in columns"
                :key="index"
                :prop="item.prop"
                :label="item.label"
                :align="item.align"
                :min-width="item.minWidth"
                >
                <template slot-scope="{row}">
                    <el-input type="text" v-model="row.delivery" clearable placeholder="本次数量" v-if="item.slot === 'delivery'"/>
                    
                    <span v-else>{
   
   {row[item.prop]}}</span>
                </template>
            </el-table-column>

猜你喜欢

转载自blog.csdn.net/ZWP_990527/article/details/127958529