1

I'm using jqgrid for which I create column definitions on server as dynamic objects and serialize them using Json.Encode:

html.Raw(System.Web.Helpers.Json.Encode(ColumnDefinition);

I have problem with applying custom formatter, as my serialized column definition is:

{"name":"Icon","index":"Icon","hidden":false,"formatter":"iconFormatter","unformat":{}}

Problem is in quotes which are added to all keys and values to respect JSON specification, and those around iconFormatter are problem in my case as I want that to be my function. Is there a simple solution for this?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79

1 Answers1

1

It seems to me that you have the same or close problem as described here. You will have to replace the string values of the formatter properties to the function reference. Pragmatic way is to search for the strings like "iconFormatter" (search for all custom formatters which you use) and replace there to the corresponding function refernce.

UPDATED: If you would be use template property inside of column definition (see here) you would be solve the problem in another way. Additionally you code will be shorter, more clear and better readable.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Yes, I have already done that search&replace on server, before Html.Raw, but I don't like that, as it solves only formatter problem, and there will be more functions. I will try now with functionsMapping and foreach through column models replacing that after jqgrid construction. – Goran Obradovic Jun 01 '11 at 08:37
  • @Oleg thanks, I have updated this post with solution based on first link you posted. Template is not what I need in this case, but it is very useful feature as I can declare part of definition in view. Thanks – Goran Obradovic Jun 01 '11 at 08:51
  • @obrad: It seems to me that the usage of column templates whould be the better way. See [here](http://stackoverflow.com/questions/6044047/stopping-columns-resizable-in-jqgrid/6047856#6047856) for detailes. See one more demo [here](http://www.ok-soft-gmbh.com/jqGrid/Localization1.htm) which use templates. Ohh! Now it seems to me that you do would has the same problems. – Oleg Jun 01 '11 at 08:53
  • @Oleg I'm aware (now :)) that it is also possible to do this with template by declaring formatter in template, but I did not do that because I need to add that property into my ColumnModel on server side, and update column definition, which will what I will do and put this into template also if anything else arrives that should be done with this column :) – Goran Obradovic Jun 01 '11 at 08:56
  • Hm, it seems changing formatter after grid construction does not work (jqgrid does not apply new function). It seems that I have to use first solution (string replace) in combination with template, so only replacement will be that for template name. – Goran Obradovic Jun 01 '11 at 09:15
  • @obrad: I think that any way is possible. If you change the formatter on a column you will have to reload the grid with `$("#gridid").trigger("reloadGrid")`. – Oleg Jun 01 '11 at 09:26
  • @obrad: I agree that no the ways are perfect, but one can quickly make any from the ways working. – Oleg Jun 01 '11 at 09:27