【问题标题】:AngularJS ng-repeat data record creation with Collapse-Expand areaAngularJS ng-repeat 使用 Collapse-Expand 区域创建数据记录
【发布时间】:2020-10-20 04:16:53
【问题描述】:

我正在尝试使用 ng-repeat 重复创建记录,并且记录的数据部分需要可折叠。我尝试了可折叠并且工作正常(我已在页面顶部添加脚本)但可折叠部分不适用于重复记录。

谁能告诉我我错过了什么?

<!DOCTYPE>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
</head>
<style>
.collapsible {
    background-color: #777;
    color: white;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
}

.active, .collapsible:hover {
    background-color: #555;
}

.collapsible:after {
    content: 'Edit';
    color: white;
    font-weight: bold;
    float: right;
    margin-left: 5px;
}

.active:after {
    content: "Done Editting";
}

.content {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
    background-color: #f1f1f1;
}
</style>
<body>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope, $window) {
    $scope.Customers = [


                ];
 
    $scope.Add = function () {

        var customer = {};
        customer.Type = $scope.AccType;
        customer.Name = $scope.Name;
        customer.Country = $scope.Country;
        $scope.Customers.push(customer);
 
        $scope.Type = "";
        $scope.Name = "";
        $scope.Country = "";
            };
 
        $scope.Remove = function (index) {
        var name = $scope.Customers[index].Name;
        if ($window.confirm("Do you want to delete " + name)) {

                    $scope.Customers.splice(index, 1);
                }
            }
        });
    </script>
    <script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.maxHeight){
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    } 
    });
}
</script>
    
    <button type="button" class="collapsible">This is working</button>
    <div class="content">
        <p>Enter your account information:</p>
        <tr>
            <label for="ftype">Acc. Type:</label>
            <td><input type="text" value=""></td>
            <td><label for="ftype">Name:</label>
            <input type="text" value=""></td>
            <td><label for="ftype">Country:</label>
            <input type="text" value=""></td>
            <td><input type="button" value="Delete"></td><br><br>
        </tr>
    </div>
    <br>

    <div ng-app="MyApp" ng-controller="MyController">
        <table cellpadding="0" cellspacing="10">
            <tbody ng-repeat="m in Customers">
                <tr>
                    <td>
                        <button class="collapsible">{{m.Name}}</button>
                        <div class="content">
                            <p>Notes</p>
                        </div>
                    </td>
                    <td><input type="text" value="{{m.AccType}}" /></td>
                    <td><input type="text" value="{{m.Name}}" /></td>
                    <td><input type="text" value="{{m.Country}}" /></td>
                    <td><input type="button" ng-click="Remove($index)" value="Delete" /></td>
                </tr>
                
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <form>
                            <label for="type">Account Type:</label><br>
                            <input type="text" ng-model="Type" id="type" name="type" value=""><br>
                            <label for="name">Name:</label><br>
                            <input type="text" ng-model="Name" id="name" name="name" value=""><br>
                            <label for="country">Country:</label><br>
                            <input type="text" ng-model="Country" id="country" name="country" value="">
                            <input type="button" ng-click="Add()" value="Add" />
                        </form>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    <script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.maxHeight){
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    } 
  });
}
</script>
</body>
</html>

【问题讨论】:

标签: javascript html css angularjs angularjs-ng-repeat


【解决方案1】:

您可以通过非常简单的步骤做到这一点:

  1. 添加您添加点击事件的ng-click="isopen = !isopen"
  2. ng-if ="isopen" 添加到要折叠的行中。

假设您有如下所示的行,并且在单击第一个 tr 标记时,您必须打开第二个 trrow。所以就像下面那样做。

<table>
   <tr ng-click="isopen = !isopen"></tr>
   <tr ng-if ="isopen"></tr>
</table>

【讨论】:

  • 感谢 Sunny 的回答!
  • 作为初学者,我不知道如何根据您的回答来修改脚本。这是我尝试过的;
  • `

    备注

    `
  • @user13837193 我编辑了我的答案。实施并检查它是否工作
  • 感谢您的帮助。我不确定如何实现对原始代码的更改。 @Sunny,您能否将其更新为我上面显示的代码?
猜你喜欢
  • 1970-01-01
  • 2015-02-23
  • 2014-10-12
  • 2018-01-26
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 2018-06-28
  • 2012-08-09
相关资源
最近更新 更多