ag-grid column search not working with object as cell value

爷,独闯天下 提交于 2020-05-13 04:53:35

问题


I am using cell_renderer to define my cell, something like this:

        var cell_renderer = function(params) {
            var element = "";
            var values = params.value;
                angular.forEach(values, function(each_param){
                    element += '<a href="#/workunit/' + each_param.id + '" target="_blank">' + each_param.id +
                            '<span class="text-danger text-bold">'+
                            '<span class="" title="' + inactive_message +
                            '">' + inactive_flag + ' </span>' +
                            '<span class="" title="' + misconfigured_message +
                            '"> ' +misconfigured_flag + '</span>'+
                            '</span></a><br>';
                })
            return element;
        };

My column definnition is this:

                var testObject = {};
                testObject.headerName = "my header";
                testObject.field = each_branch;
                testObject.width = 200;
                testObject.cellClassRules = cell_class_rules;
                testObject.cellRenderer = cell_renderer;
                columnDefs.push(testObject);

Here testObject.field = each_branch , is a json object.

Hence the inbuild search functionality not working in ag-grid. Can anyone please help me here.


回答1:


The solution is to provide valueGetter in filter params.

Say for example you have { name: 'Some Name', id: 1 }. In that case you want to filter by the name. So:

{
  filter: 'agTextColumnFilter',
  filterParams: {
    valueGetter: params => {
      return params.data.name
    }
  }
}



回答2:


By default the textFormatter from Ag-grid is returning a string representation, if you include textFormatter and return the same object in the textCustomComparator you'll have the object from the valueGetter:

   filter: "agTextColumnFilter",
filterParams: {
    textCustomComparator: function  (filter, value, filterText) {
        //value is the object from valueGetter
    },
    textFormatter: function(r) { 
        //this is the solution
        return r; 
    }, 
    valueGetter: function getter(params) {
        return {
            ...
        };
    }
}



回答3:


Try setting the text filter-properties on your column:

testObject.filter:'text',
testObject.filterParams:{
...
}

and then implement the textCustomComparator function in the filterParams object:

https://www.ag-grid.com/javascript-grid-filter-text/#gsc.tab=0




回答4:


When using a column that is an Object, you have at least 2 options:

  1. Specify a valueGetter function on the column. If you do this, the table will no longer contain the object, but rather the value returned by the valueGetter.

    valueGetter: this.getDateValueGetter("dateCol")

    private getDateValueGetter(col: string): Function { return function (p: ValueGetterParams) { if (!p.data) { return null; } return this.ui.convertStringToDate(p.data[col], "YYYY-MM-DD"); } }

  2. If you want your table to contain the object, specify both a keyCreator function and a valueFormatter function on the column. If you do this, the column, filters, and grouping will work as expected.

    { keyCreator: (param) => {return param.value}, valueFormatter: (param) => {return param.value} }



来源:https://stackoverflow.com/questions/43893083/ag-grid-column-search-not-working-with-object-as-cell-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!