传递给this.state我的行和他们的json架构

时间:2017-01-22 21:33:33

标签: json reactjs schema reactabular

我遇到了一个问题,我需要将我的架构和我的json传递给我的构造函数。首先,我正在使用Reactabular来开发SPA,但是在这个库中,我只能使用他们创建的名为generateRows的函数启动应用程序,但是我有自己的对象要注入,所以我不想生成行。

在我的组件中,我有这段代码:

class AllFeaturesTable extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      rows: generateRows(5, schema), 
      searchColumn: 'all',
      query: {}, // search query
      sortingColumns: null, 
      columns: this.getColumns(), 
      pagination: { 
        page: 1,
        perPage: 5
      }
    };

当我将行作为json对象数组传递时,一切都很有效,直到我尝试编辑这些值,并且有理由我无法编辑这些数据。我不能,因为我没有通过模式(你们可以在他们作为模式参数的子系统中看到)。

我的问题是如何实现这一目标?传递我的this.state.row架构和我的行。

这是我的行:

const predefinedRows = [
   { "combustivel" : "Flex",
  "imagem" : null,
  "marca" : "Volkswagem",
  "modelo" : "Gol",
  "placa" : "FFF-5498",
  "valor" : 20000
  },
  { "combustivel" : "Gasolina",
  "imagem" : null,
  "marca" : "Volkswagem",
  "modelo" : "Fox",
  "placa" : "FOX-4125",
  "valor" : "20000"
  },
  { "combustivel" : "Alcool",
  "imagem" : "http://carros.ig.com.br/fotos/2010/290_193/Fusca2_290_193.jpg",
  "marca" : "Volkswagen",
  "modelo" : "Fusca",
  "placa" : "PAI-4121",
  "valor" : "20000"
  }

];

这是我的架构:

const schema = {
  type: 'object',
  properties: {
    combustivel: {
      type: 'string'
    },
    imagem: {
      type: 'string'
    },
    marca: {
      type: 'string'
    },
    modelo: {
      type: 'string'
    },
    placa: {
      type: 'string'
    },
    valor: {
      type: 'integer'
    }
  },
  required: ['combustivel', 'imagem', 'marca', 'modelo', 'placa']
};

提前致谢!

1 个答案:

答案 0 :(得分:1)

您不应该使用generateRowsgenerateRows引入了here,只返回虚拟数组数据。

只需按照example即可实施您的组件。 如果要使用所有功能,请传递给this.state.row架构和行。就这样做:

this.state = {
      row: {
        rows: yourRows,
        schema: yourSchema
      }, 
      searchColumn: 'all',
      query: {}, // search query
      sortingColumns: null, 
      columns: this.getColumns(), 
      pagination: { 
        page: 1,
        perPage: 5
      }
    };