Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

Display Enum description in Jqgrid instead of the Enum

I have an Enum like this written in Java:

public enum Status
{
  ACTIVE("Active"), IN_ACTIVE("InActive");

  Status(String desc)
  {
    this.description = desc;
  }

  private String description;

  public String getDescription()
  {
    return description;
  }

  public void setDescription(String desc)
  {
    this.description = desc;
  }
}

This enum is a property in a jqGrid. But it always display the enum i.e. ACTIVE or IN_ACTIVE. I want the jqgrid to show Active and InActive. Thanks

Upvotes: 5

Views: 2040

Answers (2)

t0s6i
t0s6i

Reputation: 171

Have a toString() implemented in your Enum as follows

public toString() {
    return description;
}

This will make sure that your json response has description instead of Enum name.

Upvotes: 1

Justin Ethier
Justin Ethier

Reputation: 134227

You can write a custom formatter to achieve this. For example:

formatStatus: function (cellvalue, options, rowObject){
   if (cellvalue == "ACTIVE")
       return "Active";
   return "InActive";
}

Then make sure to use the formatter from your colmodel:

{name: 'status', formatter: formatStatus, ...},

Does that help?

Upvotes: 2

Related Questions