繁体   English   中英

Ag-Grid清除固定列功能不起作用

[英]Ag-Grid Clear Pinned columns function is not working

我正在使用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);
        }

我的代码:

我使用的不是列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函数时,什么都没有发生。

这是因为setColumnPinned方法引用了所提供的colId 在这种情况下,您没有ID为Sample name的列,但有headerName值为Sample name的值。

为了解决这个问题,你只需要添加colId声明您columnDefs对象。

最后,解决方案是使用:var colState = gridOptions.columnApi.getColumnState(); 在此处找到:ag-grid.com/javascript-grid-column-definitions

暂无
暂无

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

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