【问题标题】:angular 1 : populate table with ng-repeat using a 2-d array from server's json角度 1:使用服务器 json 中的二维数组使用 ng-repeat 填充表
【发布时间】:2017-01-21 07:37:36
【问题描述】:

我不熟悉角度。但是我的项目的前端开发人员坚持认为他想要这样的 json:

{
"data": [{
    "area1": {
        "rows": [{
            "the_desc": "A value 1",
            "value": "sample value 1"
        }, {
            "the_desc": "A value 2",
            "value": "sample value 2"
        }, {
            "the_desc": "A value 3",
            "value": "other 3"
        }, {
            "the_desc": "A value 4",
            "value": "other 4"
        }, {
            "the_desc": "A value 5",
            "value": "other goats fly"
        }, {
            "the_desc": "A value 6",
            "value": "bla blah"
        }, {
            "the_desc": "A value 7",
            "value": "other 7"
        }, {
            "the_desc": "A value 8",
            "value": "other 8"
        }]
    }
}, {
    "area2": {
        "rows": [{
            "the_desc": "A value 9",
            "value": "sample value 9"
        }, {
            "the_desc": "A value 10",
            "value": "sample value 10"
        }, {
            "the_desc": "A value 11",
            "value": "other 11"
        }, {
            "the_desc": "A value 12",
            "value": "other 12"
        }, {
            "the_desc": "A value 13",
            "value": "other goats fly 13"
        }, {
            "the_desc": "A value 14",
            "value": "bla blah"
        }, {
            "the_desc": "A value 15",
            "value": "other 15"
        }, {
            "the_desc": "A value 16",
            "value": "other 16"
        }]
    }
}]
}

我认为我们应该去掉像“the_desc”和“value”这样的重复字符串,并使用:

{
"data": [{
    "area1": [
        ["1", "2"],
        ["3", "4"]
    ]
}, {
    "area2": [
        ["21", "22", "a5"],
        ["23", "24", "b6"]
    ]
}]
}

但他坚持认为 NG-repeat 将无法获取内部数组(它们是已知的列数据行。可能是 [][] 表数据。 问题:在角度 1 中是否存在类似 gettig 数据的问题?没有嵌套的 NG 重复。如果我说每个区域的列都是固定的,会有什么不同吗?有没有办法遍历行,并通过索引访问列?在我们的例子中,每个区域的列数是已知的(页面上的表格)。

原因:来自服务器的负载减少。更快的网络数据传输。

在两个答案的帮助下得到它,在 https://plnkr.co/edit/DrsXTP4kD0CnyAQwuoSu?p=preview 和可爱的角度错误报告上进行试验:https://docs.angularjs.org/error/ngRepeat/dupes?p0=ele%20in%20data.data%5B1%5D.area2%5B0%5D&p1=string:col%203%20A&p2=col%203%20A

解决方案要点:

在js控制器中:

$scope.data =       {
"data": [{"area1":
     [[ "A value 1",...

还有 HTML:

<div class= "" ng-repeat="ele in data.data[1].area2 track by $index">
  <span class='d2'> {{(ele[0])}} </span><span class='d2'>  {{(ele[1])}}...

【问题讨论】:

    标签: javascript angularjs json multidimensional-array


    【解决方案1】:

    作为前端开发人员,我也会选择第一种结构。在任何情况下,您都必须像这样使用嵌套的 ng-repeat:

    <div class="area" ng-repeat="area in data">
        <div class="row" ng-repeat="row in area.rows">
            <p class="description">{{::row.the_desc}}</p>
            <p class="value">{{::row.value}}</p>
        </div>
    </div>
    

    对于第二个sn-p:

    <div class="area" ng-repeat="area in data">
        <div class="row" ng-repeat="(key, value) in area.rows">
            <p class="description">{{::key}}</p>
            <p class="value">{{::value}}</p>
        </div>
    </div>
    

    对于第三个片段,只要数组结构保持不变并且只有两个值,我们就可以通过索引访问它们。

    <div class="area" ng-repeat="(key,area) in data">
        <!-- Prints area1, area2 if necessary -->
        <p>{{key}}</p>
        <!-- Prints 1st and 2nd value of the area array -->  
        <div class="row" ng-repeat="row in area">               
            <p class="description">{{row[0}}</p>
            <p class="value">{{row[1]}}</p>
        </div>
    </div>
    

    是否可以在没有嵌套 ng-repeat 的情况下实现这一目标?不见得。嵌套数组太多。

    但老实说,我不认为在对象键中存储“值”是一种好习惯(即使可以使用 Angular 显示),所以我建议您遵循开发人员的建议并使用第一个结构.

    【讨论】:

    • 如果我说每个区域的列都是固定的,会有什么不同吗?有没有办法遍历行,并通过索引访问列?
    • 是的,我们可以遍历行并显示值 {{row[0]}} 和 {{row[1]}}。从技术上讲,这 3 种结构都可以工作。
    • 您是否有机会将此添加到您的答案中,然后我可以正确标记它?我也在问题中添加了其他信息
    • 更新了,给你。
    • 您的回答和@rani-radcliff 链接有所帮助。加上角度的错误消息,它给了我需要处理重复的最后信息,所以:
      {{(ele[0 ])}}     {{(ele[1])}} ...
    【解决方案2】:

    不,一点也不,ng-repeats 可以嵌套。即使在原版 Javascript 中,您也可以将这些数据转换为您想要的任何格式,以便让 ng-repeat 根据需要显示它。您提出的最后两个选项实际上会使这变得更加困难。

    对于第一个配置,假设已经是JSON.parse()ed:

    <section ng-repeat="area in data">
      <div ng-repeat="row in area.rows">
        <p>{{row.the_desc}} is {{row.value}}</p>
      </div>
    </section>
    

    这将遍历 data 数组,并为 data 中的每个 area 创建另一个 ng-repeat 用于 area#.rows 数组以遍历行。

    甚至可以使用ng-repeat 来迭代对象中的键,这需要通过其他两种解决方案来完成。

    【讨论】:

      【解决方案3】:

      我相信你需要稍微改变你的数据结构。像这样设置应该会有所帮助:

      $scope.data = [{
          name: "area1",
          rows : [{
                    "the_desc": "A value 1",
                    "value": "sample value 1"
                }, {
                    "the_desc": "A value 2",
                    "value": "sample value 2"
                }, {
                    "the_desc": "A value 3",
                    "value": "other 3"
                }, {
                    "the_desc": "A value 4",
                    "value": "other 4"
                }, {
                    "the_desc": "A value 5",
                    "value": "other goats fly"
                }, {
                    "the_desc": "A value 6",
                    "value": "bla blah"
                }, {
                    "the_desc": "A value 7",
                    "value": "other 7"
                }, {
                    "the_desc": "A value 8",
                    "value": "other 8"
                }]
      
      }, 
      {
         name: "area2",
          "rows": [{
              "the_desc": "A value 9",
              "value": "sample value 9"
          }, {
              "the_desc": "A value 10",
              "value": "sample value 10"
          }, {
              "the_desc": "A value 11",
              "value": "other 11"
          }, {
              "the_desc": "A value 12",
              "value": "other 12"
          }, {
              "the_desc": "A value 13",
              "value": "other goats fly 13"
          }, {
              "the_desc": "A value 14",
              "value": "bla blah"
          }, {
              "the_desc": "A value 15",
              "value": "other 15"
          }, {
              "the_desc": "A value 16",
              "value": "other 16"
          }]
      
      }
      

      那么使用ng-repeat就可以很方便地访问了,像这样:

            <div ng-repeat="d in data">
          <h1>{{d.name}}</h1>
          <div ng-repeat="r in d.rows">
            {{r.the_desc}}
          </div>
        </div>
      

      这是Plunker。我不确定您是如何获取数据的,或者它是否是硬编码的,但这至少可以帮助您入门。

      【讨论】:

      • 在我的例子中是硬编码的。它来自后端 Web 服务,但可以按照我们想要的方式进行操作。我们甚至可以以一种方式将它发送到 javascript,然后在它进入屏幕之前将其转换为角度(只是一个想法),这不应该超过几毫秒,因为它每个屏幕最多 100 行数据。我只是想知道如果 cols 是固定的,我们可以通过索引访问它们吗?
      • 您通常可以按索引访问列,请参阅此 Plunker plnkr.co/edit/cSuLAvUY5PYUUr7YvNWF?p=preview,但我无法弄清楚如何让它按照您的数据设置方式工作。如果我有任何想法,我会更新我的答案。
      • 我猜第一个表的 ngvalue 将是 {{rows[0]}}
      • 我希望我能再次投票给你。 Plunked 帮了忙。解决了plnkr.co/edit/DrsXTP4kD0CnyAQwuoSu?p=preview
      • 爱角度错误报告:docs.angularjs.org/error/ngRepeat/…。您的链接帮助我找到了解决方案。也许您可以阅读此内容并编辑您的答案?
      猜你喜欢
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多