Monday, October 6, 2014

SharePoint 2013: List operation using AngularJS Part 5

This post will explain you about how to delete list item. 
Add Below function to Service file

 this.DeleteListItem = function (data) {
        var config = {
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "DELETE",
                "If-Match": data.__metadata.etag
            }
        };
        return $http.post(data.__metadata.uri, null , config);
    };

Add Below code to Controller file.

 $scope.DeleteListItem = function () {
        ListService.GetListItemsById($scope.listName, $scope.itemId)
        .success(function (data) {
            var result = data.d.results[0];
            ListService.DeleteListItem(result)
            .success(function () {
                alert("item deleted");
                $scope.$broadcast("RefillData");
            })
            .error(function () {
                alert("delete failed");
            });
        })
        .error(function () {
            alert("error");
        });
    };

Update below code from HTML file

List Name : <input ng-model="listName" />ID: <input ng-model="itemId" ng-readonly="true" /> Title : <input ng-model="iTitle" /><input type="button" ng-click="AddListItem()" value="AddListItem" /><input type="button" ng-click="UpdateListItem()" value="UpdateListItem" /><input type="button" ng-click="DeleteListItem()" value="DeleteListItem" />


No comments:

Post a Comment