When working with AG Grid table, how can i change cell value format while editing this cell

In AG Grid, how can i set different value formatters for the same cell?

The 1st formatter should apply when the cell is NOT in edit mode. In this case it should display an icon.

And the 2nd formatter when cell is in edit mode. In this case it should display text instead of an icon.

This way:

What i’ve already tried is to set custom valueFormatter and cellRenderer but it doesn’t work.

class CustomCellRenderer{
    eGui;

    init(params) {
        this.eGui = document.createElement('div');
        this.eGui.innerHTML = params.value.icon;
    }

    getGui() {
        return this.eGui;
    }

    refresh() {
        return false;
    }
}


const cellSettings = {
    field: 'event',
    cellDataType: 'object',
    valueFormatter: (params) => {
        return params.value.text;
    },
    cellEditor: 'agRichSelectCellEditor',
    cellRenderer: CustomCellRenderer,
    cellEditorParams: {
        allowTyping: true,
        filterList: true,
        highlightMatch: true,
        valueListMaxHeight: 200
    }
}

Here’s what my data for this cell looks like:

const cellData = [
    {
        text: 'Text',
        icon: icon
    },
    {
        text: 'Text',
        icon: icon
    },
    {
        text: 'Text',
        icon: icon
    },
    {
        text: 'Text',
        icon: icon
    },
    {
        text: 'Text',
        icon: icon
    }
];

To achieve different formatting for a cell in AG Grid based on whether it is in edit mode or not, you can use a combination of cellRenderer and cellEditor with custom logic. The cellRenderer will handle the display of the icon when the cell is not in edit mode, and the cellEditor will handle the display of the text when the cell is in edit mode.

Here’s how you can do it:

1. Custom Cell Renderer: This will render the icon when the cell is not in edit mode.

2. Custom Cell Editor: This will render the text when the cell is in edit mode.

Step 1: Custom Cell Renderer

Create a custom cell renderer to display the icon.

class CustomCellRenderer {
    eGui;

    init(params) {
        this.eGui = document.createElement('div');
        this.eGui.innerHTML = params.value.icon;
    }

    getGui() {
        return this.eGui;
    }

    refresh(params) {
        this.eGui.innerHTML = params.value.icon;
        return true;
    }
}

Step 2: Custom Cell Editor

Create a custom cell editor to display the text when the cell is in edit mode.

class CustomCellEditor {
    eInput;

    init(params) {
        this.eInput = document.createElement('input');
        this.eInput.value = params.value.text;
        this.eInput.style.width = '100%';
    }

    getGui() {
        return this.eInput;
    }

    afterGuiAttached() {
        this.eInput.focus();
        this.eInput.select();
    }

    getValue() {
        return {
            text: this.eInput.value,
            icon: params.value.icon // Assuming the icon remains the same
        };
    }

    refresh() {
        return false;
    }
}

Step 3: Apply to Column Definition

Configure your column definition to use the custom cell renderer and cell editor.

const cellSettings = {
    field: 'event',
    cellDataType: 'object',
    cellRenderer: CustomCellRenderer,
    cellEditor: CustomCellEditor,
    editable: true, // Make sure the cell is editable
    cellEditorParams: {
        // Additional params if needed
    }
}

Putting It All Together
Here is the complete setup:

const cellData = [
    {
        text: 'Text 1',
        icon: '<i class="fa fa-icon1"></i>'
    },
    {
        text: 'Text 2',
        icon: '<i class="fa fa-icon2"></i>'
    },
    {
        text: 'Text 3',
        icon: '<i class="fa fa-icon3"></i>'
    }
];

const columnDefs = [
    {
        headerName: "Event",
        field: 'event',
        cellDataType: 'object',
        cellRenderer: CustomCellRenderer,
        cellEditor: CustomCellEditor,
        editable: true
    }
];

const gridOptions = {
    columnDefs: columnDefs,
    rowData: cellData
};

// setup the grid after the page has finished loading
document.addEventListener('DOMContentLoaded', () => {
    const gridDiv = document.querySelector('#myGrid');
    new agGrid.Grid(gridDiv, gridOptions);
});

HTML Setup
Ensure you have a div with an ID of myGrid to initialize the grid.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AG Grid Example</title>
    <link rel="stylesheet" href="https://unpkg.com/ag-grid-community/styles/ag-grid.css">
    <link rel="stylesheet" href="https://unpkg.com/ag-grid-community/styles/ag-theme-alpine.css">
    <script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.noStyle.js"></script>
</head>
<body>
    <div id="myGrid" class="ag-theme-alpine" style="height: 100%; width: 100%;"></div>
    <script src="path/to/your/script.js"></script>
</body>
</html>

This setup ensures that the cell displays an icon when not in edit mode and switches to text input when in edit mode. Adjust the CustomCellRenderer and CustomCellEditor classes as needed to fit your specific requirements.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật