简体   繁体   English

如何从 ag-grid 中的 valueFormatter 调用异步 function

[英]How to call async function from valueFormatter in ag-grid

I have an ag-grid table, and I have a column in it with value formatter:我有一个 ag-grid 表,其中有一列带有值格式化程序:

{
    headerName: "My column",
    valueFormatter: getFormattedIndexValue,
    ...
}

From getFormattedIndexValue I try to call async function:getFormattedIndexValue我尝试调用异步 function:

async function getFormattedIndexValue (params) {
    if (!params.value) return;
    return await getDecodedValue(table, params.colDef.field, params.value);
}

This is the code of async function, I try to call:这是异步 function 的代码,我尝试调用:

async function getDecodedValue(table, field, value) {
    const query = `function=decode&table=${table}&field=${field}&value=${value}`;
    const response = await fetch('routines.php', { method: 'post', body: query, headers: {"Content-Type": "application/x-www-form-urlencoded"}});
    return response.text();
}

But in this way valueFormatter doesn't return correct value, resulting in [Object Promise] .但是这样 valueFormatter 不会返回正确的值,从而导致[Object Promise] Is there a way to call async function from valueFormatter and to get proper result有没有办法从 valueFormatter 调用异步 function 并获得正确的结果

valueFormatter doesn't suit for it, but instead, you can create cellRenderer and you would be able to handle needed logic inside. valueFormatter不适合它,但相反,您可以创建cellRenderer并且您将能够在内部处理所需的逻辑。

but as I understood, you wanna have kind of reference (key to value, id(as key) on database and value(as displayed value) on view grid) - am I right?但据我所知,您想要某种参考(值的键,数据库上的 id(作为键)和视图网格上的值(作为显示值)) - 我说得对吗?

If so, ( valueFormatter and valueParser should be used) but without an async call, you need to have your key-value pair (dictionary) on grid-init process.如果是这样,(应使用valueFormattervalueParser )但没有异步调用,则需要在 grid-init 进程中使用键值对(字典)。

Here is my example:这是我的例子:

on ag-grid init process I'm saving dictionary for columns in config.ComboData在 ag-grid init 过程中,我正在为config.ComboData列保存字典

valueFormatter = (params) => {
    return this.lookupValue(config.ComboData['columnNameHere'], params.value);
};
valueParser = (params) => {
     return this.lookupKey(config.ComboData['columnNameHere'], params.newValue);
}

valueFormatter and params.value - used when ag-grid rendering the value. valueFormatterparams.value - 在 ag-grid 渲染值时使用。

valueParser and params.newValue - when you are updating the cell value. valueParserparams.newValue - 当您更新单元格值时。

and here looupValue and lookupKey functions from ag-grid doc and the sample在这里looupValuelookupKey函数来自 ag-grid doc示例

lookupValue(mappings, key:string) {
    return mappings[key];
}

lookupKey(mappings, name) {
    for (var key in mappings) {
        if (mappings.hasOwnProperty(key)) {
            if (name === mappings[key]) {
            return key;
            }
        }
    }
}

this is an old post but I hope this answer can help others because it helped me a lot.这是一篇旧帖子,但我希望这个答案可以对其他人有所帮助,因为它对我帮助很大。

I was using an async function to retrieve the data via Fetch to the Cell of the table.我使用异步 function 通过 Fetch 将数据检索到表的单元格。 No matter what I did, I always got [Object Promise] Then I used cellRenderer instead of ValueFormatter不管我做什么,我总是得到[Object Promise]然后我使用cellRenderer而不是ValueFormatter

I give an example here.我在这里举个例子。 I have some data with some people and their company ID's, I don't want the user to see company IDs in the table, I want them to see the company names instead.我有一些人及其公司 ID 的数据,我不希望用户在表中看到公司 ID,我希望他们看到公司名称。 These are the columns, I set the "company_id" field with cellRenderer ":这些是列,我用cellRenderer设置了“company_id”字段:

var columnDefsCuotas = [
       { field: 'user_id', width: 150 },
       { field: 'name', width: 250 },
       { field: 'company_id', width: 250, 
         cellRenderer: GetCompanyName
       }
 ]

Then I create a class called GetCompanyName Here I can render each cell of the table to print the name of the company instead of the ID of the company.然后我创建了一个名为GetCompanyName的 class 在这里我可以渲染表格的每个单元格以打印公司名称而不是公司 ID。 I put the fetch in this class to retrieve the data:我将 fetch 放入此 class 以检索数据:

class GetCompanyName {
    // gets called once before the renderer is used
    async init(params) {

        // create the cell
        this.eGui = document.createElement('div');
        this.eGui.innerHTML = `<span class="my-value"></span>`;

        // get references to the elements we want
        this.eValue = this.eGui.querySelector('.my-value');
    
        // set value into cell
        this.cellValue = this.getValueToDisplay(params);
        this.eValue.innerHTML = await getName(this.cellValue);
        
        async function getName(company_id) {
            let result = await fetch(`/fetch_companies?company_id=${company_id}`)
            let response = await result.json()
            console.log(response) // See if it is fetching the desired response data
            return response.company_name // I return only the parameter of the object I need to see
        }
    }
  
    getGui() {
        return this.eGui;
    }
  
    getValueToDisplay(params) {
        return params.valueFormatted ? params.valueFormatted : params.value;
    }
}

I hope it helps.我希望它有所帮助。 It worked like a charm for me它对我来说就像一个魅力

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

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