2016-09-18 2 views
0

私はクリックするとテーブルがあります<td>私はこのtdの名前と行のインデックスを返す必要があります。しかし、私がこれをng-click="showIndex(field.name, $index)"のようにすると、tdのインデックスを返すのではなく、tr(それは私が知っている論理です:))。私は親インデックスをチェックするいくつかの方法を見つけましたが、それはjqueryです、おそらくそれを角度でチェックする方法がありますか? I have some exampletrインデックスjqueryなしのangularjsでtdをクリックして

答えて

1

あなたは

<tr ng-repeat='row in table.row'> 
    <td ng-click="showIndex(field.name, $parent.$index)" ng-repeat='field in row.fields'>{{field.name}}</td> 
</tr> 

var app = angular.module('App', []); 
 

 
app.controller('Ctrl', function($scope) { 
 
    $scope.table = { 
 
row: [{ 
 
    fields: [{ 
 
    title: "Your name", 
 
    name: "John", 
 
    id: 'name' 
 
    }, { 
 
    title: "Your second name", 
 
    name: "Doe", 
 
    id: 'secondName' 
 
    }, { 
 
    title: "Your age", 
 
    name: 26, 
 
    id: 'age' 
 
    }, { 
 
    title: "Your sex", 
 
    name: "Male", 
 
    id: 'sex' 
 
    }] 
 
}, { 
 
    fields: [{ 
 
    title: "Your name", 
 
    name: "Kevin", 
 
    id: 'name' 
 
    }, { 
 
    title: "Your second name", 
 
    name: "Parker", 
 
    id: 'secondName' 
 
    }, { 
 
    title: "Your age", 
 
    name: 32, 
 
    id: 'age' 
 
    }, { 
 
    title: "Your sex", 
 
    name: "Male", 
 
    id: 'sex' 
 
    }] 
 
}, { 
 
    fields: [{ 
 
    title: "Your name", 
 
    name: "Barbara", 
 
    id: 'name' 
 
    }, { 
 
    title: "Your second name", 
 
    name: "Streisand", 
 
    id: 'secondName' 
 
    }, { 
 
    title: "Your age", 
 
    name: 60, 
 
    id: 'age' 
 
    }, { 
 
    title: "Your sex", 
 
    name: "Female", 
 
    id: 'sex' 
 
    }] 
 
}, { 
 
    fields: [{ 
 
    title: "Your name", 
 
    name: "Linda", 
 
    id: 'name' 
 
    }, { 
 
    title: "Your second name", 
 
    name: "Lorry", 
 
    id: 'secondName' 
 
    }, { 
 
    title: "Your age", 
 
    name: 20, 
 
    id: 'age' 
 
    }, { 
 
    title: "Your sex", 
 
    name: "Female", 
 
    id: 'sex' 
 
    }] 
 
}] 
 
    }; 
 

 
    $scope.showIndex = function(name, index) { 
 
$scope.showResult = name + ' in the row number ' + index; 
 
    } 
 
});
<!DOCTYPE html> 
 
<html lang="en" ng-app="App"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <title>Document</title> 
 
\t <link rel="stylesheet" href="style.css"> 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
 
    <script src='script.js'></script> 
 
</head> 
 
<body ng-controller="Ctrl"> 
 

 
\t <table> 
 
\t \t <thead> 
 
\t \t \t <tr> 
 
\t \t \t \t <th ng-repeat='header in table.row[0].fields'>{{header.title}}</th> 
 
\t \t \t </tr> 
 
\t \t </thead> 
 

 
\t \t <tbody> 
 
\t \t \t <tr ng-repeat='row in table.row'> 
 
\t \t \t \t <td ng-click="showIndex(field.name, $parent.$index)" ng-repeat='field in row.fields'>{{field.name}}</td> 
 
\t \t \t </tr> 
 
\t \t </tbody> 
 
\t </table> 
 

 
    <br> 
 
    <b>{{showResult}}</b> 
 
\t </body> 
 
</html>

+0

$parent.$index代わりの$indexを使用する必要がありますはい、それは働きます!ありがとう – YoroDiallo

関連する問題