【问题标题】:Angularjs Table using JSON data使用 JSON 数据的 Angularjs 表
【发布时间】:2017-03-04 12:46:07
【问题描述】:

我的两个 JSON 数组是,

$scope.myHospital = [
  {"id":"1","name":"hos1"}, 
  {"id":"2","name":"hos2"},
  {"id":"3","name":"hos3"},
  {"id":"4","name":"hos4"},
  {"id":"5","name":"hos5"}
];

另一个是

    $scope.data = [
        {
      "category":"first category",
      "procedure":[{
        "name":"pro1",
        "hospital": [
          {"id":"1","price":"1101"},
          {"id":"2","price":"1102"},
          {"id":"3","price":"1103"},
          {"id":"4","price":"1104"},
          {"id":"5","price":"1105"}
        ]
      }, {
        "name":"pro2",
        "hospital": [
          {"id":"1","price":"1201"},
          {"id":"2","price":"1202"},
          {"id":"3","price":"1203"},
          {"id":"4","price":"1204"},
          {"id":"5","price":"1205"}
        ]
      }, {
        "name":"pro3",
        "hospital": [
          {"id":"1","price":"1301"},
          {"id":"3","price":"1303"},
          {"id":"5","price":"1305"}
        ]
      }]
    },
    {
      "category":"Second category",
      "procedure":[{
        "name":"pro4",
        "hospital": [
          {"id":"1","price":"2101"},
          {"id":"2","price":"2102"},
          {"id":"3","price":"2103"},
          {"id":"4","price":"2104"},
          {"id":"5","price":"2105"}
        ]
      }, {
        "name":"pro5",
        "hospital": [
          {"id":"1","price":"2201"},
          {"id":"2","price":"2202"},
          {"id":"4","price":"2204"}
        ]
      }]
    }               
   ];

通过这些,我想要一张这样的桌子

我通过 ng-repeat 尝试过,直到现在我设法在plnkr 中生成表格 现在我通过匹配医院 ID 坚持null 值的逻辑。在 JSON 中手动插入 null 值对我不起作用,因为我从后端获取此 JSON。但如果需要,我可以要求他们更改 JSON 结构(数组格式)。

【问题讨论】:

  • null 是什么意思?没有记录时你想显示 NULL 吗?
  • 是的,例如“pro3”对于医院 id 2 和 4 没有任何值。所以对于医院 2 和 4,我想显示 NULL。与我在问题中发布的图像相同。

标签: html angularjs json angularjs-ng-repeat


【解决方案1】:

您可以在收到 JSON 结构后更改它,以使其可在 UI 上呈现。像这样的,

angular.forEach($scope.data[0].procedure, function(pro) {
  var arr = [];
  angular.forEach(pro.hospital, function(hos) {
    arr[hos.id] = hos.price
  });
  pro.hospital = arr;
});

现在,具有较小值的医院数组如下所示。如果您注意到,我将结构从对象数组更改为字符串数组,其中包含表示医院编号的键的价格。

hospital: [
  2: "469",
  3: "429",
  5: "319"
]

最后,您可以使用如下渲染逻辑来接受此更改,

<tr ng-repeat="(index, hos) in myHospital">
  <td ng-bind="hos.name"></td>
  <td ng-repeat="pro in data[0].procedure">
    <span ng-bind="pro.hospital[index+1]"></span>
    <span ng-if="!pro.hospital[index+1]">NULL</span> <!--Optional-->
  </td>
</tr>

DEMO

【讨论】:

  • 感谢您的回答。我正在努力,一旦完成,我会通知你。我需要做一些改变,比如我的医院 id 是 mongo 生成的 24 个字符 id。但我认为 for-each 循环将有助于实现目标。再次感谢:)
【解决方案2】:

这是工作的code

我也对您的数据源进行了一些更改,这很容易考虑和制作,在 HTML 中也是如此。

在您的 plunker 中,当 $scope.data 中的 "hospital" 对象中存在少量记录时,您显示与医院相对应的错误值。为了纠正我也添加了空对象,因此 ng-repeat 将传递正确的索引显示对比 在JS中

$scope.data = [
        {
      "category":"first category",
      "procedure":[{
          "name":"pro1",
          "hospital": [
            {"id":"1","price":"1101"},
            {"id":"2","price":"1102"},
            {"id":"3","price":"1103"},
            {"id":"4","price":"1104"},
            {"id":"5","price":"1105"}
          ]
      }, {
        "name":"pro2",
        "hospital": [
          {"id":"1","price":"1201"},
          {"id":"2","price":"1202"},
          {"id":"3","price":"1203"},
          {"id":"4","price":"1204"},
          {"id":"5","price":"1205"}
        ]
      }, {
        "name":"pro3",
        "hospital": [
          {"id":"1","price":"1301"},
           {"id":"2","price":null}, // add null objects for considering right index in display...
          {"id":"3","price":"1303"},
           {"id":"4","price":null},
          {"id":"5","price":"1305"}
        ]
      }]
    },
    {
      "category":"Second category",
      "procedure":[{
          "name":"pro4",
          "hospital": [
            {"id":"1","price":"2101"},
            {"id":"2","price":"2102"},
            {"id":"3","price":"2103"},
            {"id":"4","price":"2104"},
            {"id":"5","price":"2105"}
          ]
      }, {
        "name":"pro5",
        "hospital": [
          {"id":"1","price":"2201"},
          {"id":"2","price":"2202"},
          {"id":"3","price":null},
          {"id":"4","price":"2204"},
          {"id":"5","price":null}
        ]
      }]
    }               
   ];

在你的 HTML 中

<tr ng-repeat="(index, hos) in myHospital">
                    <td width="100px">{{hos.name}}</td>
                    <td width="100px" ng-repeat="pro in d.procedure">
                       <p ng-show="pro.hospital[index].id == index+1">{{  pro.hospital[index].price }}</p>
                       <p ng-hide="pro.hospital[index].id == index+1 && pro.hospital[index].price">NULL</p>
                    </td>
                </tr>

【讨论】:

  • 感谢您的回答,感谢您的工作。但不幸的是,我从后端获取此 JSON,因此我不会通过硬编码(如手动插入空值)来更改它。但是有没有任何编程方式可以在我的 JSON 中插入空值,比如 for each loop 或类似的东西。但是如果我们能够以编程方式实现这一点,那么仍然存在记录排序的问题,因为 ng-repeat 将按顺序选择记录。我希望你能理解我想要表达的意思。
  • 我正在尝试使用“ng-if”来实现这一点。在 ng-if 中使用一些参数(如 id 和过程名称以及函数内部)调用函数我试图为特定“过程”构建匹配 id 的逻辑并返回该“医院”的 PRICE。
  • 如果我们有一个 Id 和 price 为 null 的对象,我不明白为什么排序会有任何问题。顺便说一句,是的,你可以在 js 中编写一个 for 循环并获得所需数据源轻松!你要我写吗?如果您想这样做,后端会很容易。
  • 是的,当然,如果我们遵循这种方法,那么我们必须首先执行两个操作,我们必须在医院对象中为不存在的 id 添加空值。然后我们必须根据 id 对医院对象进行排序。如果这是可能的,那么请提供此代码。会更有帮助
【解决方案3】:

不是确切的解决方案,但可能会有所帮助..

<table border="2">
 <tbody ng-repeat="cat in data">
  <tr>
   <td>{{cat.category}}</td>
  </tr> 
  <tr ng-repeat="procedure in cat.procedure">
   <td>{{procedure.name}}</td>
   <td ng-repeat="hospital in procedure.hospital">{{hospital.price}}</td>
  </tr>
</tbody>

【讨论】:

  • 感谢@Develop tech 的网络开发人员,但我还需要更多。 :(
【解决方案4】:

试试智能表。 https://github.com/lorenzofox3/Smart-Table

我们会在 ng-repeat 之前准备好您的数据以满足您的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 2015-02-23
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多