Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

reactjs - Clickable url value in ag-grid with react

I'm currently giving ag-grid a try and trying to build a table where if the user clicks a column value, they are taken to a page containing that entry's details.

How can I make a cell value clickable in ag-grid?

I've tried using valueGetter: this.urlValueGetter with columnDefs and:

urlValueGetter(params) {
  return '<a href='bill/' + params.data.id + ''>details</a>';
}

but it now looks like this:

enter image description here

I then tried using template: '<a href='bill/{id}'>details</a>' which does show the cell text as clickable but the id is not replaced. I assume this could work if I could somehow pass in the id?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want to use a cellRenderer for that, instead of valueGetter:

https://www.ag-grid.com/javascript-grid-cell-rendering-components/#gsc.tab=0

Random example from above documentation:

// put the value in bold
colDef.cellRenderer = function(params) {
    return '<b>' + params.value.toUpperCase() + '</b>';
}

You can return a string (easier) with your link if you don't want to attach any events.

Otherwise, here's an example of a colDef if you want to attach events to an element:

{
    headerName: 'ID',
    field: 'id',
    cellRenderer: (params) => {
        var link = document.createElement('a');
        link.href = '#';
        link.innerText = params.value;
        link.addEventListener('click', (e) => {
            e.preventDefault();
            console.log(params.data.id);
        });
        return link;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[6] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...