-1

Is there any way to tap into dragstart/end event of the SlickGrid? I am trying to take the text from the SlickGrid column header and drop on the other object.

Mutant
  • 3,663
  • 4
  • 33
  • 53

1 Answers1

2

It uses jquery sortable for column sorting so you can hook to the events by modifying the setupColumnReorder function in slick.grid.js

I have added my own event handlers: onColumnsReorderStart onBeforeColumnsReordered

function setupColumnReorder() {
  $headers.filter(":ui-sortable").sortable("destroy");
  $headers.sortable({
    containment: "parent",
    distance: 3,
    axis: "x",
    cursor: "default",
    tolerance: "intersection",
    helper: "clone",
    placeholder: "slick-sortable-placeholder ui-state-default slick-header-column",
    start: function (e, ui) {
      ui.placeholder.width(ui.helper.outerWidth() - headerColumnWidthDiff);
      $(ui.helper).addClass("slick-header-column-active");
      trigger(self.onColumnsReorderStart, ui);//added HERE
    },
    beforeStop: function (e, ui) {
        $(ui.helper).removeClass("slick-header-column-active");
        trigger(self.onBeforeColumnsReordered, ui);//added HERE
    },
    stop: function (e) {
      if (!getEditorLock().commitCurrentEdit()) {
        $(this).sortable("cancel");
        return;
      }

      var reorderedIds = $headers.sortable("toArray");
      var reorderedColumns = [];
      for (var i = 0; i < reorderedIds.length; i++) {
        reorderedColumns.push(columns[getColumnIndex(reorderedIds[i].replace(uid, ""))]);
      }
      setColumns(reorderedColumns);

      trigger(self.onColumnsReordered, {});
      e.stopPropagation();
      setupColumnResize();
    }
  });
}

and in the same file you need to register the events:

// Public API

$.extend(this, {
  "slickGridVersion": "2.1",

  // Events
  "onScroll": new Slick.Event(),
  "onSort": new Slick.Event(),
  "onHeaderMouseEnter": new Slick.Event(),
  "onHeaderMouseLeave": new Slick.Event(),
  "onHeaderContextMenu": new Slick.Event(),
  "onHeaderClick": new Slick.Event(),
  "onHeaderCellRendered": new Slick.Event(),
  "onBeforeHeaderCellDestroy": new Slick.Event(),
  "onHeaderRowCellRendered": new Slick.Event(),
  "onBeforeHeaderRowCellDestroy": new Slick.Event(),
  "onMouseEnter": new Slick.Event(),
  "onMouseLeave": new Slick.Event(),
  "onClick": new Slick.Event(),
  "onDblClick": new Slick.Event(),
  "onContextMenu": new Slick.Event(),
  "onKeyDown": new Slick.Event(),
  "onAddNewRow": new Slick.Event(),
  "onValidationError": new Slick.Event(),

  "onViewportChanged": new Slick.Event(),
  "onColumnsReorderStart": new Slick.Event(),//added HERE
  "onColumnsReordered": new Slick.Event(),
  "onBeforeColumnsReordered": new Slick.Event(),//added HERE
  "onColumnsResized": new Slick.Event(),
  "onCellChange": new Slick.Event(),
  "onBeforeEditCell": new Slick.Event(),
  "onBeforeCellEditorDestroy": new Slick.Event(),
  "onBeforeDestroy": new Slick.Event(),
  "onActiveCellChanged": new Slick.Event(),
  "onActiveCellPositionChanged": new Slick.Event(),
  "onDragInit": new Slick.Event(),
  "onDragStart": new Slick.Event(),
  "onDrag": new Slick.Event(),
  "onDragEnd": new Slick.Event(),
  "onSelectedRowsChanged": new Slick.Event(),
  "onCellCssStylesChanged": new Slick.Event(),

...

Use it then like this:

    grid.onColumnsReordered.subscribe(function (s, e) {
        console.log('onColumnsReordered');
        var myItem = e.item;
    });
    grid.onColumnsReorderStart.subscribe(function (s, e) {
        console.log('onColumnsReorderStart');
        var myItem2 = e.item;
    });

    grid.onBeforeColumnsReordered.subscribe(function (s, e) {
        console.log('onBeforeColumnsReordered');
    });
Adam Moszczyński
  • 3,477
  • 1
  • 17
  • 18
  • It woks fine, Is there any way to show the value being dragged along with the arrow? I think shadow elements or something can be attached to the drag arrow and remove it once the drag ends, but not sure how to do it. – Mutant Jul 28 '14 at 18:13
  • Also, when try to drag the column outside, it moves horizontally in the table frame (even though I am able to get access to clicked element and retrieve it on the drag stops outside the table view. – Mutant Jul 28 '14 at 18:47
  • 1
    to remove movement constraints you need to remove "containment" and "axis" options from sortable initialization in slick.grid.js. As for the first comment I do not understand what value you want to show (text from header?); – Adam Moszczyński Jul 29 '14 at 05:44
  • It works fine, I am able to move it outside grid, though the drag value (column header) is not visible once the mouse is outside the column header boundary. Regarding the first comment, what I mean is when you drag something the value shown along with the moving cursor to show you what value is being dragged. – Mutant Jul 29 '14 at 19:52
  • Sounds like you have some zIndex issues when dragging the column header. Check your css for relative position container elements. Maybe u can post the example of this header hiding problem (like jsfiddle) so I can see what is going on on the live organism? – Adam Moszczyński Jul 30 '14 at 20:59
  • @AdamMoszczyński I am facing the same problem of z-index. I am able to drag the header outside and drop it, but while dragging, its not visible. http://plnkr.co/edit/yxBFVkcUQTo17ClFjYbv?p=info This is the link to plunker which shows the behaviuor. How can i solve this? – Vivek Vardhan Sep 16 '14 at 04:47
  • @Mutant : I had similar requirement, but i stuck with z-index problem, as you mentioned. Did u able to resolve it? The link to plunker is mentioned in above comment – Vivek Vardhan Sep 16 '14 at 04:49
  • @VivekVardhan No, because it was low priority at that point, we moved on with other stuff. I might give it a try sometime today or tomorrow and see if there any quick fix. – Mutant Sep 16 '14 at 18:26
  • That is because of the overflow:hidden in 3 containers of the dragged column. You can force it to visible: http://plnkr.co/edit/okDUN9EWJLscSa2nbh1k?p=preview – Adam Moszczyński Sep 17 '14 at 06:00