2017-08-04 98 views
2

My plunker如何排序依据数

在基于在排序依据本plunker NG-重复它工作正常,但是当ROWID超过父+按钮9号,其rowid变得10

父行rowid 10显示为1的子项。

在这里我分开父母和子女与-。 如果rowid是小孩,那么我在儿童记录之前添加-。 最后我想实现的是我想要显示10作为父母,1-10作为孩子。

var newRow = { 
    "rowId": "1", 
    "componentIdentification": "", 
    "componentName": "", 
    "codigo": "", 
    "componentType": "", 
    "componentState": "", 
    "actionId": "", 
    "actionPerform": "" 
    } 
    $scope.componentList = []; 
    $scope.componentList.push(angular.copy(newRow)); 

    $scope.addParentRow = function(rowId) { 
    var newGridRow = angular.copy(newRow); 
    var lastChar = getListOfSameLevel(rowId, true); //isParentRow 
    var parentId = rowId.length > 1 ? rowId.slice(0, rowId.length - 1) : ""; 
    newGridRow.rowId = parentId + getNextChar(lastChar); 
    $scope.componentList.push(newGridRow); 
    } 

    $scope.addChildRow = function(rowId) { 
    var newGridRow = angular.copy(newRow); 
    var lastChar = getListOfSameLevel(rowId, false); 
    if (rowId.length === lastChar.length) { 
     newGridRow.rowId = rowId + "-1"; 
    } else { 
     var parentId = lastChar.length > 1 ? lastChar.slice(0, lastChar.length - 1) : ""; 
     newGridRow.rowId = parentId + getNextChar(getLastChar(lastChar)); 
    } 
    $scope.componentList.push(newGridRow); 
    }; 

    var getNextChar = function(inputChar) { 
    return parseFloat(inputChar) + 1; 
    }; 

    var getLastChar = function(fullStr) { 
    return fullStr.slice(-1); 
    }; 

    var getListOfSameLevel = function(rowId, isParentRow) { 
    var compIdLength = rowId.length; 
    var matchedArray = []; 
    var sortedMatchedArray = []; 
    var latestCompId = ""; 

    if (compIdLength > 1) { 
     var parentId = isParentRow ? rowId.slice(0, rowId.length - 1) : rowId; 
     if (!isParentRow) { 
     matchedArray = _.filter($scope.componentList, function(row) { 
      return ((row.rowId.length >= compIdLength) && row.rowId.startsWith(parentId)); 
     }); 
     } else { 
     matchedArray = _.filter($scope.componentList, function(row) { 
      return ((row.rowId.length === compIdLength) && row.rowId.startsWith(parentId)); 
     }); 
     } 

     sortedMatchedArray = matchedArray.sort(); 
     latestCompId = sortedMatchedArray[sortedMatchedArray.length - 1].rowId; 
     return isParentRow ? getLastChar(latestCompId) : latestCompId; 
    } else { 
     matchedArray = _.filter($scope.componentList, function(row) { 
     return (row.rowId.length === compIdLength || (!isParentRow && row.rowId.startsWith(rowId))); 
     }); 
     sortedMatchedArray = matchedArray.sort(); 
     latestCompId = sortedMatchedArray[sortedMatchedArray.length - 1].rowId; 
     return latestCompId; 
    } 
    }; 
+1

再次相同的代码! – anu

+1

代码是相同的但问题不同 – Murali

+0

@Murali,这是因为你的id是一个字符串,而不是一个数字 – devqon

回答

0

该问题是由于您将rowID作为字符串进行排序而不是rowID作为数字进行排序的结果。

我已经添加了一个函数来返回rowID的整数值,然后对其进行排序。它将第10行放在列表中的正确位置(在9之后,而不是在1之后),但是在10处停止递增,所以你还有其他问题需要弄清楚。

基本上你添加这个功能:

$scope.rowIdAsNumber = function() { 
    return parseInt(rowId.to); 
    }; 

,然后改变你的重复来排序依据该功能

<tr data-node="treetable-1" id="basetr10" name="0" ng-repeat="rowData in componentList | orderBy:'rowIdAsNumber'"> 

我叉你plnkr这里:

https://plnkr.co/edit/Q4aQTU5CzVJ7EPUuEY4s?p=preview

+0

一个小小的改变:''rowIdAsNumber''不应该用引号 – devqon

+0

检查plnkr - 它不没有报价的工作。 –

+0

它应该作为父项来添加,但它是作为一个子项添加的,如果我添加了最后添加的其他子项 – Murali