knockout-sortable icon indicating copy to clipboard operation
knockout-sortable copied to clipboard

Added helper function to fix table widths when dragging

Open nicholasjackson opened this issue 11 years ago • 3 comments

When dragging table rows the widths of the cells compress to content and loose their set values. I have added a helper function to the sortable options to fix the widths.

sortable.options.helper = function(e, ui) {
  ui.children().each(function() {
     $(this).width($(this).width());
  });
  return ui;
};

nicholasjackson avatar Apr 06 '13 13:04 nicholasjackson

This is a great addition. Please accept it!

BobbyRichard avatar May 30 '13 19:05 BobbyRichard

It would be nice to restrict this in some way to tables, so it doesn't need to run for every sortable all of the time.

rniemeyer avatar Aug 17 '13 18:08 rniemeyer

@nicholasjackson Would you mind me giving it a go and adapting your PR to be restricted to tr elements? Unless, of course, you'd like to re-submit yourself. Just asking since I'd love to see this supported.

I suppose it's simply a matter of:

sortable.options.helper = function(e, ui) { if (ui.is('tr')) { ui.children('td, th').each(function() { $(this).width($(this).width()); }); } return ui; }; Not sure if the `if (ui.is('tr'))` check is necessary - just thought it may be faster than searching the children for `td`/`th` every time.

Edit - removed the element query since, rather obviously, nothing else could/should be inside a tr:

sortable.options.helper = function(e, ui) {
  if (ui.is('tr')) {
    ui.children().each(function() {
      $(this).width($(this).width());
    });
  }
  return ui;
};

JD-Robbs avatar Mar 01 '16 10:03 JD-Robbs