如何使用Numeric增加Column值

时间:2017-07-25 14:57:40

标签: javascript angularjs angular-components

我用Alphabets实现了下面的组件rowid,但是我需要用numberic来改变它。在我实现的组件中有一个名为RowId的列,当我们点击+按钮时,此rowid将增加,如果我按下++父rowId +以启动Alphabet开始。以下是我实施的组件。MyComponent.

我需要用numberic实现那个rowid,因为字母表在A到Z之间工作,在Z rowid像特殊字符之后一样。所以我需要用numeric实现这个功能。所以我将克服这个问题。

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);
  };

  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;
    }
  };

  $scope.getWidth = function(rowId) {
    var componentIdLength = rowId && rowId.length ? rowId.length : 0;
    var widthValue = (120 - (componentIdLength * 10));
    widthValue = widthValue ? widthValue : 10;
    return {
      'width': widthValue + 'px'
    };
  }
  $scope.removeRow = function(rowId) {
    if ($scope.componentList && $scope.componentList.length > 1) {
      var newArray = _.filter($scope.componentList, function(arrayItem) {
        return rowId !== arrayItem.rowId;
      });
      $scope.componentList = newArray;
    }
  }

你能帮我做一下吗?

0 个答案:

没有答案