最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 2D array $Index in AngularJS - Stack Overflow

programmeradmin5浏览0评论

I'm working on an AngularJS app that builds a spreadsheet out of a 2D array built by the ng-repeat function. I'm currently writing a function that will change the initial values of the array when the user enters new values on the spreadsheet. This requires me to access the point in the initial array based on its row index and column index so I can change it to the new value.

I have looked over the ng-repeat API and found that it has an $index property that allows me to check the index of the current repeated value. However, I am finding that it will only let me check the index of the value of whatever repeat loop you are inside--no other outside loops that you may also be in.

<script>

    data = [
            [A1, B1, C1],
            [A2, B2, C2],
            [A3, B3, C3]
           ]

    sheet= function($scope, $parse){
    $scope.columns = [colA, colB, colC];
    $scope.rows = data.length;
    $scope.cells = {};
    $scope.outer = data.map(function(c,row){
        return c.map(function(data, ind){
            return {
                content: data,
                model: null,
            };
        });
    });
    };
</script>


<div ng-app ng-controller="sheet">
    <table>
        <tr class="column-label">
            <td ng-repeat="column in columns">{{column}}</td>
        <tr ng-repeat="inner in outer">
            <td class="row-label" ng-repeat="data in inner">
                <div>
                    <input type="text" ng-model="data.content" value="{{data.content}}">
                </div>
            </td>
        </tr>
    </table>
</div>

I want to access the $index of outer while within the inner loop, and also be able to access the $index of the inner, too. Is there a way to do this? I plan on passing these values as parameters to the function I'm writing.

I'm working on an AngularJS app that builds a spreadsheet out of a 2D array built by the ng-repeat function. I'm currently writing a function that will change the initial values of the array when the user enters new values on the spreadsheet. This requires me to access the point in the initial array based on its row index and column index so I can change it to the new value.

I have looked over the ng-repeat API and found that it has an $index property that allows me to check the index of the current repeated value. However, I am finding that it will only let me check the index of the value of whatever repeat loop you are inside--no other outside loops that you may also be in.

<script>

    data = [
            [A1, B1, C1],
            [A2, B2, C2],
            [A3, B3, C3]
           ]

    sheet= function($scope, $parse){
    $scope.columns = [colA, colB, colC];
    $scope.rows = data.length;
    $scope.cells = {};
    $scope.outer = data.map(function(c,row){
        return c.map(function(data, ind){
            return {
                content: data,
                model: null,
            };
        });
    });
    };
</script>


<div ng-app ng-controller="sheet">
    <table>
        <tr class="column-label">
            <td ng-repeat="column in columns">{{column}}</td>
        <tr ng-repeat="inner in outer">
            <td class="row-label" ng-repeat="data in inner">
                <div>
                    <input type="text" ng-model="data.content" value="{{data.content}}">
                </div>
            </td>
        </tr>
    </table>
</div>

I want to access the $index of outer while within the inner loop, and also be able to access the $index of the inner, too. Is there a way to do this? I plan on passing these values as parameters to the function I'm writing.

Share Improve this question asked Jul 9, 2013 at 16:06 user2465164user2465164 9374 gold badges16 silver badges29 bronze badges 3
  • Maybe there's a more straight-forward answer, but I would proabably just start checking out ng-repeat, perhaps you can just write you're own directive that specifically handles this case for you. A good question though. – shaunhusain Commented Jul 9, 2013 at 16:09
  • 2 $parent.$index or maybe $parent().$index - One of these two should work. – rGil Commented Jul 9, 2013 at 16:45
  • $parent.$index works. Thanks! – user2465164 Commented Jul 9, 2013 at 16:51
Add a ment  | 

2 Answers 2

Reset to default 13

Haha, well, it was answered in the ments while I was putting together my fiddle, but here's a demonstration of it for people finding this question down the road:

http://jsfiddle/5s6z1dfc/2/

<div ng-controller="MyCtrl">
  <table>
      <tr ng-repeat="row in data">
         <td ng-repeat="cell in row">
             {{cell}} ({{$index}},{{$parent.$index}})
          </td>
      </tr>
    </table>
</div>

Here is my code, maybe it will help you or someone who find the way to loop 2D array into two ng-repeat element. HTML code

<div ng-repeat="row in gameMatrix track by $index">
    <div id=cell_{{row.id}}>
    </div>
</div>

Javascript code:

$scope.gameData = [
    [1, 0, 0],
    [1, 0, 0],
    [1, 0, 0],
    [1, 0, 0]
];

$scope.presentCellsData = function() {
    var Str = "";
    var rows = [];
    var term = [];
    for (i = 0; i < $scope.gameData.length; i++) {
        Str = "";
        rows = "";
        for (j = 0; j < $scope.gameData[i].length; j++) {
            console.log("i j " + i + " " + j);
            console.log($scope.gameData[i][j]);
            Str = "<div style='display:inline-block'> " + $scope.gameData[i][j] + "&nbsp;</div>";
            rows += Str;
        }
        rows += "</br>";
        term.push({ "id": i, "data": rows });
    }
    console.log(term);
    return term;
}

$scope.gameMatrix = $scope.presentCellsData();
angular.element(document).ready(function() {
    for (i = 0; i < $scope.gameMatrix.length; i++) {
        console.log("$scope.gameMatrix[i]");
        console.log($scope.gameMatrix[i]);
        document.getElementById("cell_" + i).outerHTML = $scope.gameMatrix[i].data;
    }
});

@Karen: thanks you, I can using your code in that way with my data:

<div style="display: inline-block;" ng-repeat="row in gameData track by $index">
        <!-- <div id=cell_{{row.id}}>
            aaa
        </div> -->
        <div ng-repeat="cell in row track by $index">
            {{cell}}
        </div>
    </div>
发布评论

评论列表(0)

  1. 暂无评论