基于angularjs中json输入的下拉选择绑定数据

dsf9zpds  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(328)

我希望你们在这个大流行时期做得很好,抽出一些时间来帮助我。
我正在尝试根据下拉列表的下拉选择筛选数据。这里我将从json输入中获取数据。
在提交日期中,我选择了2022年6月30日,因此在此日期,以下地区办事处可用。

因此,如果我将日期更改为任何其他日期,如2022年7月30日,则仅显示2个地区办事处

如果我们展开+图标,它应该显示如下所示的值。这就是要求,我已经完成了编写扩展折叠功能的代码的工作。

但是,我不确定我需要如何绑定与下面的日期和显示关联的数据。这是我写的代码

  1. var app = angular.module('acturial', ['ui.bootstrap']);
  2. ////configure routes
  3. //// TODO:Will implement and configure routes but for now it is not needed
  4. //app.config(function ($routeprovider) {
  5. // $routeprovider
  6. // // route for default page
  7. // // in our case only page exists for now
  8. // .when('/', { templateurl: 'Index.html', controller: 'Regioncontroller' })
  9. // //todo: can able to add more pages using by specifying the condition in when clause
  10. //});
  11. var RegionController = function ($scope, $http) {
  12. $scope.title = "Acturial App";
  13. //$scope.data = [{
  14. // "name": "Billings",
  15. // "values": ['300031', '300051', '300091', '300111', '300131']
  16. //}];
  17. var regionDetails = [
  18. {
  19. "date": "6/30/2022",
  20. "regionOffice": [
  21. {
  22. "name": "Valdosta",
  23. "values": [
  24. "300031",
  25. "300051",
  26. "300091",
  27. "300111",
  28. "300131"
  29. ]
  30. },
  31. {
  32. "name": "Jackson",
  33. "values": [
  34. "300031",
  35. "300051",
  36. "300091",
  37. "300111",
  38. "300131"
  39. ]
  40. },
  41. {
  42. "name": "Springfield",
  43. "values": [
  44. "300031",
  45. "300051",
  46. "300091",
  47. "300111",
  48. "300131"
  49. ]
  50. },
  51. {
  52. "name": "Billings",
  53. "values": [
  54. "300031",
  55. "300051",
  56. "300091",
  57. "300111",
  58. "300131"
  59. ]
  60. }
  61. ]
  62. },
  63. {
  64. "date": "7/30/2023",
  65. "regionOffice": [
  66. {
  67. "name": "Springfield",
  68. "values": [
  69. "300031",
  70. "300051",
  71. "300091",
  72. "300111",
  73. "300131"
  74. ]
  75. },
  76. {
  77. "name": "Billings",
  78. "values": [
  79. "300031",
  80. "300051",
  81. "300091",
  82. "300111",
  83. "300131"
  84. ]
  85. }
  86. ]
  87. }
  88. ];
  89. $scope.dataArray = regionDetails;
  90. //var billingDetails = {
  91. // name: 'Billings',
  92. // values: ['300031', '300051', '300091', '300111', '300131']
  93. //}
  94. //$scope.data = billingDetails;
  95. // TODO:Still have to make some improvements for the below functions
  96. // The below code will be used when we have WebAPI endpoint so we can use that to populate the data
  97. // instead of the static/hard-coded data
  98. //var onUserComplete = function (response) {
  99. // $scope.data = response.data;
  100. // $http.get($scope.regionUrl)
  101. // .then(onRepos, onError);
  102. //}
  103. //onRepos = function (response) {
  104. // $scope.data = response.data;
  105. //};
  106. //var onError = function (response) {
  107. // $scope.error = "Couldn't able to retreive the data";
  108. //}
  109. $scope.expandedRegion = null;
  110. $scope.manageCollapseExpand = function (obj, isExpandedRegion) {
  111. obj.expanded = !obj.expanded;
  112. if (obj.expanded) {
  113. if (!isExpandedRegion) {
  114. $scope.expandedRegion = obj;
  115. }
  116. }
  117. }
  118. };
  119. app.controller("RegionController", ["$scope", "$uibModal", "$http", RegionController]);

这是html页面

  1. <!DOCTYPE html>
  2. <html ng-app="acturial" ng-controller="RegionController">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>{{title}}</title>
  6. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
  7. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
  8. <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
  9. <script src="Scripts/angular.js"></script>
  10. <script src="acturial.js"></script>
  11. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
  12. <link data-require="bootstrap-css@*" data-semver="4.0.0-alpha.4" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" />
  13. <link data-require="font-awesome@*" data-semver="4.5.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.css" />
  14. <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
  15. </head>
  16. <body>
  17. <div class="">
  18. <label>
  19. Filing Date:
  20. </label>
  21. <select ng-model="data.date" ng-options="data.date for data in dataArray" >
  22. <option value=""> </option>
  23. </select>
  24. <br />
  25. </div>
  26. <div class="">
  27. <button class="btn" ng-click="manageCollapseExpand(region, false)">
  28. <span ng-class="{'glyphicon glyphicon-minus': region.expanded, 'glyphicon glyphicon-plus': !region.expanded }"></span>
  29. </button>
  30. {{region.name}} ({{region.values.length}})
  31. </div>
  32. <div class="" ng-show="region.expanded">
  33. <div class="" ng-repeat="value in region.values">
  34. <div class="">
  35. {{value}}
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

因此,您可以帮助我绑定与下拉选择值关联的数据并显示在下面吗?
下面是我在控制台中看到的结构化阵列

z18hc3ub

z18hc3ub1#

我们将这样设置数据,因为ng选项(以及选择默认的预选选项)需要绑定到同一个对象。在同一个对象上,我们将绑定所选日期和完整数组。此外,我们还需要一个索引变量,用于将办公室列表与特定日期关联。

  1. $scope.selectedDateIndex= null
  2. $scope.ds = {};
  3. $scope.ds.dataArray = regionDetails;
  4. $scope.ds.date = $scope.ds.dataArray[0].date // this is our default pre-selected date
  5. $scope.onSelectDate() // this will trigger showing the default date's office list
  6. // this function will take the date that is chosen, find the data set in the array that matches, and set the `$scope.selectedDateIndex` we'll need
  7. $scope.onSelectDate = function () {
  8. let date = $scope.ds.date;
  9. console.log(date)
  10. $scope.ds.dataArray.forEach((el, index) => {
  11. if (el.date == date) $scope.selectedDateIndex = index
  12. })
  13. }

对于 <select> ,设立 ng-change 方法。另外,请注意我们之前设置的对象上的ng模型点,以及 ng-options 有这种格式吗 optionValue as optionName forEach item in dataArray . 为此,angular必须同时关联选项名称和值。

  1. <select ng-model="ds.date"
  2. ng-options="data.date as data.date for data in ds.dataArray"
  3. ng-change="onSelectDate()">
  4. </select>

然后,这里有两个外部div,都使用 region 对象是错误的。我把它们合在一起。您的切换按钮不应调用外部函数-只需设置region.expanded=!地区扩大了。这样我们就可以把它放在物体里面 region 它只存在于这个循环中。

  1. <div class=""
  2. ng-if="selectedDateIndex !== null"
  3. ng-repeat="region in ds.dataArray[selectedDateIndex].regionOffice">
  4. <!-- for the rest of this loop we can work with the `region` object -->
  5. <button class="btn" ng-click="region.expanded = !region.expanded">
  6. <span
  7. ng-class="{'glyphicon glyphicon-minus': region.expanded, 'glyphicon glyphicon-plus': !region.expanded }"></span>
  8. </button>
  9. {{region.name}} ({{region.values.length}})
  10. <div class="" ng-show="region.expanded">
  11. <div class=""
  12. ng-repeat="value in region.values">
  13. <div class="">
  14. {{value}}
  15. </div>
  16. </div>
  17. </div>
  18. </div>

我无法测试这个,所以如果您遇到错误,请告诉我。

展开查看全部

相关问题