在使用vue和element-ui做一个页面,在做数据回显的时候出现了一个问题。
有些值需要传入到子组件中。具体办法就是在msgbox选项中添加一个属性。然后在子组件中使用this.$parent.data调用数据就行了
<template>
<div id="side">
<div id="menu">
<el-button type="primary" icon="el-icon-folder-add" @click="addNode"></el-button>
<el-button type="primary" icon="el-icon-edit-outline" @click="editNode"></el-button>
<el-button type="primary" icon="el-icon-folder-delete" @click="deleteNode"></el-button>
</div>
<div id="tree">
<el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
<el-tree
class="filter-tree"
:data="data"
:props="defaultProps"
default-expand-all
show-checkbox
:render-content="renderContent"
:filter-node-method="filterNode"
ref="tree"
></el-tree>
</div>
</div>
</template>
<script>
let req_data = [
{
id: 1,
label: "一级 1",
children: [
{
id: 4,
label: "二级 1-1",
children: [
{
id: 9,
label: "三级 1-1-1"
},
{
id: 10,
label: "三级 1-1-2"
}
]
}
]
},
{
id: 2,
label: "一级 2",
children: [
{
id: 5,
label: "二级 2-1"
},
{
id: 6,
label: "二级 2-2"
}
]
},
{
id: 3,
label: "一级 3",
children: [
{
id: 7,
label: "二级 3-1"
},
{
id: 8,
label: "二级 3-2"
}
]
}
]
import Form from './formComponent/Form'
export default {
name: "Side",
watch: {
filterText(val) {
this.$refs.tree.filter(val);
}
},
data() {
return {
filterText: "",
data: req_data,
defaultProps: {
children: "children",
label: "label"
}
};
},
components: {
"v-form": Form
},
methods: {
handleNodeClick(data) {
console.log(data);
},
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
},
addNode() {
console.log("添加节点!");
this.$alert("<strong>这是 <i>HTML</i> 片段</strong>", "添加节点", {dangerouslyUseHTMLString: true})
},
editNode(data) {
console.log("editNode-data:", data);
this.$msgbox({
title: "编辑节点",
message: <v-form ref="editForm" ></v-form>, // 在message 中可以添加组件进行
showCancelButton: true,
confirmButtonText: '确定',
cancelButtonText: '取消',
data: this.data, // 把数据放入组件中
beforeClose: (action, instance, done) => {
if (action === 'confirm') {
instance.confirmButtonLoading = true;
instance.confirmButtonText = '执行中...';
setTimeout(() => {
done();
setTimeout(() => {
instance.confirmButtonLoading = false;
}, 300);
}, 3000);
} else {
done();
}
}
}).then(action => {
this.$message({
type: 'info',
message: "编辑成功!"
});
}).catch(msg => {
if (msg == "cancel") {
this.$message.info("已取消")
} else {
this.$message.error("编辑失败")
}
});
},
deleteNode(node, data) {
console.log("deleteNode");
console.log("deleteNode-node:", node);
console.log("deleteNode-data:", data);
this.$confirm("此操作将会删除该节点,是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
//当点击删除的时候调用的函数
const parent = node.parent;
const children = parent.data.children || parent.data;
const index = children.findIndex(d => d.id === data.id);
children.splice(index, 1);
this.$message.success("删除成功");
})
.catch((error) => {
// 当点击取消或者出现异常的时候的时候的回调函数,
if (error == "cancel") {
this.$message.info("已取消删除");
} else {
this.$message.error("发生了错误!" + error)
}
});
},
renderContent(h, { node, data, store }) {
/***
* h可以接受3个参数,
* 1.html标签字符串(String)|组件选项对象(Object)|节点解析函数(function)
* 2.定义节点特性的对象
* 3.子节点,h构建的VNode节点或字符串生成的无标签文本节点(Array|String)
*/
return h("span", {
class: ["custom-tree-node"], //绑定类名
style: {
width: "100%",
"text-align": "left"
}
}, [
h("span", {
domProps:{innerText:data.label},
}),
h("span", {
style:{
float: "right",
},
}, [
h("el-button", { // 创建按钮---编辑按钮
attrs: {
size: 'mini',
type:"text"
},
domProps: {innerText:"编辑"},
on: {
click: () => this.editNode(data)
}
}),
h("el-button", { // 创建按钮---删除按钮
attrs: {
size: 'mini',
type:"text",
disabled: data.children ? true : false
},
domProps: {innerText:"删除"},
on: {
click: () => this.deleteNode(node, data)
}
}),
])
])
}
}
};
</script>
<style scoped>
</style>
一个form子组件
<template>
<div>
<el-form ref="form">
<el-form-item label="活动名称">
<el-select v-model="form.item1" placeholder="请选择活动区域">
<el-option label="根节点" value="0"></el-option>
<el-option v-for="(item, index) in list_data" label="{item.label}" value="item.id" :key="'editForm-' + index">{{item.label}}</el-option>
</el-select>
</el-form-item>
</el-form>
<el-button type="primary" @click="printData">打印信息</el-button>
</div>
</template>
<script>
export default {
name: "editForm",
data () {
return {
form: {
item: String
},
list_data: this.$parent.data
}
},
methods: {
printData () {
console.log("parent:", this.$parent.data)
console.log(this.list_data)
}
}
// props: [data]
}
</script>
<style scoped>
</style>