How to make the Kendo UI grid collapse duplicate rows

I have Kendo UI gridone that can add new lines.

The added row may have the same identifier as the existing row, I need to delete the old existing rows.

Wrote code for this, but it does not work.

function checkSameID(e){
    if(e.type != 'create'){
        return false;
    }
    var grid = $("#grid").data("kendoGrid");
    $.map(e.response, function(row){

        grid.table.find('tbody tr').each(function(){
            var $this = $(this);
            var id = $('td:first-child', $this).html();
            if(id == row.id){
                var uid = $this.data('uid');
                grid.collapseRow(grid.table.find('tr[data-uid="' + uid + '"]'));
            }
        });
    });
}

dataSource.bind("requestEnd", checkSameID);

Where is my problem?

UPDATE

DataSource:

dataSource = new kendo.data.DataSource({
    transport: {
        read:  {
            url: crudServiceBaseUrl,
            dataType: "json",
            type: 'post',
            data:{
                'table':'user','action':'get'}
        },
        update: {
            url: crudServiceBaseUrlSave,
            dataType: "json",
            type: 'POST'
        },
        destroy: {
            url: crudServiceBaseUrlSave,
            dataType: "json",
            type: 'POST',
        },
        create: {
            url: crudServiceBaseUrlSave,
            dataType: "json",
            type: 'POST',
        },
        parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
                return {table:'user',action:operation, models: kendo.stringify(options.models)};
            }

            return {'table':'user','action':'get'};
        }
    },
    success: function(e){
        console.log(e);
    },
    batch: true,
    pageSize: 20,
    schema: {
        model: {
            id: "id",
            fields: {
                id: {
                    editable: false, nullable: true },
                    percent: {
                        type: "number", validation: {
                            required: true}
                    },
                    active: {
                        type: "boolean" },
                        group:{
                            defaultValue: {
                                id:0,name:'Group'},validation: {
                                    required: true }
                        },
                        date:{
                            editable: false, nullable: true },
                            user_name:{
                                editable: false, nullable: true },
            }
        }
    }
});
+4
source share
3 answers

I think it can help you,

function onSave(e){
    var currentProductName = e.model.ProductName;
    var currentProductID = e.model.ProductID;
    var data = this.dataSource.data();
    for(item in data){
        if(data[item].ProductName == currentProductName &&
           data[item].ProductID != currentProductID){
            e.preventDefault();
            alert("Duplicates found");
             // here you can delete your Duplicates
             // you had to pass ur UID to 'getByUid' function
             // var dataRow = $('#grid').data("kendoGrid").dataSource.getByUid(uid);
             // $('#grid').data("kendoGrid").dataSource.remove(dataRow);
        }

    }
}

then you can continue to delete the duplicate.

See:
1.Kendo grid check for duplicate values

2.Delting grid string by UID software

3.Adding and removing elements in the kendo.data.DataSource file

+4
source

, - - ( ) . .

+3

In IE browser, items like "data [item] .ProductName" cannot be read. therefore use as shown below.

var currentActionCode = e.model.lffaActionCode;
var data = this.dataSource.data();
var i=0;
for(item in data){
    if(e.model.isNew()  && (data[i] != undefined) &&(e.model.uid != data[i].uid) && (data[i].lffaActionCode == currentActionCode.toUpperCase())){
        e.preventDefault();
        $(
        "#errorNotification")
        .data(
                "kendoNotification")
        .show(
                "Duplicate values not allowed.",
                "error");
        break;
    }
    i++;
}
0
source

All Articles