我正在使用ag-grid,并将从金字塔应用程序后端传递来的列和行数据替换为存储在页面上的'grid-options'变量-呈现网格是必需的。网格可以工作,但是我无法使用传递到网格中的数据来实现包含的ag-grid特定功能(例如; clearPinned()等)。用于定义网格的Ag-grids默认方法似乎使用硬编码的列标题。至少在下面的示例中,使用香草JS方法进行了演示:(https://www.ag-grid.com/javascript-grid-pinning/)
网格的列固定功能正在运行。我可以固定所有列,但是要重置它们,我必须刷新页面。我想使用clearPinned()方法重置列。我将列标题存储在名为“ allSampleTableColumns”的变量中。我将其传递给gridOptions变量,而不是硬编码的columnDefs变量。例如,在我的清除固定功能中,我尝试指定“样品名称”标签,并在固定该列后让清除固定重设该列。
ag-grid中带有硬编码列标题的方法:
HTML:
<div style="padding: 4px;">
<button onclick="clearPinned()">Clear Pinned</button>
<button onclick="resetPinned()">Left = #, Athlete, Age; Right = Total
</button>
<button onclick="pinCountry()">Left = Country</button>
</div>
JS:
var columnDefs = [
{headerName: "#", colId: "rowNum", valueGetter: "node.id", width: 40, pinned: 'left'},
{headerName: "Athlete", field: "athlete", width: 150, pinned: 'left'},
{headerName: "Age", field: "age", width: 90, pinned: 'left'},
{headerName: "Country", field: "country", width: 120},
{headerName: "Year", field: "year", width: 90},
{headerName: "Date", field: "date", width: 110},
{headerName: "Sport", field: "sport", width: 110},
{headerName: "Gold", field: "gold", width: 100},
{headerName: "Silver", field: "silver", width: 100},
{headerName: "Bronze", field: "bronze", width: 100},
{headerName: "Total", field: "total", width: 100, pinned: 'right'}
];
var gridOptions = {
defaultColDef: {
resizable: true
},
columnDefs: columnDefs,
debug: true,
rowData: null
};
function clearPinned() {
gridOptions.columnApi.setColumnPinned('rowNum', null);
gridOptions.columnApi.setColumnPinned('athlete', null);
gridOptions.columnApi.setColumnPinned('age', null);
gridOptions.columnApi.setColumnPinned('country', null);
gridOptions.columnApi.setColumnPinned('total', null);
}
我的代码:
我正在使用:of
,而不是列refs()。var allSampleTableColumns = [{label:"sampleId", field:"sampleId", type:"string", pinned: 'left', lockPinned: true, cellClass: 'lock-pinned'}].concat(dataFromPython.sampleTable.columns.map(function(item) { return {label: item, field: item, type:"string"}}));
列标题来自我的金字塔应用程序传递给页面的python变量:dataFromPython.sampleTable.columns
它是包含所需的所有列标题的对象数组:
[{ "label": "sampleId", "field": "sampleId", "type": "string", "pinned": "left", "lockPinned": true, "cellClass": "lock-pinned" }, { "label": "Replicate Group ID", "field": "Replicate Group ID", "type": "string" }, { "label": "Sample name", "field": "Sample name", "type": "string" }, { "label": "Sample name long", "field": "Sample name long", "type": "string" }, { "label": "Sample Type", "field": "Sample Type", "type": "string" }, { "label": "Sample type long", "field": "Sample type long", "type": "string" }, { "label": "Generic sample type", "field": "Generic sample type", "type": "string" }, { "label": "Generic sample type long", "field": "Generic sample type long", "type": "string" }, { "label": "Sample Description", "field": "Sample Description", "type": "string" }, { "label": "Tissue/organism part", "field": "Tissue/organism part", "type": "string" }, { "label": "Parental cell type", "field": "Parental cell type", "type": "string" }, { "label": "Final cell type", "field": "Final cell type", "type": "string" }, { "label": "Cell line", "field": "Cell line", "type": "string" }, { "label": "Reprogramming method", "field": "Reprogramming method", "type": "string" }, { "label": "Developmental stage", "field": "Developmental stage", "type": "string" }, { "label": "Media", "field": "Media", "type": "string" }, { "label": "Disease State", "field": "Disease State", "type": "string" }, { "label": "Labelling", "field": "Labelling", "type": "string" }, { "label": "Genetic modification", "field": "Genetic modification", "type": "string" }, { "label": "FACS profile", "field": "FACS profile", "type": "string" }, { "label": "Age", "field": "Age", "type": "string" }, { "label": "Organism", "field": "Organism", "type": "string" }]
我将其传递到我的ag-grid gridOptions变量中:
var gridOptions = {
defaultColDef: {
resizable: true
},
columnDefs: allSampleTableColumns,
debug: true,
rowData: dataFromPython.allSampleTable,
};
我的clearPinned函数如下所示,我试图指定固定后要重置的列(“样品名称”)。
clearPinned: function(){
gridOptions.columnApi.setColumnPinned(this.allSampleTableColumns['Sample name'], null);
}
期望的是,单击清除固定的按钮会将所有列重置为指定的布局(仅一列,默认情况下应固定sampleId)。调用clearPinned函数没有任何反应。
答案 0 :(得分:0)
这是因为setColumnPinned
方法引用了所提供的colId
。在这种情况下,您没有ID为Sample name
的列,但是您有一个headerName
且值为Sample name
的列。
要解决此问题,您只需向colId
对象添加columnDefs
声明。
答案 1 :(得分:0)
最后,解决此问题的方法是使用:var colState = gridOptions.columnApi.getColumnState();在此处找到:ag-grid.com/javascript-grid-column-definitions