RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1450547
Accepted
Maria Avanova
Maria Avanova
Asked:2022-09-21 18:37:17 +0000 UTC2022-09-21 18:37:17 +0000 UTC 2022-09-21 18:37:17 +0000 UTC

在 React JS 中可视化表格组件之间的数据关系

  • 772

我正在可视化两个表的数据:

const json ={
  "tables": [
    {
      "tableName": "One",
      "coordinates": {
        "x": "100",
        "y": "100"
      },
      "columns": [
        {"name": "smthLike",
            "type": "string",
            "typeFormat": "",
            "example": "",
            "descriptor": ""},
        {"name": "@MainColumn",
            "type": "integer",
            "typeFormat": "int64",
            "example": "",
            "descriptor": "codePlatform"}
      ]
    },
    {
      "tableName": "Two",
      "coordinates": {
        "x": "200",
        "y": "200"
      },
      "columns": [
        {"name": "codePlatform",
            "type": "string",
            "typeFormat": "",
            "example": "",
            "descriptor": ""},
        {"name": "namePlatform",
            "type": "string",
            "typeFormat": "",
            "example": "",
            "descriptor": ""}
      ]
    }
  ]
}
введите сюда код

在我的工作中,我使用 react-xarrows 库代码,示例 4:https ://codesandbox.io/embed/github/Eliav2/react-xarrows/tree/master/examples?fontsize=14&hidenavigation=1&theme=dark

表具有名称描述符键。如果描述符与名称匹配(在本例中为 codePlatform),则数据链接箭头应从具有 codePlatform 的字段延伸到描述符为 (@MainColumn) 的字段。

我试图开出这样的条件,但到目前为止它不起作用。要画一个箭头,你需要给它一个起点和一个终点。告诉我,如何在 Column 字段组件中编写这样的条件?

import React from "react";
import styles from "./Column.module.css";
import Xarrow, { useXarrow, Xwrapper } from "react-xarrows";

export const Column = ({ column, index }) => {
  const { name, descriptor } = column;

  const line = {
    from: "",
    to: "",
  };

  return (
    <>
      <div className={styles.ColumnContainer}>
        {name}: {descriptor}
      </div>
    </>
  );
};

表格组件代码:

import Draggable from 'react-draggable';
import React from 'react';
import Xarrow, { useXarrow, Xwrapper } from 'react-xarrows';
import { Column } from '../Column/Column';

export const DraggableBox = ({table, key, reference = undefined, id = undefined, ...style }) => {
  const {tableName, coordinates} = table;
  
  const updateXarrow = useXarrow();
  
  //if (initialOffset) moreStyle = { position: 'absolute', left: initialOffset.x, top: initialOffset.y };
  return (
    <Draggable onDrag={updateXarrow} onStop={updateXarrow}>
      <div ref={reference} id={id} style={{position: 'absolute', left: coordinates.x + 'px', top: coordinates.y + 'px', border: '2px solid'}}>
      <p>Название: {tableName}</p>
      <div>
        {table.columns.map((column, index) => (
          <Column table={table} column={column} key={index} />
        ))}
      </div>
      </div>
      
    </Draggable>
  );
};
введите сюда код

和一个我迭代整个数据数组的组件:

import { Table } from "../Table/Table";
import styles from "./Tables.module.css";
import tableData from "../utils/all_db_schema.json";

export const Tables = () => {
  const [tableState, setTableState] = useState(tableData);

  return (
    <>
      {tableState.map((elem, index) => (
        <div
          className={styles.AreaContainer}
          key={index}
          style={{ color: elem.color }}
        >
          <p>Имя: {elem.groupName}</p>
          <div className={styles.ClassesContainer}>
            {elem.tables.map((table, index) => (
              DraggableBox table={table} key={index} />
            ))}
          </div>
        </div>
      ))}
    </>
  );
};
reactjs
  • 0 0 个回答
  • 0 Views

0 个回答

  • Voted
  1. Best Answer
    Daniil Loban
    2022-09-22T02:08:55Z2022-09-22T02:08:55Z

    react-xarrows 文档说字符串 可以充当and ,所以我更喜欢使用它们(必须通过整个应用程序),它们是唯一的 id 就足够了(在整个应用程序中!),我输入了它们在列数据中,例如:startenduseRefselfId

    {
      name: "smthLike",
      type: "string",
      typeFormat: "",
      example: "",
      descriptor: "",
      selfId: "box1" <--- добавляем Id (уникальное среди всех)
    },
    

    创建列时,它被id分配给一个元素:

    export const Column = ({ column, index }) => {
      const { name, descriptor, selfId } = column;
      return (
        <div id={selfId} className={styles.ColumnContainer}> // Присваиваем id
          {name}: {descriptor}
        </div>
      );
    };
    

    selfId链接本身的存储可以以完全不同的方式组织,所以我没有做这部分,它可以是一个单独的数组,这将简化渲染的解析,或者这个信息可以与它一起存储在一个列

    在表格中,我们显示所有可用的箭头

    <Xarrow start="box1" end="box3" />
    

    拖动onMouseMove={() => setUpd(!upd)}更新可以通过任何其他方式进行更新,因为此处使用的方法可能会影响性能。

    我见过这样的更新方式,或许它有它的优点,但是这一切都是为扁平结构设计的(当没有组件嵌套时),即 目前的情况不同。

    const [, reRender] = useReducer(() => ({}), {});
    const renderCount = useRef(0);
    useEffect(() => {
       renderCount.current += 1;
       if (renderCount.current === 2) {
         reRender();
         renderCount.current = 0;
       }
    });
    

    不幸的是updateXarrow,由于DraggableBox某种原因,它不会导致更新。

    export const Tables = () => {
      const [upd, setUpd] = useState(false);
    
      return (
        .........        
        <div
           onMouseMove={() => setUpd(!upd)} // Обновляем при движении мыши
        >
         .........
          <div className={styles.ClassesContainer}>
            {elem.tables.map((table, index) => (
              <DraggableBox table={table} key={index} />
            ))}
            <Xarrow start="box1" end="box3" /> // Cтрелки
          </div>
         .........
     );
    

    在此处输入图像描述

    链接到具有上述更改的沙箱(组件文件、导入已稍作修改!)

    • 0

相关问题

  • 如果图像在道具中,如何使背景图像做出反应?

  • 项目未显示

  • 引发错误:InvalidTokenError: Invalid token specified: Cannot read property 'replace' of undefined

  • 如何在没有 node.js 的情况下运行 react.js 应用程序

  • 如何从 React Native 中的导航堆栈中清除上一个屏幕?

  • 为什么渲染后会触发 Click 事件?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5