javascript - 用于 reactjs 的 MDBootstrap 表中的模态

标签 javascript reactjs bootstrap-4 bootstrap-material-design mdbreact

我对 reactjs 和 Material 设计 Bootstrap 还很陌生。我正在尝试使用模拟 json 文件将一组数据填充到表中。 我确实设法在这方面取得了一定程度的成功。我想在表格行中加入一个模态。这样,当我单击该行时,会弹出一个模态弹出窗口。

我会贴出我用过的代码。任何帮助将不胜感激。

//TablePage.js

import React, { Component } from "react";
import { MDBDataTable } from "mdbreact";
import testdata from "../../data.js";

class TablePage extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {
        columns: [
          {
            label: "test ID",
            field: "testID",
            sort: "asc",
            width: 50
          },
          {
            label: "test Entity",
            field: "testEntity",
            sort: "asc",
            width: 50
          },
          {
            label: "test Entity Key",
            field: "testEntityKey",
            sort: "asc",
            width: 50
          },
          {
            label: "test Business Date",
            field: "testBusinessDate",
            sort: "asc",
            width: 100
          },
          {
            label: "test created Date",
            field: "testcreatedDate",
            sort: "asc",
            width: 100
          },
          {
            label: "testScore",
            field: "test Score",
            sort: "asc",
            width: 50
          },
          {
            label: "test Score Normalised",
            field: "testScoreNormalised",
            sort: "asc",
            width: 50
          },
          ,
          {
            label: "testUnitIdentifier",
            field: "test Unit Identifier",
            sort: "asc",
            width: 50
          },
          {
            label: "testOwnerIdentifier",
            field: "testOwner Identifier",
            sort: "asc",
            width: 50
          },
          {
            label: "testState",
            field: "test State",
            sort: "asc",
            width: 50
          },
          {
            label: "edit",
            field: "Edit",
            sort: "asc",
            width: 50
          }
        ],
        rows: testdata
      }
    };

  }

  render() {

    return (
      <MDBDataTable btn striped bordered hover data={this.state.data}>
        test
      </MDBDataTable>
    );
  }
}

export default TablePage;

这是我尝试使用的模拟数据文件。

//data.js
const data = [
    {
        testID: 938932,
        testEntity: "employee",
        testEntityKey: 1527003,
        testBusinessDate: "6/14/2017",
        testcreatedDate: "8/7/2017",
        testScore: 115,
        testScoreNormalised: "100",
        testUnitIdentifier: "",
        testownerIdentifier: "938932",
        testState: "inprogress"
    },
    {
        testID: 903576,
        testEntity: "employee",
        testEntityKey: 1593873,
        testBusinessDate: "6/5/2017",
        testcreatedDate: "1/17/2018",
        testScore: 175,
        testScoreNormalised: "100",
        testUnitIdentifier: "",
        testownerIdentifier: "903576",
        testState: "onhold"
    },
    {
        testID: 947830,
        testEntity: "employee",
        testEntityKey: 1735438,
        testBusinessDate: "8/16/2017",
        testcreatedDate: "10/14/2017",
        testScore: 139,
        testScoreNormalised: "100",
        businessUnitIdentifier: "",
        testownerIdentifier: "947830",
        testState: "inprogress"
    },
    {
        testID: 952479,
        testEntity: "employee",
        testEntityKey: 1305158,
        testBusinessDate: "1/14/2018",
        testcreatedDate: "2/3/2018",
        testScore: 160,
        testScoreNormalised: "100",
        testUnitIdentifier: "",
        testownerIdentifier: "952479",
        testState: "ready"
    },
    {
        testID: 927366,
        testEntity: "employee",
        testEntityKey: 1645384,
        testBusinessDate: "8/20/2017",
        testcreatedDate: "3/18/2018",
        testScore: 123,
        testScoreNormalised: "100",
        testUnitIdentifier: "",
        testownerIdentifier: "927366",
        testState: "onhold"
    }]

我也发个图

Table snapshot

最佳答案

如果我没理解错的话,您想知道如何添加按钮以在编辑单元格中打开模式窗口。这可以通过将组件添加到 data.js 中的编辑字段来轻松完成:

import React from 'react';
import ModalPage from './ModalPage';

export default [
  {
    testID: 938932,
    testEntity: "employee",
    testEntityKey: 1527003,
    testBusinessDate: "6/14/2017",
    testcreatedDate: "8/7/2017",
    testScore: 115,
    testScoreNormalised: "100",
    testUnitIdentifier: "",
    testownerIdentifier: "938932",
    testState: "inprogress",
    edit: <ModalPage />
  },
  {
    testID: 927366,
    testEntity: "employee",
    testEntityKey: 1645384,
    testBusinessDate: "8/20/2017",
    testcreatedDate: "3/18/2018",
    testScore: 123,
    testScoreNormalised: "100",
    testUnitIdentifier: "",
    testownerIdentifier: "927366",
    testState: "onhold",
    edit: <ModalPage />
  }
];

我在这里使用的 ModalPage 组件是来自 mdb docs 的模态页面示例组件。 .你可以引用我在Stackblitz上做的例子.

有关如何自定义数据表以包含不同控件的更多信息,您可以找到 here .

关于javascript - 用于 reactjs 的 MDBootstrap 表中的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557907/

相关文章:

javascript - componentRecieveProps 获得与渲染中不同的 props 值

reactjs - 使用动态导入测试 React 组件 (Enzyme/Mocha)

html - Bootstrap 不包装没有大小的列

twitter-bootstrap - Bootstrap网格系统半屏

javascript - 开启jquery?

javascript - Joomla 模块 jQuery 冲突

javascript - 将 Angular 8 开发工具更新到 15 开发工具后,调试器无法在浏览器开发工具上运行

javascript - React 组件返回映射项列表,然后消失

html - 我的导航栏上的 "hamburger"按钮不起作用( Bootstrap )

如何定义一个JavaScript函数?