How to avoid duplicate entry in Grid

7 posts, 0 answers
  1. Quest Resource Management
    Quest Resource Management avatar
    15 posts
    Member since:
    Oct 2012

    Posted 27 Dec 2012 Link to this post

    Hello,

    How to avoid duplicate entry in Grid / Inline editing?

    I am used MVC 4 With Eazor.

    Thanks,
    Quest
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 28 Dec 2012 Link to this post

    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!
  3. Quest Resource Management
    Quest Resource Management avatar
    15 posts
    Member since:
    Oct 2012

    Posted 29 Dec 2012 Link to this post

    Hello Atanas,

    Please check attached screenshot.

    Thanks,
    Quest

  4. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 29 Dec 2012 Link to this post

    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!
  5. Robert
    Robert avatar
    19 posts
    Member since:
    Mar 2015

    Posted 13 Jul Link to this post

    I use custom validation on the dataSource 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;
            },
        },
    },
  6. Robert
    Robert avatar
    19 posts
    Member since:
    Mar 2015

    Posted 13 Jul in reply to Robert Link to this post

    I forgot to check for a non blank 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;
            },
        },
    },
  7. Robert
    Robert avatar
    19 posts
    Member since:
    Mar 2015

    Posted 13 Jul in reply to Robert Link to this post

    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;
            },
        },
    },
Back to Top