简体   繁体   English

将新行添加到网格顶部的 ag-grid 反应

[英]Adding new row to ag-grid at the top of the grid react

I'm having trouble figuring out how i should properly go about adding a empty row to my ag-grid at the top of the table.我无法弄清楚我应该如何正确地向表格顶部的 ag-grid 添加一个空行。

Right now this is how my component / user lifecycle is:现在我的组件/用户生命周期是这样的:

  1. Load page加载页面
  2. Render Grid (from componentDidMount, asking for data from reducer) Render Grid(来自componentDidMount,从reducer请求数据)
  3. fetch data from db (list of roles)从数据库中获取数据(角色列表)
  4. update my reducer to have list of roles (normalized) -> send to component更新我的减速器以获得角色列表(标准化)-> 发送到组件
  5. My RolesComponent now denormalizes that data it received我的 RolesComponent 现在非规范化它收到的数据
  6. Display on grid在网格上显示
  7. Click on [Add Button] -> dispatches action to create role {name: '', description: ''}单击 [添加按钮] -> 调度操作以创建角色 {name: '', description: ''}
  8. Sends to api, api returns new role发送到 api,api 返回新角色
  9. Reducers adds new role to list of roles Reducers 向角色列表添加新角色
  10. Refreshes grid component since new props are loaded, new empty role is added to bottom of the table由于加载了新道具,因此刷新网格组件,新的空角色被添加到表格底部

Because the role I add is new, its 'id' is the highest, hence I believe it goes to the end of the table.因为我添加的角色是新的,它的 'id' 是最高的,因此我相信它会出现在表格的末尾。 However i could skip any reducer logic to add (step 9) and just manually do it like this:但是,我可以跳过任何 reducer 逻辑来添加(第 9 步),只需手动执行如下操作:

this.props.api.addDispatcher({name: '', description: ''}).then((result) => {
  this.props.api.updateRowData({ add: [result] });
});

Where the reducer does not have a case for 'ADD_ROLE', however this feels dirty.减速器没有“ADD_ROLE”的情况,但这感觉很脏。 I'm pretty stuck and don't believe this is the right path to take.我很困惑,不相信这是正确的道路。 A general or better approach to take is very much appreciated.非常感谢采用通用或更好的方法。

Even ontop of that i'm stuck on whether I should be utilizing redux to rehydrate my table or be depending on ag-grids api.甚至在此之上,我还纠结于是否应该使用 redux 来给我的桌子补充水分,还是依赖于 ag-grids api。 Ideally, I feel like using ag-grids api is more efficient.理想情况下,我觉得使用 ag-grids api 更有效。

You can use the grid property of defaultGroupSortComparator and provide a comparator of id it to make grid sorting.您可以使用defaultGroupSortComparator的 grid 属性并提供一个 id it 的比较器来进行网格排序。

// your comparator
const sortbyId(first, second) => {
    // the logic;
}

render() {
    return( 
        <AgGridReact
         defaultGroupSortComparator={this.sortById}
    />);
}

您可以使用addIndex属性,所以在这种情况下

this.props.api.updateRowData({ add: [result], addIndex: 0 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM