1

Each of my columns will be a complex type.

Using custom formatting I'd like to be able to display the text of one property, then colour the cell (or do anything really) dependant on the other property.

So for example:

public class MyRowObject
{
    public MyCellObject Cell1 { get; set; }
    public MyCellObject Cell2 { get; set; }
    public SomeOtherCellObject Cell3 { get; set; }

}

public class MyCellObject
{
    public string MyDisplayText { get; set; }
    public int MyNumber { get; set; }
}

then use a custom formatter javascript function to do stuff, for example:

function formatCourseData(cellValue, options, rowObject) {

    var linkHTML = cellValue.MyDisplayText;
    if (cellValue.MyNumber > 10) {

        //format the html in some way

    }

    return linkHTML;
}

Now I know I cannot do cellValue.MyDisplayText but this or something like is what I would like to be able to do.

Is this possible?

Is it documented anywhere? (I've looked, but cannot find anything).

thanks in advance.

ozz
  • 5,098
  • 1
  • 50
  • 73

1 Answers1

1

What you can do is pass each field of the complex object as its own column, and then within your formatter you can use the formatter's rowObject parameter to access the other value to do anything you need, such as coloring.

Alternatively, you may not need to use a second column if each number indicates the same display text. You could just use a select formatter (or a custom version of one), or a lookup table or such.

Anyway, here is an example of how you can use rowObject to access row data: access-row-data-in-jqgrid-custom-formatter

Does that help?

Community
  • 1
  • 1
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • thanks Justin. Yeah, essentially flattening out my sub-objects, and hiding the ones I don't want displayed. I'd rather not do that as it seems a little messy, but thanks for the suggestion. Have you used JQGrid a lot? are you saying there is no native way to do what I am looking for? – ozz May 23 '12 at 15:53
  • I have used the JavaScript jqGrid extensively, but have not used the Asp.NET versions much at all. That said, if you look at the examples from Trirand http://www.trirand.net/demoaspnetmvc.aspx - under `Functionality` | `Cell Formatters / Templates (custom)`, all of the custom formatters are written in JavaScript. – Justin Ethier May 23 '12 at 16:00
  • I'm using custom formatters already, I'm well versed in those, so that is not an issue. I could do what you suggest quite easily, but it seems like a limitation of JQGrid and I was hoping for a neater solution. It does not scale well the more properties you want to customise by. thanks again! – ozz May 23 '12 at 16:07
  • 1
    Agreed... maybe you should suggest that to them in the forums. It seems like a great feature, although it may require too many changes to the grid itself to be a practical feature for the short term. – Justin Ethier May 23 '12 at 16:59