简体   繁体   English

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

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

I am using ag-grid and have substituted column and row data passed from my pyramid application back-end into the 'grid-options' variable stored on my page - required to render the grid. 我正在使用ag-grid,并将金字塔应用程序后端传递的列和行数据替换为存储在页面上的'grid-options'变量-呈现网格是必需的。 The grid works, but I am not able to implement the included ag-grid specific functions (ex; clearPinned(); etc.) using the data I am passing into the grid. 网格可以工作,但是我无法使用传递到网格中的数据来实现包含的ag-grid特定功能(例如; clearPinned()等)。 Ag-grids default method for defining the grid appears to use hard coded column headers. 用于定义网格的Ag-grids默认方法似乎使用硬编码的列标题。 At least that is what is demonstrated in the example I am following, using the vanilla JS method: ( https://www.ag-grid.com/javascript-grid-pinning/ ) 至少这是我在下面的示例中使用香草JS方法演示的内容:( https://www.ag-grid.com/javascript-grid-pinning/

The column pinning functionality for the grid is working. 网格的列固定功能正在运行。 I can pin all columns but in order to reset them I have to refresh the page. 我可以固定所有列,但是要重置它们,我必须刷新页面。 I want to reset the columns using the clearPinned() method. 我想使用clearPinned()方法重置列。 I am storing my column headers in a variable called 'allSampleTableColumns'. 我将列标题存储在名为“ allSampleTableColumns”的变量中。 I am passing that into the gridOptions variable instead of a hard coded columnDefs variable. 我将其传递给gridOptions变量,而不是硬编码的columnDefs变量。 In my clear pinned function, I am trying to specify the 'Sample name' label, for example, and have the clear pinned reset that column after I pin it. 例如,在我的清除固定功能中,我尝试指定“样品名称”标签,并在固定该列后让清除固定重设该列。

Method from ag-grid with hard coded column headers: 来自ag-grid的方法,带有硬编码的列标题:

Html: 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: 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);
        }

My code: 我的代码:

Instead of columns refs (), I am using: 我使用的不是列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"}}));

The column headers are coming from a python variable which is being passed to the page by my pyramid application: dataFromPython.sampleTable.columns 列标题来自python变量,该变量由我的金字塔应用程序传递到页面:dataFromPython.sampleTable.columns

It is an array of objects containing all the column headers required: 它是包含所需的所有列标题的对象数组:

[{ "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" }]

I am passing it into my ag-grid gridOptions variable: 我将其传递到我的ag-grid gridOptions变量中:

var gridOptions = {
        defaultColDef: {
            resizable: true
        },
        columnDefs: allSampleTableColumns,
        debug: true,
        rowData: dataFromPython.allSampleTable,
    };

My clearPinned function looks like this, I am trying to specify which column ('Sample name') to reset after I have pinned it. 我的clearPinned函数如下所示,我试图指定固定后要重置的列(“样品名称”)。

clearPinned: function(){
                gridOptions.columnApi.setColumnPinned(this.allSampleTableColumns['Sample name'], null);
            }

The expectation is that hitting the clear pinned button will reset all columns back to the specified layout (only one column, sampleId should be pinned by default). 预期点击清除固定按钮会将所有列重置为指定的布局(默认情况下,只应固定一列sampleId)。 Nothing is happening when I call the clearPinned function. 当我调用clearPinned函数时,什么都没有发生。

This is because the setColumnPinned method refers to the colId that is supplied. 这是因为setColumnPinned方法引用了所提供的colId In this case, you don't have a column with an id of Sample name , but you have a headerName with a value of Sample name . 在这种情况下,您没有ID为Sample name的列,但有headerName值为Sample name的值。

To fix this, you just need to add colId declarations to your columnDefs objects. 为了解决这个问题,你只需要添加colId声明您columnDefs对象。

In the end the solution to this was to use the: var colState = gridOptions.columnApi.getColumnState(); 最后,解决方案是使用:var colState = gridOptions.columnApi.getColumnState(); found here: ag-grid.com/javascript-grid-column-definitions 在此处找到:ag-grid.com/javascript-grid-column-definitions

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

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