关键词搜索

源码搜索 ×
×

Web前端文档阅读笔记-vis.js动态添加节点(vue cli环境)

发布2020-05-21浏览2570次

详情内容

这里主要是针对vis的network图进行节点动态添加

 

图用的是vis.js,表单使用的是element-ui

程序运行截图如下:

添加一个节点:

这里是不需要刷新页面就能添加的。
 

程序结构如下:

关键源码如下:

FormGroup.vue

  1. <template>
  2. <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  3. <el-form-item label="id" prop="pass">
  4. <el-input v-model="ruleForm.pass" autocomplete="off"></el-input>
  5. </el-form-item>
  6. <el-form-item label="结点名" prop="checkPass">
  7. <el-input v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  8. </el-form-item>
  9. <el-form-item label="连接到节点ID" prop="age">
  10. <el-input v-model.number="ruleForm.age"></el-input>
  11. </el-form-item>
  12. <el-form-item>
  13. <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
  14. <el-button @click="resetForm('ruleForm')">重置</el-button>
  15. </el-form-item>
  16. </el-form>
  17. </template>
  18. <script>
  19. import {addNode} from 'JS/visTest.js'
  20. export default {
  21. data() {
  22. let checkAge = (rule, value, callback) => {
  23. };
  24. let validatePass = (rule, value, callback) => {
  25. };
  26. let validatePass2 = (rule, value, callback) => {
  27. };
  28. return {
  29. ruleForm: {
  30. pass: '',
  31. checkPass: '',
  32. age: ''
  33. },
  34. rules: {
  35. pass: [
  36. { validator: validatePass, trigger: 'blur' }
  37. ],
  38. checkPass: [
  39. { validator: validatePass2, trigger: 'blur' }
  40. ],
  41. age: [
  42. { validator: checkAge, trigger: 'blur' }
  43. ]
  44. }
  45. };
  46. },
  47. methods: {
  48. submitForm(formName) {
  49. addNode(this.ruleForm['pass'], this.ruleForm['checkPass'], this.ruleForm['age']);
  50. },
  51. resetForm(formName) {
  52. this.$refs[formName].resetFields();
  53. }
  54. }
  55. }
  56. </script>

HelloWorld.vue

  1. <template>
  2. <div>
  3. <div id="networkDemo" style="width:800px; height: 600px">
  4. </div>
  5. <FormGroup></FormGroup>
  6. </div>
  7. </template>
  8. <script>
  9. import {createNode} from 'JS/visTest.js'
  10. import FormGroup from './FormGroup'
  11. export default {
  12. name: 'HelloWorld',
  13. mounted(){
  14. this.init();
  15. },
  16. components:{
  17. FormGroup
  18. },
  19. data () {
  20. return {
  21. msg: 'Welcome to Your Vue.js App'
  22. }
  23. },
  24. methods: {
  25. init(){
  26. createNode('networkDemo')
  27. }
  28. }
  29. }
  30. </script>
  31. <style scoped>
  32. </style>

main.js

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import ElementUI from 'element-ui'
  7. import 'element-ui/lib/theme-chalk/index.css'
  8. Vue.config.productionTip = false
  9. Vue.use(ElementUI)
  10. /* eslint-disable no-new */
  11. new Vue({
  12. el: '#app',
  13. router,
  14. components: {App},
  15. template: '<App/>'
  16. })

visTest.js

  1. import vis from 'vis'
  2. let nodes;
  3. let edges;
  4. export function createNode(idStr) {
  5. nodes = new vis.DataSet([
  6. {id: 1, label: 'Node 1'},
  7. {id: 2, label: 'Node 2'},
  8. {id: 3, label: 'Node 3'},
  9. {id: 4, label: 'Node 4'},
  10. {id: 5, label: 'Node 5'},
  11. ]);
  12. edges = new vis.DataSet([
  13. {from: 1, to: 3, label: 'Hello'},
  14. {from: 1, to: 2},
  15. {from: 2, to: 4},
  16. {from: 2, to: 5},
  17. {from: 3, to: 5}
  18. ])
  19. let container = document.getElementById(idStr);
  20. let data = {
  21. nodes: nodes,
  22. edges: edges
  23. };
  24. let options = {};
  25. let network = new vis.Network(container, data, options);
  26. }
  27. export function addNode(id, label, toId){
  28. let newNode = {id: id, label: label};
  29. let line = {from: id, to: toId};
  30. console.log(newNode)
  31. nodes.add(newNode);
  32. edges.add(line);
  33. console.log("add Node over");
  34. }

 

 

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载