This is a migrated thread and some comments may be shown as answers.

Add/Remove KendoSortable Dynamically

19 Answers 1726 Views
Grid
This is a migrated thread and some comments may be shown as answers.
VP
Top achievements
Rank 1
VP asked on 30 Sep 2015, 08:18 PM

Hello

 I am working on Including KendoSortable in my Kendo UI Grid. I am able to add it on a radio button select event (as below). 

 http://demos.telerik.com/kendo-ui/sortable/integration-grid

 This is how I am able to add it:

 currentGrid.table.kendoSortable({
                filter: filter,
                cursor: "move",
                ignore: ignore,
                hint: $.noop,
                //hint: function (element) { //customize the hint
                //    return element.clone().addClass("hint");
                //},
                placeholder: function (element) {
                    return element.clone().addClass("k-state-hover").css("opacity", 0.65);
                },...............

 How can I remove it and re-add it dynamically, say on addSortable and removeSortable radio button click events.

Thanks.

19 Answers, 1 is accepted

Sort by
0
VP
Top achievements
Rank 1
answered on 01 Oct 2015, 01:59 PM

I would appreciate if I can get some pointers to my earlier question.

 Thanks.

0
Alexander Valchev
Telerik team
answered on 02 Oct 2015, 01:05 PM
Hi,

You may destroy the Sortable widget at the time when it has to be removed and re-initialize it again when it has to be added back.

currentGrid.table.data("kendoSortable").destroy(); //remove
 
currentGrid.table.kendoSortable({ ..... }); //add

I hope the information will help.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
VP
Top achievements
Rank 1
answered on 05 Oct 2015, 06:14 PM

Hi 

I tried this and it does not remove the dragDrop (kendoSortable widget) feature from the grid. I am making this change on GridDataBound Event. Am I doing something wrong - please see attached code.

 Thanks,

VP

0
Alexander Valchev
Telerik team
answered on 07 Oct 2015, 08:31 AM
Hello,

The else statement will never be executed because the if statement will be always true.
That said the destroy method of the Sortable widget will never be called.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
VP
Top achievements
Rank 1
answered on 07 Oct 2015, 01:33 PM

Sorry for the confusion. The code that I pasted was just a sample. The If statement has a condition to be met (based on the radio button clicked). So in the right scenarios it will be called (as of course its not if(true) in my actual code - I cannot paste my code here as per policy). So my question still stays - the destroy does not remove the kendoSortable widget.

 currentGrid.table.data("kendoSortable").destroy(); //remove ---- does not work.

Can you please provide with a working sample with adding and removing kendoSortable from a grid based on a event (say 2 radiobuttons).

 

Thanks.

 

0
Alexander Valchev
Telerik team
answered on 09 Oct 2015, 08:31 AM
Hello,

Please check the following example: http://dojo.telerik.com/IqIBI

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
VP
Top achievements
Rank 1
answered on 13 Oct 2015, 03:14 PM

I tried in the way you suggested, but the only difference with my approach is I am using AngularJS. In your example, if I am not wrong, I see that the grid gets created every time the 'Toggle Sortable' button is clicked. In my case, I am not creating the grid every time on the radio button click events.

My Grid is defined as below on my html:
<div id="cmGrid"
 k-editable="vm.editableOptionsCM"
 kendo-grid="cmGrid"
 k-on-edit="vm.onCMEdit(kendoEvent)"
 k-on-cancel="vm.onCMCancel()"
 k-on-remove="vm.onCMDelete(kendoEvent)"
 k-on-save="vm.onCMSave(kendoEvent)"
 k-sortable="false"
 k-data-source="vm.gridDataSourceCM"
 k-scrollable="false"
 k-columns="vm.nmCMGridColumns"
 k-data-bound="vm.cmGridDataBound">
 </div>

All the vm objects defined above have the corresponding definitions in my js file.
I have 3 radio buttons: addKendoSortable, removeKendoSortable1, removeKendoSortable2. Initially on the load of the html page, I add the kendoSortable widget only if addKendoSortable is selected, else I add it on the select event of addKendoSortable radio button after the initial load and rebind the grid datasource. The issue here is I am not able to remove the kendoSortable on removeKendoSortable1, removeKendoSortable2 select events with the destroy() code you provided. Can you please provide with an example with AngularJS or do I have to go with the jQuery way to implement this functionality.

When addKendoSortable radio button is selected, I call the function to add the kendoSortable on the GridDataBound event

(see below code).

vm.cmGridDataBound = function (e) {
            var currentGrid = e.sender;
   
            if (when addKendoSortable radio is selected) {
                sortGrid(currentGrid, ">tbody >tr", "#cmGrid tbody", "");
            }
  }
 
 function sortGrid(currentGrid, filter, container, ignore) {
            currentGrid.table.kendoSortable({
                filter: filter,
                cursor: "move",
                ignore: ignore,
                hint: $.noop,
               
                placeholder: function (element) {
                    return element.clone().addClass("k-state-hover").css("opacity", 0.65);
                },
                container: container,
                change: function (e) {
                    var grid = currentGrid,
                      oldIndex = e.oldIndex, //the old position
                      newIndex = e.newIndex, //the new position
                      view = grid.dataSource.view(),
                      dataItem = currentGrid.dataSource.getByUid(e.item.data("uid")); //retrieve the moved dataItem
                    dataItem.Order = newIndex; //update the order
                    dataItem.dirty = true;
                    //shift the order of the records
                    if (oldIndex < newIndex) {
                        for (var i = oldIndex + 1; i <= newIndex; i++) {
                            view[i].Order--;
                            view[i].dirty = true;
                        }
                    } else {
                        for (var j = oldIndex - 1; j >= newIndex; j--) {
                            view[j].Order++;
                            view[j].dirty = true;
                        }
                    }
                    currentGrid.dataSource.remove(dataItem);
                    currentGrid.dataSource.insert(newIndex, dataItem);
                 
                }
            });
        }
 
 I am able to add the kendoSortable as shown above, but I cannot remove it when removeKendoSortable1 or removeKendoSortable2 radiobuttons are selected.
 
 

0
Alexander Valchev
Telerik team
answered on 15 Oct 2015, 02:47 PM
Hi,

This is the first time you mention that this is AngularJS application.
For AngularJS application I will recommend to use the k-rebind feature to change the Sortable settings which will allow you to enable/disable the widget.

I tested the scenario but encountered with a bug on our side which we will have to fix at first. I will mark this thread as 'requires additional answer" and will get back to you when the k-rebind issue is fixed.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
VP
Top achievements
Rank 1
answered on 19 Oct 2015, 05:59 PM

Hi Alexander

 Thanks for your response. Please post an example when the said issue is resolved.

 VP

0
VP
Top achievements
Rank 1
answered on 09 Nov 2015, 06:31 PM
Please let me know if this issue has been resolved and a new build is available, if not, when would it be fixed?
0
Alexander Valchev
Telerik team
answered on 11 Nov 2015, 12:21 PM
Hi,

The issue is fixed and the fix will be available in the service pack release which should be available for download till the end of this week.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
VP
Top achievements
Rank 1
answered on 05 Jan 2016, 04:00 PM

Hi,

Can you please provide a working example with AngularJS after this fix, where we can add and remove kendosortable dynamically. Please let me know what version of kendo should I upgrade to for me to get this fix.

 Thank You.

 VP

0
Alexander Valchev
Telerik team
answered on 07 Jan 2016, 03:58 PM
Hi,

Here is an example: http://dojo.telerik.com/IwAwO
It works with Q3 SP (2015.3.1111) and later versions.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
lorraine
Top achievements
Rank 1
answered on 05 May 2017, 11:57 AM

Given the original example of http://demos.telerik.com/kendo-ui/sortable/sortable-panels,I have added a chart to one of the panels and i am unable to interact with the chart because the focus is on the panel and dragging on the chart drags the panel.

 

I have tried adding a mousedown handler on the chart but have been unable to disable the dragging on the parent panel.  I have seen various posts about having to call destroy() on the kendoSortable() however using the above example I am unable to call destroy().

 

Any assistance would be greatly appreciated,

Thanks,

0
lorraine
Top achievements
Rank 1
answered on 05 May 2017, 01:33 PM
the method of calling destroy and recreating the kendoSortable works if the example is run full screen 
0
Stefan
Telerik team
answered on 09 May 2017, 11:08 AM
Hello Lorraine,

Please have in mind that the Dojo is using an iframe when it is not in fullscreen mode, and due to the iframe specifics, some functionalities does not work as expected.

We do recommend testing all of the examples in fullscreen mode to achieve a result similar to a real application.

Regards,
Stefan
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
0
Vigneswar
Top achievements
Rank 1
answered on 31 May 2018, 11:12 AM
Kendo UI grid sortable(or reordering or drag drop rows in grid) not disabled on using kendoSortable destroy jQuery in all scenarios.

I've a scenario in which the grid reordering or drag-drop rows(can be achieved using kendoSortable in kendo UI) should be enabled or disabled. 
For ex., if a particular set of columns are sorted or grouped the drag-drop function should be disabled and if one specific column is sorted it should be enabled. 
The problem occurs when applying filter to some columns. During filtering if kendoSortable is applied to a column it stays the same even if ("kendoSortable").destroy() is executed i.e the drag-drop functionality should be disabled on executing destroy() only that it wasn't. All it did was return some kinda error while the page was inspected.
Find attachment screen of error with this question or the text of error,

Error in event handler for (unknown): TypeError: Cannot read property 'options' of undefined
    at y.<anonymous> (chrome-extension://mgijmajocgfcbeboacabfgobmjgjcoja/content.min.js:16:421)

Attaching the jQuery method(will be invoked from databound event of grid) that implements the function,

    KendoCustom.OnSortAndGroup = function (e) {
    var grid = e.sender;    
    if (grid.dataSource.group() && grid.dataSource.group().length > 0
        && grid.dataSource.sort() && grid.dataSource.sort().length > 0) {
        grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'default');
                 });

        if ($("#RulesGrid").data("kendoSortable")) {
            $("#RulesGrid").data("kendoSortable").destroy();
        }
    }
    else if (grid.dataSource.sort() && grid.dataSource.sort().length > 0) {
        var col = grid.dataSource.sort();
        if (col[0]["field"] == 'Rank' || col[0]["field"] == 'VisualRank') {
            grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'move');
                 });

            $("#RulesGrid").kendoSortable({
                "change": onChangeDrag,
                "filter": "table > tbody > tr",
                "container": "#RulesGrid tbody",
                "cursor": "move",
                "hint": noHint,
                "placeholder": placeholder
            });
        }
        else {
            grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'default');
                 });

            if ($("#RulesGrid").data("kendoSortable")) {
                $("#RulesGrid").data("kendoSortable").destroy();
            }
        }
    }
}
0
Vigneswar
Top achievements
Rank 1
answered on 31 May 2018, 11:14 AM

I've a scenario in which the grid reordering or drag-drop rows(can be achieved using kendoSortable in kendo UI) should be enabled or disabled. 
For ex., if a particular set of columns are sorted or grouped the drag-drop function should be disabled and if one specific column is sorted it should be enabled. 
The problem occurs when applying filter to some columns. During filtering if kendoSortable is applied to a column it stays the same even if ("kendoSortable").destroy() is executed i.e the drag-drop functionality should be disabled on executing destroy() only that it wasn't. All it did was return some kinda error while the page was inspected.

The text of error,
Error in event handler for (unknown): TypeError: Cannot read property 'options' of undefined
    at y.<anonymous> (chrome-extension://mgijmajocgfcbeboacabfgobmjgjcoja/content.min.js:16:421)

Attaching the jQuery method(will be invoked from databound event of grid) that implements the function,

    KendoCustom.OnSortAndGroup = function (e) {
    var grid = e.sender;    
    if (grid.dataSource.group() && grid.dataSource.group().length > 0
        && grid.dataSource.sort() && grid.dataSource.sort().length > 0) {
        grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'default');
                 });

        if ($("#RulesGrid").data("kendoSortable")) {
            $("#RulesGrid").data("kendoSortable").destroy();
        }
    }
    else if (grid.dataSource.sort() && grid.dataSource.sort().length > 0) {
        var col = grid.dataSource.sort();
        if (col[0]["field"] == 'Rank' || col[0]["field"] == 'VisualRank') {
            grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'move');
                 });

            $("#RulesGrid").kendoSortable({
                "change": onChangeDrag,
                "filter": "table > tbody > tr",
                "container": "#RulesGrid tbody",
                "cursor": "move",
                "hint": noHint,
                "placeholder": placeholder
            });
        }
        else {
            grid.tbody.find('>tr').each(
                 function () {
                     $(this).css('cursor', 'default');
                 });

            if ($("#RulesGrid").data("kendoSortable")) {
                $("#RulesGrid").data("kendoSortable").destroy();
            }
        }
    }
}

0
Stefan
Telerik team
answered on 01 Jun 2018, 05:48 AM
Hello, Vigneswar,

I provided an answer to the same questions asked in the following forum post.

https://www.telerik.com/forums/sorting-and-filtering-not-working-in-kendo-grid

If possible, please continue the discussion in only one of the threads by your choice.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
VP
Top achievements
Rank 1
Answers by
VP
Top achievements
Rank 1
Alexander Valchev
Telerik team
lorraine
Top achievements
Rank 1
Stefan
Telerik team
Vigneswar
Top achievements
Rank 1
Share this question
or