Ext js sorting custom column by contents

前端 未结 3 2081
悲&欢浪女
悲&欢浪女 2021-01-05 13:28

I have a grid in ext with some custom columns, and I want to be able to sort this column - I want to sort it by what is displayed inside of it, but really I just cannot figu

相关标签:
3条回答
  • 2021-01-05 13:44

    There is a convert method on the Ext.data.Model class that allows you to convert the data before it's being used. Then you can just specify this 'dataIndex' in your column and do a normal sort. The column will be sorted by that converted value. Here is the a sample model with just one field (Parent) and with it's corresponding conversion:

    Ext.define('MyModel', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'Parent',   type: 'string', convert: sortParent},
            // other fields...
        ],
        sortParent: function(value, record) {
            var ret = record.raw.Parent;
            if (ret) {
                return ret.Name;
            } else {
                meta.tdCls = 'invisible';
                return record.data.Name;
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-05 13:53

    For Ext JS version 5, it looks like doSort was taken out, so I couldn't override that. Instead, I went the route of listening to the sortchange event, and from there, I used the Ext.data.Store.setSorters method. The code is a bit custom, and overly complex because of the data that I'm using, so keep that in mind (Fiddle here):

    // grid class
    initComponent: function() {
      ...
      this.on('sortchange', this.onSortChange, this);
    },
    
    onSortChange: function(container, column, direction, eOpts) {
      // check for dayColumnIndex
      if (column && column.dayColumnIndex !== undefined) {
        this.sortColumnByIndex(column.dayColumnIndex, direction);
      }
    },
    
    sortColumnByIndex: function(columnIndex, direction) {
      var store = this.getStore();
      if (store) {
        var sorterFn = function(rec1, rec2) {
          var sortValue = false;
          if (rec1 && rec2) {
            var day1;
            var daysStore1 = rec1.getDaysStore();
            if (daysStore1) {
              day1 = daysStore1.getAt(columnIndex);
            }
            var day2;
            var daysStore2 = rec2.getDaysStore();
            if (daysStore2) {
              day2 = daysStore2.getAt(columnIndex);
            }
            if (day1 && day2) {
              var val1 = day1.get('value');
              var val2 = day2.get('value');
              sortValue = val1 > val2 ? 1 : val1 === val2 ? 0 : -1;
            }
          }
          return sortValue;
        };
        if (direction !== 'ASC') {
          sorterFn = function(rec1, rec2) {
            var sortValue = false;
            if (rec1 && rec2) {
              var day1;
              var daysStore1 = rec1.getDaysStore();
              if (daysStore1) {
                day1 = daysStore1.getAt(columnIndex);
              }
              var day2;
              var daysStore2 = rec2.getDaysStore();
              if (daysStore2) {
                day2 = daysStore2.getAt(columnIndex);
              }
              if (day1 && day2) {
                var val1 = day1.get('value');
                var val2 = day2.get('value');
                sortValue = val1 < val2 ? 1 : val1 === val2 ? 0 : -1;
              }
            }
            return sortValue;
          };
        }
        store.setSorters([{
          sorterFn: sorterFn
        }]);
      }
    }
    
    0 讨论(0)
  • 2021-01-05 14:00

    You should be able to override the doSort method of the column. Here's the gist of it. I also created a working fiddle (http://jsfiddle.net/cfarmerga/LG5uA/). The fiddle uses the string length of a field as the property to sort on, but of course you could apply your own custom sort logic.

    var grid = Ext.create('Ext.grid.Panel',{
        //...
        columns: [
            { text: 'name', dataIndex: 'name', sortable: true },
            {
                text: 'Custom',
                sortable : true,
                dataIndex: 'customsort',
                doSort: function(state) {
                    var ds = this.up('grid').getStore();
                    var field = this.getSortParam();
                    ds.sort({
                        property: field,
                        direction: state,
                        sorterFn: function(v1, v2){
                            v1 = v1.get(field);
                            v2 = v2.get(field);
                            return v1.length > v2.length ? 1 : (v1.length < v2.length ? -1 : 0);
                        }
                    });
                }
            }
        ]
       //....  
    });
    
    0 讨论(0)
提交回复
热议问题