EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: CLKG on October 20, 2015, 12:05:22 AM



Title: extend datagrid onSelect event
Post by: CLKG on October 20, 2015, 12:05:22 AM
Hi, since datagrid doesn't have the "onSelectChanged" event, I try to write one myself.
I just extend the default "onSelect" event and keep a "selectedIndex" value in the opts, when the selectedIndex changes, trigger the onSelectChanged event.
But I realize that whenever I wanna use the onSelect event, I need to paste this code snippet everywhere...
So is there any better way?

Plus, I think it's better to add a "getSelectedIndex" method to datagrid, instead of getRowIndex + getSelected.


Title: Re: extend datagrid onSelect event
Post by: stworthy on October 20, 2015, 01:43:45 AM
Please try this code to trigger the 'onSelectChanged' event. You only need to include the code to your page and do not need to paste it everywhere.
Code:
(function($){
  $.extend($.fn.datagrid.defaults, {
    onSelect: function(index, row){
      var opts = $(this).datagrid('options');
      if (opts.onSelectRow){
        opts.onSelectRow.call(this, index, row);
      }
      if (opts.selectedIndex != index){
        opts.selectedIndex = index;
        if (opts.onSelectChanged){
          opts.onSelectChanged.call(this, index, row);
        }
      }
    }
  })
})(jQuery);