英文:
make table layout with 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;
问题
- 如何高效地修复问题,使得React表格能够与UI模型匹配?
任何帮助将不胜感激,谢谢。
英文:
Description
I want to make table with the table data like this.
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 :
Here's the code of the table :
import React from "react";
import { Table, Form } from "react-bootstrap";
const tableHeader = [
"No",
"Menu",
"Sub Menu",
"Lihat",
"Tambah",
"Ubah",
"Hapus",
"Export",
];
const submenu = [
["-"],
["Akun Pengguna", "Grup & Kontrol Hak Akses Akun", "Antrian Pasien", "Rawat Jalan"],
["Pemeriksaan Penunjang", "Rekam Medis", "Admisi Rawat Jalan"],
["Status dan Penempatan Kamar", "Transfer Pasien"],
];
let i = 1;
const tableDataMenu = [
[i++, "Dashboard", submenu[0], "", null, null, null, ""],
[i++, "Konfigurasi/Pengaturan", submenu[1], "", "", "", "", null],
[i++, "Layanan Pasien", submenu[2], "", "", "", "", null],
[i++, "Rawat Inap", 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;
Question
- 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) => {
return (
<td key={idx}>
{row[2] &&
row[2].map((subIdx) => (cell !== null) && (
<div key={subIdx} className="mb-3 text-center">
<Form.Check type="switch" />
</div>
))}
{cell}
</td>
);
})}
英文:
In this block
{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>
);
})}
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) => {
return (
<td key={idx}>
{row[2] &&
row[2].map((subIdx) => (cell !== null) && (
<div key={subIdx} className="mb-3 text-center">
<Form.Check type="switch" />
</div>
))}
{cell}
</td>
);
})}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论