Cannot get destroy/delete to actually delete docs in db using REST Service

4 posts, 0 answers
  1. Bryan
    Bryan avatar
    13 posts
    Member since:
    Feb 2016

    Posted 02 Nov Link to this post

    I had deletes working in my grid but lost some of the code and have been trying without success the last 2 days to get it to work again.

    I have a REST service I call to do the actual delete, it is backed by Java. The relevant part of that code is below, followed by my grid.

    I have floundered around changing this and that and never can get it to work. The Java Rest Service detects that I am doing a DELETE but I cannot get the parts. I only really need the ID.

    What am I doing wrong?

     

     

     

     

     

     

    Java based REST service

    private void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Map prmMap = request.getParameterMap();
    // String id = prmMap.get("id").toString();
    String prmUNID = request.getParameter("id");
    Database DB = this.getAppData();
    // String t = request.getParameter("unid");
    Document tmpDoc = DB.getDocumentByUNID(prmUNID);
    if (tmpDoc != null) {
    tmpDoc.remove(true);
    } else {
    }
    }

     

     


    script is below

     $( document ).ready( function () {

    // Add Document
        $('#newDoc').click(function(event){
        var url = "xpFormPC.xsp";
        window.open(url,"_self");
        });


    // Setup Rest Service
    var loc = ( location.href );
    var url = loc.substring( 0, loc.lastIndexOf( "/" ) ) + "/xpRest.xsp/custom/";
    var searchInput = XSP.getElementById("#searchInput");
    var crudServiceBaseUrl = "xpRest1.xsp", 
    dataSource = new kendo.data.DataSource({
    autoSync : true,
    transport : {
    read : {
    url : url + "get?status=5",
    dataType : "json",

    type : "GET"

    },
    t
    destroy : {
    dataType : "json",
    url : "destroy?id="

    type : "DELETE"

    }

    },
    parameterMap: function(data, type) {
    if (type == "destroy") {
    return { models: kendo.stringify(data.models) }
    }
    },
    batch:true,
    pageSize : 15,
    scrollable : false,
    height : 600,
     
    schema : {
    model : {
    id : "unid",
    fields : {
    serialNumber : {
    type : "string",
    editable : false
    },
    statusDescription : {
    type : "string",
    editable : false
    },
    lastActionDate : {
    type : "date",
    editable : false
    },
    lastActionUser : {
    type : "string",
    editable : false
    },
    lastActionLocation : {
    type : "string",
    editable : false
    },
    assetTag : {
    type : "string",
    editable : false
    },
    model : {
    type : "string",
    editable : false
    },
    unid : {
    type : "string",
    nullable : false
    }
    }
    }
    }
    });

    // Grid
    grid = $("#grid").kendoGrid( {
          excel:{
              fileName: "PC Inventory All.xlsx",
              filterable:true,
              allPages:false
            },
    dataSource : dataSource,
    dataBound: onDataBound,
    columns : [
           //define template column with checkbox and attach click event handler
           {width: "30px", 
           template: "<input type='checkbox' class='checkbox' />" },
    {
            width : "150px",
    field : "serialNumber",
    title : "Serial Number",
    template : "<a href=xpFormPC.xsp?action=openDocument?&key=#=unid#><h5><b>#=serialNumber#</b></h5></a>"
    }, {
    width : "200px",
    field : "statusDescription",
    title : "Status"
    }, {
    width : "250px",
    field : "lastActionDate",
    title : "Being Decomissioned Date",
    template: "#= (lastActionDate == null) ? ' ' : kendo.toString(kendo.parseDate(lastActionDate, 'yyyy-mm-dd'), 'MM/dd/yyyy') #"
    }, {
    width : "250px",
    field : "lastActionUser",
    title : "Being Decomissioned User"
    }, {
    width : "275px",
    field : "lastActionLocation",
    title : "Being Decomissioned Location"
    }, {
      width : "150px",
    field : "assetTag",
    title : "Asset Tag"
    }, {
       //width : "150px",
    field : "model",
    title : "Model"
    }, {
    hidden: false,
    width : "50px",
    template : "<button type='button' class='btn btn-danger k-grid-delete'>X</button>"
    }
    ],
           editable: "inline",
    pageable : {
    refresh : true,
    pageSizes : true,
    buttonCount : 5
    },
    groupable : true,
    reorderable : true,
    filterable : true,
    selectable : true,
    sortable : true,
    resizable : true,
    columnMenu : true
    });


    // Search
    $( "#searchInput" ).keyup( function () {

    var selecteditem = $( "#searchInput" ).val();
    var kgrid = $( "#grid" ).data( "kendoGrid" );
    selecteditem = selecteditem.toUpperCase();
    var selectedArray = selecteditem.split( " " );
    if ( selecteditem ) {
    var orfilter = {
    logic : "or",
    filters : []
    };
    var andfilter = {
    logic : "and",
    filters : []
    };
    $.each( selectedArray, function ( i, v ) {
    if ( v.trim() == "" ) {
    } else {
    $.each( selectedArray, function ( i, v1 ) {
    if ( v1.trim() == "" ) {
    } else {
    orfilter.filters.push( {
    field : "serialNumber",
    operator : "contains",
    value : v1
    }, {
    field : "status",
    operator : "contains",
    value : v1
    }, {
    field : "curLocation",
    operator : "contains",
    value : v1
    }, {
    field : "model",
    operator : "contains",
    value : v1
    }, {
    field : "assetTag",
    operator : "contains",
    value : v1
    } );
    andfilter.filters.push( orfilter );
    orfilter = {
    logic : "or",
    filters : []
    };
    }

    } );
    }
    } );
    kgrid.dataSource.filter( andfilter );
    } else {
    kgrid.dataSource.filter( {} );
    }
    });

        var checkedIds = {};

        // On click of the checkbox:
        function selectRow() {
            var checked = this.checked,
            row = $(this).closest("tr"),
            grid = $("#grid").data("kendoGrid"),
            dataItem = grid.dataItem(row);

            checkedIds[dataItem.id] = checked;
            if (checked) {
                //-select the row
                row.addClass("k-state-selected");
                } else {
                //-remove selection
                row.removeClass("k-state-selected");
            }
        }


        //on dataBound event restore previous selected rows:
        function onDataBound(e) {
            var view = this.dataSource.view();
            for(var i = 0; i < view.length;i++){
                if(checkedIds[view[i].id]){
                    this.tbody.find("tr[data-uid='" + view[i].uid + "']")
                    .addClass("k-state-selected")
                    .find(".checkbox")
                    .attr("checked","checked");
                }
            }}
            
            });

  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 04 Nov Link to this post

    Hello Bryan,

     

    There is a specific about your case since the batch option of the Kendo UI DataSource is enabled. Together with the functionality in the parameterMap function an array of models is sent to the server. I guess you might need to parse the request and extract the array of model first and then access each id value. 

     

    Regards,
    Boyan Dimitrov
    Telerik by Progress
     
    Build rich, delightful, *native* Angular 2 apps with Kendo UI for Angular 2. Try it out today! Kendo UI for Angular 2 (currently in beta) is a jQuery-free toolset, written in TypeScript, designed from the ground up to offer true, native Angular 2 components.
     
  3. Kendo UI is VS 2017 Ready
  4. Bryan
    Bryan avatar
    13 posts
    Member since:
    Feb 2016

    Posted 04 Nov in reply to Boyan Dimitrov Link to this post

    Boyan,

    I understand what you are saying, but I am not committed to any other settings - I can turn batch to false. What I want to know is the easiest way to get the id to the rest service.

    When I turned batch to false then I am getting a param map, but as you said it is buried in an array. Would prefer to just send the ID. 

    Also, the destroy action seems to run in an odd exponential fashion. The first time it runs once, all good. The second time it runs twice with the first param attached (previously deleted doc) and new param for the to be deleted doc. Third time it runs 3 times, etc. I do not want it to do that obviously.

  5. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 08 Nov Link to this post

    Hello Bryan,

    Same question is discussed in your other thread. I would suggest to continue the communication in your support ticket in order to avoid any misunderstandings. 

    The DataSource expects the removed/deleted item to be returned from the server. This is required in order to update its internal state. 

    Regards,
    Boyan Dimitrov
    Telerik by Progress
     
    Build rich, delightful, *native* Angular 2 apps with Kendo UI for Angular 2. Try it out today! Kendo UI for Angular 2 (currently in beta) is a jQuery-free toolset, written in TypeScript, designed from the ground up to offer true, native Angular 2 components.
     
Back to Top
Kendo UI is VS 2017 Ready