使用React.js创建表格布局

huangapple go评论78阅读模式
英文:

make table layout with react js

问题

描述

我想制作一个表格,表格数据如下所示。

使用React.js创建表格布局

让我们看看开关。在第一行或“仪表板”上有两个表单开关。其余的有四个表单开关。

问题

我尝试找到最佳和有效的解决方案,但结果像这样:
使用React.js创建表格布局

这是表格的代码:

import React from "react";
import { Table, Form } from "react-bootstrap";

const tableHeader = [
  "No",
  "菜单",
  "子菜单",
  "查看",
  "添加",
  "编辑",
  "删除",
  "导出",
];

const submenu = [
  ["-"],
  ["用户账户", "组 & 控制用户访问权限", "患者队列", "门诊"],
  ["辅助检查", "病历", "门诊入院"],
  ["房间状态和安置", "患者转移"],
];

let i = 1;

const tableDataMenu = [
  [i++, "仪表板", submenu[0], "", null, null, null, ""],
  [i++, "配置/设置", submenu[1], "", "", "", "", null],
  [i++, "患者服务", submenu[2], "", "", "", "", null],
  [i++, "住院服务", submenu[3], "", "", "", "", null],
];

const FormCreateRole = () => {
  return (
    <Table>
      <thead style={{ backgroundColor: "#ECEFF1" }}>
        <tr>
          {tableHeader.map((header, index) => (
            <th
              key={index}
              className="txt-blue-grey-700 base-md text-bold text-center"
              style={{ padding: "0.75rem" }}
            >
              {header}
            </th>
          ))}
        </tr>
      </thead>
      <tbody>
        {tableDataMenu.map((row, idx) => (
          <React.Fragment key={idx}>
            <tr>
              {row.slice(0, 2).map((cell, idx) => (
                <td key={idx} className="text-center">{cell}</td>
              ))}
              <td>
                {row[2] && (
                  <div>
                    {row[2].map((sub, idx) => (
                      <div key={idx} className="mb-3">{sub}</div>
                    ))}
                  </div>
                )}
              </td>
              {row.slice(3).map((cell, idx) => {
                  return (
                    <td key={idx}>
                      {row[2] &&
                        row[2].map((subIdx) => (
                          <div key={subIdx} className="mb-3 text-center">
                            <Form.Check type="switch" />
                          </div>
                        ))}
                      {cell}
                    </td>
                  );
              })}
            </tr>
          </React.Fragment>
        ))}
        </tbody>
      </Table>
  );
};

export default FormCreateRole;

问题

  1. 如何高效地修复问题,使得React表格能够与UI模型匹配?

任何帮助将不胜感激,谢谢。

英文:

Description

I want to make table with the table data like this.

使用React.js创建表格布局

Let's look at the switch. In the first row or "Dashboard" have two form switch. and the rest have four form switch.

Problem

I've been tried to find best and effective solution but the result end like this :
使用React.js创建表格布局

Here's the code of the table :

import React from &quot;react&quot;;
import { Table, Form } from &quot;react-bootstrap&quot;;
const tableHeader = [
&quot;No&quot;,
&quot;Menu&quot;,
&quot;Sub Menu&quot;,
&quot;Lihat&quot;,
&quot;Tambah&quot;,
&quot;Ubah&quot;,
&quot;Hapus&quot;,
&quot;Export&quot;,
];
const submenu = [
[&quot;-&quot;],
[&quot;Akun Pengguna&quot;, &quot;Grup &amp; Kontrol Hak Akses Akun&quot;, &quot;Antrian Pasien&quot;, &quot;Rawat Jalan&quot;],
[&quot;Pemeriksaan Penunjang&quot;, &quot;Rekam Medis&quot;, &quot;Admisi Rawat Jalan&quot;],
[&quot;Status dan Penempatan Kamar&quot;, &quot;Transfer Pasien&quot;],
];
let i = 1;
const tableDataMenu = [
[i++, &quot;Dashboard&quot;, submenu[0], &quot;&quot;, null, null, null, &quot;&quot;],
[i++, &quot;Konfigurasi/Pengaturan&quot;, submenu[1], &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, null],
[i++, &quot;Layanan Pasien&quot;, submenu[2], &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, null],
[i++, &quot;Rawat Inap&quot;, submenu[3], &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, null],
];
const FormCreateRole = () =&gt; {
return (
&lt;Table&gt;
&lt;thead style={{ backgroundColor: &quot;#ECEFF1&quot; }}&gt;
&lt;tr&gt;
{tableHeader.map((header, index) =&gt; (
&lt;th
key={index}
className=&quot;txt-blue-grey-700 base-md text-bold text-center&quot;
style={{ padding: &quot;0.75rem&quot; }}
&gt;
{header}
&lt;/th&gt;
))}
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{tableDataMenu.map((row, idx) =&gt; (
&lt;React.Fragment key={idx}&gt;
&lt;tr&gt;
{row.slice(0, 2).map((cell, idx) =&gt; (
&lt;td key={idx} className=&quot;text-center&quot;&gt;{cell}&lt;/td&gt;
))}
&lt;td&gt;
{row[2] &amp;&amp; (
&lt;div&gt;
{row[2].map((sub, idx) =&gt; (
&lt;div key={idx} className=&quot;mb-3&quot;&gt;{sub}&lt;/div&gt;
))}
&lt;/div&gt;
)}
&lt;/td&gt;
{row.slice(3).map((cell, idx) =&gt; {
return (
&lt;td key={idx}&gt;
{row[2] &amp;&amp;
row[2].map((subIdx) =&gt; (
&lt;div key={subIdx} className=&quot;mb-3 text-center&quot;&gt;
&lt;Form.Check type=&quot;switch&quot; /&gt;
&lt;/div&gt;
))}
{cell}
&lt;/td&gt;
);
})}
&lt;/tr&gt;
&lt;/React.Fragment&gt;
))}
&lt;/tbody&gt;
&lt;/Table&gt;
);
};
export default FormCreateRole;

Question

  1. How to fix the problem efficiently. So, the table react can match with the UI mockup ?

Any help will be appreciated, thank you

答案1

得分: 1

在这个代码块中:

{row.slice(3).map((cell, idx) =&gt; {
return (
&lt;td key={idx}&gt;
{row[2] &amp;&amp;
row[2].map((subIdx) =&gt; (cell !== null) &amp;&amp; (
&lt;div key={subIdx} className=&quot;mb-3 text-center&quot;&gt;
&lt;Form.Check type=&quot;switch&quot; /&gt;
&lt;/div&gt;
))}
{cell}
&lt;/td&gt;
);
})}
英文:

In this block

{row.slice(3).map((cell, idx) =&gt; {
return (
&lt;td key={idx}&gt;
{row[2] &amp;&amp;
row[2].map((subIdx) =&gt; (
&lt;div key={subIdx} className=&quot;mb-3 text-center&quot;&gt;
&lt;Form.Check type=&quot;switch&quot; /&gt;
&lt;/div&gt;
))}
{cell}
&lt;/td&gt;
);
})}

You are saying: "For each element in my row array, starting from index 3, create a new table cell containing a toggle switch, IF row2 is truthy". What you want to say is "For each element in my row array, starting from index 3, create a new table cell containing a toggle switch, IF the current element (cell) is truthy". I believe something like the following will work, although I didn't test it so it may have some syntax errors or something of the like.

{row.slice(3).map((cell, idx) =&gt; {
return (
&lt;td key={idx}&gt;
{row[2] &amp;&amp;
row[2].map((subIdx) =&gt; (cell !== null) &amp;&amp; (
&lt;div key={subIdx} className=&quot;mb-3 text-center&quot;&gt;
&lt;Form.Check type=&quot;switch&quot; /&gt;
&lt;/div&gt;
))}
{cell}
&lt;/td&gt;
);
})}

huangapple
  • 本文由 发表于 2023年3月7日 10:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定