У меня есть таблица, которую я отобразил с помощью angularjs. Просто список элементов с идентификатором в каждой строке. Теперь я хочу видеть детали каждой строки в раскрывающемся списке, возможно, div с вложенной таблицей. (Этот подробный вид сделает асинхронный вызов на сервер, чтобы вытащить требуемые данные на основе идентификатора строки). Я, вероятно, могу это сделать, используя обычный bootstrap modal, но я хотел бы сделать это в раскрывающемся представлении на той же родительской странице. Все это возможно с помощью jquery, но я хотел бы реализовать его с помощью angularjs.
Следующее выполнит то, что вы ищете:
ng-click
для привязки события click к строке таблицы$http
чтобы поговорить с сервером и получить информацию о клике IDВот полный пример:
контроллер:
public class AngularController: Controller {public ActionResult Index() {return View(); } }
public ActionResult SomeActionMethod(int id)
{
//Write the code to get the specific details for the passed through id
var user = new {Name="User Name",Surname = "User surname" };
return Json(user,JsonRequestBehavior.AllowGet);
}
}
Посмотреть:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<style type="text/css">
.close {
color: red;
}
</style>
<script type="text/javascript">
$(function () {
$(document).on("click", "span.close", function () {
$(this).parent().parent().empty();
});
});
var app = angular.module('app', []);
app.controller('controller', function ($scope, $http) {
var current = "";
$scope.ids = [1, 2, 3, 4, 5];
$scope.RowClick = function (i) {
$http.get("/Angular/SomeActionMethod/" + i).success(function (data, status, headers, config) {
$('#users > tbody > tr').each(function () {
var tr = $(this);
var isPopup = $(tr).hasClass("popup");
if (!isPopup) {
var children = $(tr).children();
var id = children[0].innerText;
if (id == i && current != id) {
alert($(tr).html());
$("<tr class='popup'><td>" + data.Name + " " + data.Surname + " <span class='close'>X</span></td></tr>").insertAfter(tr);
current = id;
}
}
});
});
};
});
</script>
<div ng-app="app" ng-controller="controller">
<table id="users">
<thead>
<tr>
<th>
ID
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="i in ids">
<td ng-click="RowClick(i)">{{i}}</td>
</tr>
</tbody>
</table>
</div>
Выход: