0

I need to implement custom sorting in jqGrid, in column we have html markup, which brakes proper sorting, example:

<a href="/Templates/Article.aspx?id=12884945915" class="link" title="Article  123">Article </a>

Now is there a way to set jqGrid to sort this column ignoring html markup.

update This is colmodel for this column

{"sortable":true,"name":"Title","index":"Title","hidden":false,"sorttype":null,"formatter":null,"formatoptions":null,"datefmt":null,"typeName":null},

And this is one example of column:

"Title":"<a href='/Templates/Article.aspx?id=4294967489' class='link' title='Sensor'>Sensor</a>"
jasin_89
  • 1,993
  • 7
  • 30
  • 41

1 Answers1

3

I suppose that you will don't have the described problem if you would construct contain of the column with respect of predefined formatter: "showlink" or with respect of your custom formatter. In case of usage custom formatter you should don't forget to define unformat function which get the text from the cells of the column.

I suppose that your problem will be solved if you just define unformat property for the column.

One more alternative option which one has is the usage of custom sorting. It's nothing more as specifying of sorttype as function. The answer and this one provide examples of such implementation. It's important to understand that sorttype will be used only in case of local sorting. If you use server side sorting then you should search for the origin of described problem in your server side code.

UPDATED: It's not recommended to use HTML fragments inside of data. In the way you mix the data with markup which makes sorting more difficult. Instead of that you could for example replace the input data

"Title":"<a href='/Templates/Article.aspx?id=4294967489' class='link' title='Sensor'>Sensor</a>"

to

"ArticleId":4294967489, "Title":"Sensor"

The <a> element inside of the cell you can construct with respect of custom formatter. In the case you could use column definition like

{name: "Title", width: 100,
    formatter: function (cellvalue, options, rowObject) {
        return "<a href='/Templates/Article.aspx?id=" + rowObject.refid +
            "' class='link' title='" + $.jgrid.stripHtml(cellvalue) + "'>" +
            $.jgrid.htmlEncode(cellvalue) + "</a>";
    }}

If the rowid which you use in the grid (the column with the name id or the column having the property key: true and which values are unique) is the same as ArticleId then you can use options.rowId instead of rowObject.refid.

You can read more about custom formatter for example in the answer.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I am not using custom formatter, and I don't know how to implement custom sorttype that will sort only by link name. – jasin_89 Dec 04 '12 at 16:40
  • 1
    @jasin_89: You should include the JavaScript code which you use. It's absolutely unclear how you fill the grid. How the cell get the value `Article `? How is defined the column in `colModel` where the cell exist? – Oleg Dec 04 '12 at 17:07
  • I added colmodel and json part for this column. – jasin_89 Dec 04 '12 at 17:30
  • Even with column defention like this, I am getting plain data not 'aaaa', what is wrong in it: {"sortable":true,"name":"Headline","index":"Headline","hidden":false,"sorttype":null,"formatter":"function (cellvalue, options, rowObject) {return 'aaaa'; }","formatoptions":null,"datefmt":null,"typeName":null}, – jasin_89 Dec 06 '12 at 13:11
  • 1
    @jasin_89: The value of `formatter` which you posted as **string** and not as function!!! Object with functions can't be serialized as JSON. I don't understand why you want to build *all* data of `colModel` per Ajax. – Oleg Dec 06 '12 at 13:40
  • ah, I am serializing class from server to JSON, as I know we can't pass function in JSON :( – jasin_89 Dec 06 '12 at 14:01
  • 1
    @jasin_89: There are *many* ways to solve the problem. Till now we spend a lot of time in writing back and forth. I still don't know what you do, how the jqGrid definition (JavaScript code) looks like, how looks backend program (web application),which format you use as input for jqGrid and so on. You should describe your problem and your program more detailed to save our time. To solve problem with formatter you can for example define your own formatter as formatter plugin and use the formater *under the name* like `formatter: "myCustomFormatter"`. – Oleg Dec 06 '12 at 14:18
  • 1
    @jasin_89: Look at [the answer](http://stackoverflow.com/a/9048483/315935) and in [the code](https://github.com/OlegKi/jqGrid-plugins/blob/master/jQuery.jqGrid.dynamicLink.js) for an example how to register own formatter as plugin. – Oleg Dec 06 '12 at 14:40