我有一张包含以下细节的表格。
Row_no | Contact Person | Address | 26 | Andarw | DEL | 25 | Celret | DRT | 24 | Driok | ddd | 23 | Andarw | DEL | 22 | Celret | DRT | 2 | Driok | ddd | 3 | Andarw | DEL | 4 | Celret | DRT | 5 | Driok | ddd |Row_no是唯一的。 我保持它是唯一的,以便在删除行时,我会知道我想删除哪些行。 但问题如下。
如果我删除Row_no 25,24,23,22的行 - 它正在删除这些行,但它也删除2,3,4,5。 它表现得很奇怪。
这是我用的代码......
function remove(names) { currentrows = currentrows.filter(function (obj) { return names.indexOf(obj.row_no) == -1; }); } $scope.deleteuser = function () { //selectedRowsString is a string. its value is '25,24,23,22' remove(selectedRowsString); $scope.gridOptions.rowData = currentrows; //updates new rowdata to grid table $scope.gridOptions.api.setRowData($scope.gridOptions.rowData);//updates new rowdata to grid table selectedRows = "";//this is to reset for next deletion selectedRowsString = ""; //this is to reset for next deletion };我相信函数remove(names)中的indexOf导致问题。 在删除row_no 22,23,24,25时,它也删除了2,3,4,5,因为22,23,24,25在索引中有这些值。
我没有得到如何更正此错误。 有人可以请帮助。
I have a table with following details.
Row_no | Contact Person | Address | 26 | Andarw | DEL | 25 | Celret | DRT | 24 | Driok | ddd | 23 | Andarw | DEL | 22 | Celret | DRT | 2 | Driok | ddd | 3 | Andarw | DEL | 4 | Celret | DRT | 5 | Driok | ddd |Row_no is unique. I have kept it as unique so that while deleting rows, i would come to know which rows i want to delete. But the problem is as follows.
If i delete rows with Row_no 25,24,23,22 - it is deleting these rows but it is also deleting 2,3,4,5. It is behaving weird.
Here is the code i use...
function remove(names) { currentrows = currentrows.filter(function (obj) { return names.indexOf(obj.row_no) == -1; }); } $scope.deleteuser = function () { //selectedRowsString is a string. its value is '25,24,23,22' remove(selectedRowsString); $scope.gridOptions.rowData = currentrows; //updates new rowdata to grid table $scope.gridOptions.api.setRowData($scope.gridOptions.rowData);//updates new rowdata to grid table selectedRows = "";//this is to reset for next deletion selectedRowsString = ""; //this is to reset for next deletion };I believe the indexOf in function remove(names) is causing the problem. While deleting row_no 22,23,24,25-- it is also deleting 2,3,4,5 because 22,23,24,25 has those values in the index.
I am not getting exactly how to correct this error. Can someone please help.
最满意答案
我在这里为你准备了一个小提琴: https : //jsfiddle.net/czeee3dd/
首先,你对你的问题是正确的。 当您在字符串上执行indexOf时,将拾取字符串中任何出现的row_no 。
一个简单的解决方法是首先将您的名称字符串转换为字符串数组。
names = names.split(',');然后你可以进行当前的比较,但是你必须确保在比较之前将每个row_no值转换为字符串。
return names.indexOf(item.row_no.toString()) == -1;这里有完整的小提琴代码:
var data = [ {row_no: 26, name: 'Andarw'}, {row_no: 21, name: 'another'}, {row_no: 2, name: 'thid'}, {row_no: 4, name: 'hagrid'} ]; function doFilter(names){ names = names.split(','); var filtered = data.filter(function(item){ return names.indexOf(item.row_no.toString()) == -1; }); return filtered; } var output = doFilter('26,21'); console.log(output);I have put a fiddle together for you here: https://jsfiddle.net/czeee3dd/
Firstly, you are correct about your problem. As you are doing an indexOf on a string, any occurrence of the row_no in the string is going to be picked up.
An easy fix is to firstly convert your names string into an array of strings.
names = names.split(',');Then you can do the comparison you currently have, but you have to make sure you convert each row_no value to a string before comparing.
return names.indexOf(item.row_no.toString()) == -1;Full fiddle code here:
var data = [ {row_no: 26, name: 'Andarw'}, {row_no: 21, name: 'another'}, {row_no: 2, name: 'thid'}, {row_no: 4, name: 'hagrid'} ]; function doFilter(names){ names = names.split(','); var filtered = data.filter(function(item){ return names.indexOf(item.row_no.toString()) == -1; }); return filtered; } var output = doFilter('26,21'); console.log(output);更多推荐
发布评论