如何逐行增加数字

时间:2017-08-02 12:26:34

标签: javascript angularjs

Plunker.

我已经实现了上面的plunker,在那个plunker中,rowId正在增加Alphabets。如下所示

该组件中将有+++以及 - 按钮。 如果我按+,那么The Rowid将从Starting Alphabet(A)开始。 如果我按下++那么父母rowid +从Starting Alphabet开始就意味着(AA)。 再次,如果我按++,那么从开始字母开始的父rowId +意味着(AAA)。像这样会增加。

请访问Plunker here.

但我希望用Numbers表示实现该功能

对于单一加号,它应该显示1,对于++,它应该显示111这样我需要实现的目标。

你能帮我做一下这个功能。

var newRow = {
    "rowId": "A"
  }
  $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 + "A";
    } 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 String.fromCharCode(inputChar.charCodeAt(0) + 1);
  };

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

1 个答案:

答案 0 :(得分:2)

您需要将 newRow.rowId 的初始值从A更改为1,并将下一个数字的初始值从A更改为{ {strong> addChildRow 函数中的{1}},如下所示:

1

Here是您的数字

的掠夺者