【问题标题】:React Data Grid external data updateReact Data Grid 外部数据更新
【发布时间】:2019-05-10 10:50:44
【问题描述】:

所以我几乎可以肯定做错了什么,并且不了解这里的底层库,但是我如何做到这一点,以便我可以将行项目从外部更新到 React-Data-Grid?

下面的例子,5s后第一行的第一个col被更新,但表中的数据没有。为什么?以及如何改变?

let cols = [
  {
    key: 'sapAssetNumber',
    name: 'SAP Asset number',
    editable: false
  },
  {
    key: 'assetDescription',
    name: 'Description',
    editable: false
  },
  {
    key: 'assetNBV',
    name: 'Asset NBV',
    editable: false
  },
  {
    key: 'salePrice',
    name: 'Sale price',
    editable: false
  }
];

let rows = [
    {
        "id" : "0",
        "sapAssetNumber" : "woof",
        "isAlreadyDisposed" : "",
        "assetDescription" : "test",
        "assetNBV" : "100",
        "salePrice" : "100"
    },
    {
        "id" : "0",
        "sapAssetNumber" : "",
        "isAlreadyDisposed" : "",
        "assetDescription" : "test",
        "assetNBV" : "100",
        "salePrice" : "100"
    },
    {
        "id" : "this",
        "sapAssetNumber" : "is",
        "isAlreadyDisposed" : "a",
        "assetDescription" : "test",
        "assetNBV" : "",
        "salePrice" : ""
    },
    {
        "id" : "jfkhkdafhn",
        "sapAssetNumber" : "",
        "isAlreadyDisposed" : "",
        "assetDescription" : "",
        "assetNBV" : "",
        "salePrice" : ""
    },
    {
        "id" : "kjdsaflkadjsf",
        "sapAssetNumber" : "",
        "isAlreadyDisposed" : "",
        "assetDescription" : "",
        "assetNBV" : "",
        "salePrice" : ""
    },
    {
        "id" : "",
        "sapAssetNumber" : "",
        "isAlreadyDisposed" : "",
        "assetDescription" : "",
        "assetNBV" : "500",
        "salePrice" : "500"
    }
];

class Example extends React.Component {
  constructor() {
  	super();
		
    setTimeout(() => {
    	console.log('changed');
      rows[0].sapAssetNumber = 'meow'
    }, 5000);
  }
  
	render() {
  	return <ReactDataGrid
        columns={cols}
        rowGetter={(i) => rows[i]}
        rowsCount={10}
      />;
  }
}

ReactDOM.render(<Example />, document.querySelector("#app"))
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

.done {
  color: rgba(0, 0, 0, 0.3);
  text-decoration: line-through;
}

input {
  margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-data-grid/6.1.0/react-data-grid.min.js"></script>

<div id="app"></div>

【问题讨论】:

    标签: reactjs react-data-grid


    【解决方案1】:

    rows 变量是文件中的全局变量。 5 秒后行变量发生了变化,但是,因为行变量既不是由示例组件状态维护,也不是示例组件的道具,示例组件生命周期方法不会跟踪更改。因此,当 rows 变量发生变化时,不会调用 Example 组件的 render 方法,因此 ReactDataGrid 没有获取更新的值。

    尝试在 Example 组件的状态下维护 rows 变量。像这样-

    let cols = [
        {
          key: 'sapAssetNumber',
          name: 'SAP Asset number',
          editable: false
        },
        {
          key: 'assetDescription',
          name: 'Description',
          editable: false
        },
        {
          key: 'assetNBV',
          name: 'Asset NBV',
          editable: false
        },
        {
          key: 'salePrice',
          name: 'Sale price',
          editable: false
        }
      ];
    
      let rows = [
          {
              "id" : "0",
              "sapAssetNumber" : "woof",
              "isAlreadyDisposed" : "",
              "assetDescription" : "test",
              "assetNBV" : "100",
              "salePrice" : "100"
          },
          {
              "id" : "0",
              "sapAssetNumber" : "",
              "isAlreadyDisposed" : "",
              "assetDescription" : "test",
              "assetNBV" : "100",
              "salePrice" : "100"
          },
          {
              "id" : "this",
              "sapAssetNumber" : "is",
              "isAlreadyDisposed" : "a",
              "assetDescription" : "test",
              "assetNBV" : "",
              "salePrice" : ""
          },
          {
              "id" : "jfkhkdafhn",
              "sapAssetNumber" : "",
              "isAlreadyDisposed" : "",
              "assetDescription" : "",
              "assetNBV" : "",
              "salePrice" : ""
          },
          {
              "id" : "kjdsaflkadjsf",
              "sapAssetNumber" : "",
              "isAlreadyDisposed" : "",
              "assetDescription" : "",
              "assetNBV" : "",
              "salePrice" : ""
          },
          {
              "id" : "",
              "sapAssetNumber" : "",
              "isAlreadyDisposed" : "",
              "assetDescription" : "",
              "assetNBV" : "500",
              "salePrice" : "500"
          }
      ];
    
      class Example extends React.Component {
        constructor() {
            super();
    
            this.state={rows:rows}
    
    
        }
    
        componentDidMount = () =>{
            const context =  this;
            setTimeout(() => {
                rows[0].sapAssetNumber = 'meow'
                context.setState({rows});
          }, 5000);
        }
    
          render() {
            return <ReactDataGrid
              columns={cols}
              rowGetter={(i) => rows[i]}
              rowsCount={10}
            />;
        }
      }
    
      ReactDOM.render(<Example />, document.querySelector("#app"));
    

    请阅读此内容以了解组件状态:-https://reactjs.org/docs/faq-state.html

    请阅读本文以了解何时调用渲染方法:-https://lucybain.com/blog/2017/react-js-when-to-rerender/

    希望它能解决问题!

    【讨论】:

    • 我很欣赏这些信息,我知道这一点,在我的现实生活示例中,行是道具,但行为相同。有许多技巧可以让它工作,可惜你的建议并没有真正解决问题
    • prop 改变后是否调用了 render 方法?
    【解决方案2】:

    在通过 GitHub 咨询react-data-grid 中的问题后,我使用了那里提供的解决方案。

    然后只需致电this.refresh()。它不漂亮,但似乎是 RDG 中的一个漏洞

    getRowCount() {
        let count = this.props.rows.length;
        if(this.state.refresh && count > 0) {
          count--; // hack for update data-grid
          this.setState({
            refresh: false
          });
        }
    
        return count;
      }
    
    refresh() {
        this.setState({
          refresh: true
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多