简体   繁体   English

如何使用javascript从数组中删除特定行?

[英]How to delete a particular row from array using javascript?

I have a problem on deleting a table row data by using JavaScript splice() method. 我在使用JavaScript splice()方法删除表行数据时遇到问题。 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.StudentDetails是我的数组,包含所有数据,如id,name,dept,age,address。,

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

在此输入图像描述在此输入图像描述

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!!! 提前致谢!!!

I suggest wiring up your delete something like this (this is assuming you are using an ng-repeat for the students): 我建议你删除这样的删除(假设你正在为学生使用ng-repeat):

<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. 不像您期望的那样工作,它不会查看对象的对象数组。

It makes no sense to delete the entry in your scope, since it's coming from a ajax source. 删除范围中的条目是没有意义的,因为它来自ajax源。 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. 它可能不再出现在您的范围内,但下次打开页面时, $scope.Load()将重新加载数据源。 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. 您需要编写一个能够从您的db / json文件或wherever_your_data_is中删除学生的ajax处理程序。 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: 并从你的HTML调用它像这样:

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

Angular will automaticly update your scope an the dom Angular会自动更新你的范围和dom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM