Vue3+X6流程圖實(shí)現(xiàn)數(shù)據(jù)雙向綁定詳解
代碼很簡單,看看就可以了,不用多說。
定義組件:實(shí)現(xiàn)html節(jié)點(diǎn)與數(shù)據(jù)初始化、更新
<script setup lang="ts"> import { Graph, Shape } from "@antv/x6"; import { onMounted, reactive, ref, watch } from "vue"; import useFlowModel from "./useFlowModel"; import flowdata from "./flowdata"; const container: any = ref(null); Shape.HTML.register({ shape: "custom-html", effect: ["data"], html(cell: any) { const { bgcolor, label } = cell.getData(); return `<div style="background:${bgcolor};width:100%;height:100%;">${label}</div>`; }, }); const flowModel: any = reactive({ graph: null, datatype: 0, modeldata: flowdata, }); const { graphToModelData, modelDataToGraph, addNode, addNodeData } = useFlowModel(flowModel); onMounted(() => { flowModel.graph = new Graph({ container: container.value, // 設(shè)置畫布背景顏色 background: { color: "#F2F7FA", }, height: 300, width: 600, grid: { visible: true, type: "doubleMesh", args: [ { color: "#eee", // 主網(wǎng)格線顏色 thickness: 1, // 主網(wǎng)格線寬度 }, { color: "#ddd", // 次網(wǎng)格線顏色 thickness: 1, // 次網(wǎng)格線寬度 factor: 4, // 主次網(wǎng)格線間隔 }, ], }, }); flowModel.graph.fromJSON(flowModel.modeldata); // 渲染元素 flowModel.graph.centerContent(); // 居中顯示 flowModel.graph.on("cell:changed", (val: any) => { graphToModelData(val); }); watch( () => flowModel.modeldata.cells.map((el: any) => JSON.stringify(el.data)), (newVal, oldVal) => { modelDataToGraph(newVal, oldVal); }, { deep: true } ); }); </script> <template> <template v-if="flowModel.modeldata?.cells && flowModel.modeldata?.cells[0]?.position" > <input type="number" v-model="flowModel.modeldata.cells[0].position.x" /> <input type="text" v-model="flowModel.modeldata.cells[0].data.label" /> <input type="text" v-model="flowModel.modeldata.cells[0].data.bgcolor" /> </template> 【{{ flowModel.modeldata?.cells && flowModel.modeldata?.cells[0].position }}】 <button @click="addNode()">加G</button> <button @click="addNodeData()">加A</button> <button @click="console.log(666.1001, flowModel.modeldata.cells[0].x, flowModel)" > 實(shí)時(shí) </button> <div id="container" ref="container"></div> <div class="as-show-model">{{ flowModel.modeldata }}</div> </template> <style scoped> html, body { margin: 0; padding: 0; } .as-show-model { display: inline-block; white-space: pre-wrap; overflow: auto; height: 300px; width: 300px; } .custom-html { outline: 1px dashed bisque; } #container { display: inline-block; background-color: rgb(248, 255, 240); margin-right: 8px; margin-left: 8px; border-radius: 5px; box-shadow: 0 12px 5px -10px rgb(0 0 0 / 10%), 0 0 4px 0 rgb(0 0 0 / 10%); } </style>
基本函數(shù)處理
export default (flowModel: any) => { function modelDataToGraph(newVal: any, oldVal: any) { // console.log(666.333, oldVal, newVal); if (flowModel.datatype) { flowModel.datatype = 0; } else if ( !oldVal?.length || !newVal?.length || oldVal?.length !== newVal?.length ) { flowModel.graph.fromJSON(flowModel.modeldata); } else { const nowCells: any = flowModel.graph.getCells(); newVal.forEach((el: any, index: number) => { if (!oldVal.includes(el)) { flowModel.datatype = 2; nowCells[index].setProp({ data: JSON.parse(el), }); } }); } } function graphToModelData(val: any) { // const { cell, options } = val; if (flowModel.datatype) { flowModel.datatype = 0; } else { flowModel.datatype = 1; flowModel.modeldata = flowModel.graph.toJSON(); } } function addNodeData() { flowModel.modeldata.cells.push({ position: { x: 30, y: 40, }, size: { width: 100, height: 40, }, data: { bgcolor: "#8f8f8f", label: "我是節(jié)點(diǎn)", color: "#333232", a: { B: 1 }, }, attrs: { text: { text: "我是節(jié)點(diǎn)", }, body: { stroke: "#8f8f8f", strokeWidth: 1, fill: "#fff", rx: 6, ry: 6, }, }, visible: true, shape: "custom-html", id: "node1" + Date.now(), zIndex: 1, }); } function addNode() { flowModel.graph.addNode({ shape: "custom-html", x: 60, y: 100, width: 80, height: 40, data: { bgcolor: "#8f8f8f", label: "我是節(jié)點(diǎn)", }, }); } return { graphToModelData, modelDataToGraph, addNode, addNodeData }; };
演示數(shù)據(jù)
export default { cells: [ { position: { x: 30, y: 40, }, size: { width: 120, height: 40, }, data: { bgcolor: "#8f8f8f", label: "我是節(jié)點(diǎn)", color: "#333232", a: { B: 1 }, }, attrs: {}, visible: true, shape: "custom-html", id: "node1", zIndex: 1, }, { position: { x: 160, y: 180, }, size: { width: 111, height: 40, }, attrs: { text: { text: "世界你好" }, body: { stroke: "#8f8f8f", strokeWidth: 1, fill: "#fff", rx: 6, ry: 6, }, }, visible: true, shape: "rect", id: "node2", zIndex: 1, }, { position: { x: 190, y: 100, }, size: { width: 160, height: 40, }, data: { bgcolor: "#8f8f8f", label: "my test", color: "#333232", a: { B: 1 }, }, attrs: {}, visible: true, shape: "custom-html", id: "node3", zIndex: 1, }, { shape: "edge", attrs: { label: { text: "x6", }, line: { stroke: "#8f8f8f", strokeWidth: 1, }, }, id: "0d9f0031-f018-413c-9631-1cd21d2f8c1f", source: { cell: "node1", }, target: { cell: "node2", }, labels: [ { attrs: { label: { text: "x6", }, }, }, ], zIndex: 1, }, ], };
以上就是Vue3+X6流程圖實(shí)現(xiàn)數(shù)據(jù)雙向綁定詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue3數(shù)據(jù)雙向綁定的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue如何動(dòng)態(tài)綁定img的src屬性(v-bind)
這篇文章主要介紹了vue如何動(dòng)態(tài)綁定img的src屬性(v-bind),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue+element+Java實(shí)現(xiàn)批量刪除功能
這篇文章主要介紹了vue+element+Java實(shí)現(xiàn)批量刪除功能,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04ElementUI表單驗(yàn)證validate和validateField的使用及區(qū)別
Element-UI作為前端框架,最常使用到的就是表單驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于ElementUI表單驗(yàn)證validate和validateField的使用及區(qū)別,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02vue+highcharts實(shí)現(xiàn)3D餅圖效果
這篇文章主要為大家詳細(xì)介紹了vue+highcharts實(shí)現(xiàn)3D餅圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03如何使用el-table實(shí)現(xiàn)純前端導(dǎo)出(適用于el-table任意表格)
我們?nèi)粘W鲰?xiàng)目,特別是后臺(tái)管理系統(tǒng),常常需要導(dǎo)出excel文件,這篇文章主要給大家介紹了關(guān)于如何使用el-table實(shí)現(xiàn)純前端導(dǎo)出的相關(guān)資料,本文適用于el-table任意表格,需要的朋友可以參考下2024-03-03vue中$router.back()和$router.go()的用法
這篇文章主要介紹了vue中$router.back()和$router.go()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue3+koa實(shí)現(xiàn)文件上傳功能的全過程記錄
開發(fā)項(xiàng)目的時(shí)候,用到文件上傳的功能很常見,包括單文件上傳和多文件上傳,下面這篇文章主要給大家介紹了關(guān)于vue3+koa實(shí)現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下2023-01-01