如何在 ag-grid 中指定 RowNode.id
How do I specify the RowNode.id in ag-grid
我把这个贴在这里是因为这浪费了我今天的时间。
我试图根据我设置 aggrid 的 rowData
属性 的对象中的数据来设置 RowNode.id
。意思是,我想在我的数据模型上使用 属性 来提供 ag-grid 行模型的内置 id
字段。他们在文档中提到了这一点,但没有解释如何做。
这个问题的答案是你需要在网格的网格选项对象上设置getRowNodeId
属性,像这样:
// where 'd' is an object in your model
this.gridOptions.getRowNodeId = d => {
return d.id; // return the property you want set as the id.
}
他们其实不只是提一下,还有一个很好的例子:
// callback tells the grid to use the 'id' attribute for id's
// id's should always be strings
gridOptions.getRowNodeId = function(data) {
return data.id;
};
// get the row node with ID 55
var rowNode = api.getRowNode('55');
// do something with the row, eg select it
rowNode.setSelected(true);
要ag-grid使用应用分配的 ID,请实施网格回调 getRowNodeId()。回调应该 return 返回特定行数据的 ID。例如,以下代码片段 returns 返回所提供数据项的属性 'id' 的值:
function getRowNodeId(data) {
return data.id;
}
提供 ID 时必须遵守以下规则:
- ID 必须是唯一的
- ID 不得更改
如果您打算用作 ID 的属性不是唯一的或发生了更改,它将导致网格中出现未指定的行为。换句话说,不要使用不唯一或可以更改的字段。
如果使用行分组,网格将始终为组级别分配 ID(因为没有 one-to-one 映射与 application-supplied 行数据)。回调 getRowNodeId() 仅用于 non-group 级行。
我把这个贴在这里是因为这浪费了我今天的时间。
我试图根据我设置 aggrid 的 rowData
属性 的对象中的数据来设置 RowNode.id
。意思是,我想在我的数据模型上使用 属性 来提供 ag-grid 行模型的内置 id
字段。他们在文档中提到了这一点,但没有解释如何做。
这个问题的答案是你需要在网格的网格选项对象上设置getRowNodeId
属性,像这样:
// where 'd' is an object in your model
this.gridOptions.getRowNodeId = d => {
return d.id; // return the property you want set as the id.
}
他们其实不只是提一下,还有一个很好的例子:
// callback tells the grid to use the 'id' attribute for id's
// id's should always be strings
gridOptions.getRowNodeId = function(data) {
return data.id;
};
// get the row node with ID 55
var rowNode = api.getRowNode('55');
// do something with the row, eg select it
rowNode.setSelected(true);
要ag-grid使用应用分配的 ID,请实施网格回调 getRowNodeId()。回调应该 return 返回特定行数据的 ID。例如,以下代码片段 returns 返回所提供数据项的属性 'id' 的值:
function getRowNodeId(data) {
return data.id;
}
提供 ID 时必须遵守以下规则:
- ID 必须是唯一的
- ID 不得更改
如果您打算用作 ID 的属性不是唯一的或发生了更改,它将导致网格中出现未指定的行为。换句话说,不要使用不唯一或可以更改的字段。
如果使用行分组,网格将始终为组级别分配 ID(因为没有 one-to-one 映射与 application-supplied 行数据)。回调 getRowNodeId() 仅用于 non-group 级行。