How to orderby number

Viewed 198

My plunker

In this plunker based on orderby in ng-repeat it is working fine but when the rowId exceeds 9 number in parent + button, the rowid becomes 10.

The parent rowid 10 is displaying as a child of 1.

Here I am separating parent and child with -. If the rowid is child then I am adding - before child record. Finally what I want to achieve is I want to display 10 as parent, 1-10 as child.

    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 Answers
Related