0

I have a problem on deleting a table row data by using JavaScript splice() method. But i cant delete the data in the meanwhile delete the first and last row of the array.

$scope.StudentDetails is my array which contains all the data like id,name,dept,age,address.,

$scope.deleteData = function (item)
{
    var index = $scope.StudentDetails.indexOf(item);
    if (index > -1)
    {
        $scope.StudentDetails.splice(index, 1);
    }
};

enter image description here enter image description here

in the first image i will click delete .then it show below image ..but not delete the exact data. i am using above coding.

using below code load my data and push to the array and populate the date in the table elements.

$scope.Load = function ()
{
    $scope.StudentDetails = [];
    $http({ method: 'GET', url: '/Home/GetStudentDetails' }).success(function (data)
    {
        if (data != null)
        {
            $.each(data.Data, function (index, value)
            {
                $scope.StudentDetails.push(value);
            });

            // $scope.checked = false;
        }
    }).error(function ()
    {
        alert("Failed");
    });
}

Please help anybody!!! Thanks In advance!!!

9
  • Can you post more code (including the deletion of first/last entry)? Commented Feb 5, 2014 at 12:30
  • what does item holds? Commented Feb 5, 2014 at 12:31
  • the item ,which holds the row id ... Commented Feb 5, 2014 at 12:33
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Feb 5, 2014 at 12:34
  • Where does your "exact data" come from? Hardcoded or from an ajax call? In the last casse you should obviously also delete it from your db/json/xml ore other source. In the first case you need to rewrite your code very fast:-) Commented Feb 5, 2014 at 12:48

2 Answers 2

1

It makes no sense to delete the entry in your scope, since it's coming from a ajax source. It might not be present in your scope anymore, but the next time you open the page $scope.Load() will just reload the data source. You need to write an ajax handler that is capable to delete a student from your, db/json file or wherever_your_data_is stored. Write your delete function like this:

$scope.deleteData = function (item){
    $http({ method: 'GET', url: '/Home/DeleteStudent'/'+ $routeParams.item }).success(function (data)
    {
       //After succesfull deletion reload the changed scope
        $scope.Load();
    });
};

and call it from your html like this:

<td><a ng-click="deleteData(your_id_for_this_item)">delete</a> </td>

Angular will automaticly update your scope an the dom

Sign up to request clarification or add additional context in comments.

2 Comments

sir this is ok sir ..but if there any way to delete the row without using another call
No, I don't think so. At some point you have to tell you serversided data that is has changed. You could delay that and add a 'sync now' button that synchronises the datasource with the current scope of your app. But thats not the intention of ajax. In my projects every single keystroke in an Input field causes an update of the data source. There is no submit or save button anymore. Anyhow, for large datafields I do a scope reload only if the field is blurred. But changecalls to the data source happen while typing.
1

I suggest wiring up your delete something like this (this is assuming you are using an ng-repeat for the students):

<a data-ng-click="deleteStudent($index)">delete</a>

And then in your controller

$scope.deleteStudent = funciton(index){
    $scope.studentDetails.splice(index, 1);
};

The real problem with your question is that

var index = $scope.StudentDetails.indexOf(item);

Does not work like you are expecting, it doesn't look through an array of objects for an object.

1 Comment

This is the solution I also use, but I considered that it indirectly alter the DOM (changing the scope causes ng-repeat to modify the DOM), this is why I put the delete function into a directive, without forgetting to wrap with $scope.$apply(). NB: there are some typos mistakes in your answer (that I can't edit because there are less than 6 characters) ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.