This is a migrated thread and some comments may be shown as answers.
How to avoid duplicate entry in Grid
9 Answers 333 Views
This is a migrated thread and some comments may be shown as answers.
Quest Resource Management
Top achievements
Rank 1
Quest Resource Management asked on 27 Dec 2012, 02:56 PM
Hello,

How to avoid duplicate entry in Grid / Inline editing?

I am used MVC 4 With Eazor.

Thanks,
Quest

9 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 28 Dec 2012, 02:08 PM
Hello Eric,

 We are not sure what you mean by duplicate. Could you please clarify? Generally speaking you can implement a custom validator to perform any non-standard validation.

Greetings,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Quest Resource Management
Top achievements
Rank 1
answered on 29 Dec 2012, 08:50 AM
Hello Atanas,

Please check attached screenshot.

Thanks,
Quest

0
Atanas Korchev
Telerik team
answered on 29 Dec 2012, 09:12 AM
Hi Eric,

 Thank you for the clarification. I understand now what you need.

 Currently Kendo Grid does not provide any built-in feature which would prevent such cases. Perhaps this can be implemented using custom validation. Here is a forum thread discussion custom validation in the context of grid editing. This thread may also be interesting to you.

Kind regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Robert
Top achievements
Rank 2
answered on 13 Jul 2018, 03:59 PM

I use custom validation on the schema field:

Name: {
    editable: true,
    type: "string",
    validation: {
        required: true,
        duplicate: function (input) {
            if (input.is("[name='Name']") && input.val() != "") {
                // 1. get data
                var name = input.val();
                var data = $("#personnel_screen_grid").data().kendoGrid.dataSource.view()
 
                // 2. check for duplicate
                var valid = true;
                for (var i = 0; i < data.length; i++) {
                    if (name == data[i].Name) {
                        valid = false;
                        input.attr("data-duplicate-msg", "Duplicates not allowed.");
                        break;
                    }
                }
                return valid;
            }
 
            return true;
        },
    },
},
0
Robert
Top achievements
Rank 2
answered on 13 Jul 2018, 04:22 PM

I forgot to check for a ID when you're adding a new row:

Name: {
    editable: true,
    type: "string",
    validation: {
        required: true,
        duplicate: function (input) {
            if (input.is("[name='Name']") && input.val() != "") {
                // 1. get data
                var name = input.val();
                var data = $("#personnel_screen_grid").data().kendoGrid.dataSource.view()
                console.log(JSON.stringify(data));
 
                // 2. check for duplicate
                var valid = true;
                for (var i = 0; i < data.length; i++) {
                    if ((name == data[i].Name) && data[i]._id) {
                        valid = false;
                        input.attr("data-duplicate-msg", "Duplicates not allowed.");
                        break;
                    }
                }
                return valid;
            }
 
            return true;
        },
    },
},
0
Robert
Top achievements
Rank 2
answered on 13 Jul 2018, 08:14 PM

You could not edit the current row name with the last code.  I fixed that here:

Name: {
    editable: true,
    type: "string",
    validation: {
        required: true,
        duplicate: function (input) {
            if (input.is("[name='Name']") && input.val() != "") {
                // 1. get data
                var name = input.val();
                var grid = $("#personnel_screen_grid").data('kendoGrid');
                var view = grid.dataSource.view();
                var dataItem = grid.dataItem(grid.current().closest("tr"));
                var name_edit = dataItem.Name;
 
                // 2. check for duplicate
                var valid = true;
                for (var i = 0; i < view.length; i++) {
                    if ((name == view[i].Name) && (name != name_edit) && view[i].id) {
                        valid = false;
                        input.attr("data-duplicate-msg", "Duplicates not allowed.");
                        break;   
                    }
                }
                return valid;
            }
 
            return true;
        },
    },
},
0
Han
Top achievements
Rank 1
answered on 18 Mar 2019, 09:01 AM

Hi Robert,

 

I tried this code to get a current row:

but it through an exception: Uncaught TypeError: grid.current is not a function 

var name = input.val();
 var grid = $scope.mainGridOptions;
 var data = grid.dataSource.view();
 var dataItem = grid.dataSource(grid.current().closest("tr"));
 console.log(dataItem);

 

0
Han
Top achievements
Rank 1
answered on 20 Mar 2019, 07:15 AM

Hello,

I've figured it out.

thank you

0
Imran
Top achievements
Rank 1
Iron
answered on 05 May 2021, 12:40 PM

I have achieved this by doing the following

 



 var name = input.val();
                                           var grid = $("#grid").data('kendoGrid');
                                           var view = grid.dataSource.view();
                                           var dataItem = grid.dataItem(input.closest("tr"));

 

When the grid enters edit mode, it creates an "editable" object that is available only for the edited row. It contains the data item of the data source and you could access it as follows: $("#grid").getKendoGrid().editable.options.model.
- by
Tsvetomir
Telerik team
on 07 May 2021, 07:14 AM
Tags
Grid
Asked by
Quest Resource Management
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Quest Resource Management
Top achievements
Rank 1
Robert
Top achievements
Rank 2
Han
Top achievements
Rank 1
Imran
Top achievements
Rank 1
Iron
Share this question
or