EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: James on April 23, 2020, 08:31:39 PM



Title: How to change datagrid column sortable set after datargid is parsered
Post by: James on April 23, 2020, 08:31:39 PM
How to change datagrid column sortable set after datargid is parsered.

I want to use a switchbutton to enable/disable some columns sortable

Is there any way to do that . I know there is onBeforeSortColumn can return false to stop sort, but I don't want the column shows it can sort when not match the condition what I give.


Title: Re: How to change datagrid column sortable set after datargid is parsered
Post by: jarry on April 26, 2020, 03:17:00 AM
This is the extended method to enable or disable the sortable on a column.
Code:
(function($){
  function setSortable(target, field, sortable){
    var col = $(target).datagrid('getColumnOption', field);
    col.sortable = sortable;
    var state = $(target).data('datagrid');
    var dc = state.dc;
    var header = dc.header1.add(dc.header2);
    var icon = header.find('td[field="'+field+'"] .datagrid-sort-icon');
    sortable ? icon.show() : icon.hide();
  }

  $.extend($.fn.datagrid.methods, {
    enableSortable: function(jq, field){
      return jq.each(function(){
        setSortable(this, field, true)
      })
    },
    disableSortable: function(jq, field){
      return jq.each(function(){
        setSortable(this, field, false)
      })
    }
  })

})(jQuery);

Usage example:
Code:
$('#dg').datagrid('enableSortable', 'name');
$('#dg').datagrid('disableSortable', 'name');