Author Topic: Please tell me how to sort <a href>tag  (Read 3327 times)

hagix

  • Newbie
  • *
  • Posts: 2
    • View Profile
Please tell me how to sort <a href>tag
« on: August 06, 2015, 09:50:22 pm »
I want to sort <a href>tag.
but not work it.

(for example)
 $(function () {
        var data = [[1, 'Exxon Mobil', '339,938.0', '36,130.0'],
            [2, '<a href="c.shtml">1</a>', '315,654.0', '11,231.0'],
         [3, '<a href="b.shtml">2</a>', '306,731.0', '25,311.0'],
         [4, '<a href="a.shtml">3</a>', '267,600.0', '22,341.0'],
         [5, 'General Motors', '192,604.0', '-10,567.0'],
         [6, 'Chevron', '189,481.0', '14,099.0'],
         [7, 'DaimlerChrysler', '186,106.3', '3,536.3'],
         [8, 'Toyota Motor', '185,805.0', '12,119.6'],
         [9, 'Ford Motor', '177,210.0', '2,024.0'],
         [10, 'ConocoPhillips', '166,683.0', '13,529.0'],
         [11, 'General Electric', '157,153.0', '16,353.0'],
         [12, 'Total', '152,360.7', '15,250.0'],
         [13, 'ING Group', '138,235.3', '8,958.9'],
         [14, 'Citigroup', '131,045.0', '24,589.0'],
         [15, 'AXA', '129,839.2', '5,186.5'],
         [16, 'Allianz', '121,406.0', '5,442.4'],
         [17, 'Volkswagen', '118,376.6', '1,391.7'],
         [18, 'Fortis', '112,351.4', '4,896.3'],
         [19, 'Crédit Agricole', '110,764.6', '7,434.3'],
         [20, 'American Intl. Group', '108,905.0', '10,477.0']];


        var obj = {
            scrollModel: {autoFit: true},
            height: 400,
            colModel: [
                { title: "Rank", width: 100, dataType: "integer" },
                { title: "Company", width: 200, sortType: function (rowData1, rowData2, dataIndx) {
                    var val1 = rowData1[dataIndx],
                        val2 = rowData2[dataIndx],
                        c1 = $.trim(val1).length,
                        c2 = $.trim(val2).length;

                    if (c1 > c2) {
                        return 1;
                    }
                    else if (c1 < c2) {
                        return -1;
                    }
                    else {
                        return 0;
                    }
                }
                },
                { title: "Revenues ($ millions)", width: 180, dataType: "float" },
                { title: "Profits ($ millions)", width: 180, dataType: "float" }
            ],
            dataModel: {
                data: data,
                sortIndx: 1,
                sortDir: "up"
            }
        };

        $("#grid_custom_sorting").pqGrid(obj);
    });

When I push table-head(named "company", sort this.
2   <a href="c.shtml">1</a>   315,654.0   11,231.0
3   <a href="b.shtml">2</a>   306,731.0   25,311.0
4   <a href="a.shtml">3</a>   267,600.0   22,341.0

and next I re-pushed table head , sort this.
15   AXA   129,839.2   5,186.5
12   Total   152,360.7   15,250.0
18   Fortis   112,351.4   4,896.3
6   Chevron   189,481.0   14,099.0
16   Allianz   121,406.0   5,442.4
13   ING Group   138,235.3   8,958.9
14   Citigroup   131,045.0   24,589.0
9   Ford Motor   177,210.0   2,024.0
17   Volkswagen   118,376.6   1,391.7
1   Exxon Mobil   339,938.0   36,130.0
8   Toyota Motor   185,805.0   12,119.6
5   General Motors   192,604.0   -10,567.0
10   ConocoPhillips   166,683.0   13,529.0
7   DaimlerChrysler   186,106.3   3,536.3
19   Crédit Agricole   110,764.6   7,434.3
11   General Electric   157,153.0   16,353.0
20   American Intl. Group   108,905.0   10,477.0
2   <a href="c.shtml">1</a>   315,654.0   11,231.0
3   <a href="b.shtml">2</a>   306,731.0   25,311.0
4   <a href="a.shtml">3</a>   267,600.0   22,341.0

I expect next sort pushed , sort this.
4   <a href="a.shtml">3</a>   267,600.0   22,341.0
3   <a href="b.shtml">2</a>   306,731.0   25,311.0
2   <a href="c.shtml">1</a>   315,654.0   11,231.0

because display value is it correct sort.
But actuary did not sort.
I think sort key is inner <h ref="a/b/c">

please tell me how to implement.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6255
    • View Profile
Re: Please tell me how to sort <a href>tag
« Reply #1 on: August 08, 2015, 12:15:21 am »
Easier and recommended way is to keep 1, 2, 3 in the data and use column.render to add <a href> tags in the view. That way the sorting would work as expected and you would be able to see <a href> tags in the view.

Alternative way is to write a regular expression or do string manipulation to extract 1, 2, 3 from <a href> tags in sortType callback for custom sorting.
« Last Edit: August 08, 2015, 12:18:15 am by paramquery »

hagix

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Please tell me how to sort <a href>tag
« Reply #2 on: August 10, 2015, 09:36:55 am »
Thank you for your reply.
I resolved this problem.