在html表中嵌套的ng-repeat

时间:2016-07-10 23:45:17

标签: javascript angularjs

我有一个包含三个不同级别数据的表格,当我使用ng-click调用子级别的数据时数据显示正常但是当我调用孙子级别时数据未显示时,我的响应没关系,因为我可以在我的控制台中看到它,所以经过一段时间后我意识到我需要嵌套我的ng-repeat以显示来自孙子级别的数据,但我不知道怎么做,我还检查了ng-repeat-start指令,但不知道它是否适用于我的htlm结构,这就是我的观点。

<table class="table table-bordered drilldown-table" id="fixTable" style="height: 505px">

  <tbody ng-repeat="cat in (exportData.result.slice(0, exportData.result.length-2))" >

    <tr class="parent">

      <td ng-click="drillDownSubcategoriesCat(cat.merchMetrics.DEPT_NBR,cat.merchMetrics.CATG_GRP_NBR,cat.merchMetrics.DEPT_CATG_NBR)"  drill-down>+</td>

      <td style="text-align: left">{{cat.merchMetrics.DEPT_CATG_DESC}}</td>

      <!--SALES-->
      <td>{{cat.merchMetrics.SALES_AMOUNT_LW | number : 2}}</td>
      <td>{{cat.merchMetrics.SALES_LW_CHG_LY}}%</td>
      <td>{{cat.merchMetrics.SALES_L4W_CHG_LY}}%</td>
      <td>{{cat.merchMetrics.SALES_L13W_CHG_LY}}%</td>
      <td>{{cat.merchMetrics.SALES_L52W_CHG_LY}}%</td>
      <td>{{cat.merchMetrics.SALES_LW_VS_L13W}}</td>
   </tr>

   <tr ng-repeat="subcat in drillDownSubcatCatList['D' + cat.merchMetrics.DEPT_NBR + 'CG' + cat.merchMetrics.CATG_GRP_NBR + 'C' + cat.merchMetrics.DEPT_CATG_NBR]" class="child" ng-if="!$last" style="background-color: #f3eee9">

      <td ng-click="drillDownCategoryItemsCat(subcat.merchMetrics.DEPT_NBR, subcat.merchMetrics.CATG_GRP_NBR, subcat.merchMetrics.DEPT_CATG_NBR, subcat.merchMetrics.DEPT_SUBCATG_NBR)"  drill-down>+</td>

      <td style="text-align: left; padding-left: 25px">{{subcat.merchMetrics.DEPT_SUBCATG_DESC}}</td>

      <!--SALES-->
      <td>{{subcat.merchMetrics.SALES_AMOUNT_LW | number : 2}}</td>
      <td>{{subcat.merchMetrics.SALES_LW_CHG_LY}}%</td>
      <td>{{subcat.merchMetrics.SALES_L4W_CHG_LY}}%</td>
      <td>{{subcat.merchMetrics.SALES_L13W_CHG_LY}}%</td>
      <td>{{subcat.merchMetrics.SALES_L52W_CHG_LY}}%</td>
      <td>{{subcat.merchMetrics.SALES_LW_VS_L13W}}</td>
  </tr>

  <tr ng-repeat="items in drillDownCategoryItemCatList[nbrSubcatItem]" class="grandson" ng-if="!$last" style="background-color: #f3eee9">

       <td></td>
       <td style="text-align: left; padding-left: 45px">{{items.merchMetrics.DEPT_SUBCATG_DESC}}}</td>

       <!--SALES-->
       <td>{{items.merchMetrics.SALES_AMOUNT_LW | number : 2}}</td>
       <td>{{items.merchMetrics.SALES_LW_CHG_LY}}%</td>
       <td>{{items.merchMetrics.SALES_L4W_CHG_LY}}%</td>
       <td>{{items.merchMetrics.SALES_L13W_CHG_LY}}%</td>
       <td>{{items.merchMetrics.SALES_L52W_CHG_LY}}%</td>
       <td>{{items.merchMetrics.SALES_LW_VS_L13W}}</td>

  </tr>

</tbody>

要从孩子和孙子级别生成数据,我会调用函数。

   $scope.drillDownSubcatCatList = {};

    $scope.subcatNbr = '';

    $scope.drillDownSubcategoriesCat = function (dept,group,catg) {

        $scope.nbr1 = [dept];
        $scope.nbr2 = [group];
        $scope.nbr3 = [catg];

        $scope.nbrSubcat = 'D' + $scope.nbr1 + 'CG' + $scope.nbr2 + 'C' + $scope.nbr3;
        $scope.nbrSubcat.toString();

        $scope.subCategoryConst = ['Product Sales (Subcategory Rank)'];
        $scope.drillDownRecords = "5";

        console.log($scope.nbr1);
        console.log($scope.nbr2);
        console.log($scope.nbr3);
        console.log("NBR: " + $scope.nbrSubcat);

        if (!($scope.nbrSubcat in $scope.drillDownSubcatCatList)) {

            $scope.loadViewCategories = false;
            $scope.graphLoading = "loading";
            searchFactory.search('merchandise',

                {
                    timeStamp: $scope.dateTime,
                    timeFrameType: $scope.whenType,
                    custMembSelectionType: $scope.customerType,
                    locationSelectionType: $scope.locationType,
                    merchandiseSelectionType: "Category",
                    startYear: $scope.whenStartParentYear,
                    endYear: $scope.whenStartParentYear,
                    startUnit: $scope.whenStartValue,
                    endUnit: $scope.whenStartValue,
                    comparator: $scope.locationType.indexOf('(Comp)', $scope.locationType.length - '(Comp)'.length) !== -1,
                    locationSelections: $scope.locationSelected,
                    merchandiseSelections: [$scope.nbrSubcat],
                    custMembSelections: $scope.customerSelected,
                    metricSelections: $scope.subCategoryConst,
                    rankOrder: $scope.drillDownRecords


                }).success(function (data) {


                    console.log("drillDownSubcategories OK");
                    $scope.loadViewCategories = true;
                    $scope.graphLoading = "white";
                    $scope.canvasContent = true;
                    $scope.exportDataCategoryDrillDown = data;
                    $scope.metricSelected = $scope.filters.baropt.displayName;

                    if (data.error) {
                        $scope.noData = "There are no results based on your selected dropdowns. Please change your selection.";
                        $scope.data = undefined;
                        $scope.hasError = true;
                    }

                    $scope.isExportDisabled = 'auto';

                    if (!$scope.drillDownSubcatCatList[$scope.nbrSubcat]) {

                        $scope.drillDownSubcatCatList[$scope.nbrSubcat] =  $scope.exportDataCategoryDrillDown.result;
                    }



                }).error(function (ret, status) {
                    console.log(ret, status);
                    if (status == 500 || status == 0) {
                        if (ret.error.indexOf("Filter criteria not found!") > -1 || ret.error.indexOf("Bad Selections!") > -1) {
                            $scope.errorMessage = "The combination of dropdowns you selected is invalid.  Please select a different combination.";
                        } else {
                            $scope.errorMessage = "The database may be down. Please contact Data Cafe Development Team - Applications for help.";
                        }
                    } else if (status == 400) {
                        $scope.errorMessage = "There was a bad request sent to the database. Please contact Data Cafe Development Team - Applications for help.";
                    } else {
                        $scope.errorMessage = "There was an error while trying to process your request. Please contact Data Cafe Development Team - Applications for help.";
                    }
                    $scope.hasError = true;
                    $scope.graphLoading = "white";
                    $scope.canvasContent = true;
                    $scope.showCaptions = false;
                }
            );
        }

        if($scope.nbrSubcat in $scope.drillDownSubcatCatList) {
            console.log("--------------- " + $scope.nbrSubcat);
            console.log("RETURN DD SUBCATEGORY LIST: ", $scope.drillDownSubcatCatList);
        }

    }

这是第二个功能:

    $scope.drillDownCategoryItemCatList = {};

    $scope.drillDownCategoryItemsCat = function (dept,group,catg,subcat) {

        $scope.nbr1 = [dept];
        $scope.nbr2 = [group];
        $scope.nbr3 = [catg];
        $scope.nbr4 = [subcat];

        $scope.nbrSubcatItem = 'D' + $scope.nbr1 + 'CG' + $scope.nbr2 + 'C' + $scope.nbr3 + 'SC' + $scope.nbr4;
        $scope.nbrSubcatItem.toString();

        $scope.ItemsConst = ['Product Sales (Item Rank)'];
        $scope.drillDownRecords = "5";

        console.log($scope.nbr1);
        console.log($scope.nbr2);
        console.log($scope.nbr3);
        console.log($scope.nbr4);
        console.log("NBR: " + $scope.nbrSubcatItem);

        if (!($scope.nbrSubcatItem in $scope.drillDownCategoryItemCatList)) {

            $scope.loadViewCategories = false;
            $scope.graphLoading = "loading";
            searchFactory.search('merchandise',

                {
                    timeStamp: $scope.dateTime,
                    timeFrameType: $scope.whenType,
                    custMembSelectionType: $scope.customerType,
                    locationSelectionType: $scope.locationType,
                    merchandiseSelectionType: "SubCategory",
                    startYear: $scope.whenStartParentYear,
                    endYear: $scope.whenStartParentYear,
                    startUnit: $scope.whenStartValue,
                    endUnit: $scope.whenStartValue,
                    comparator: $scope.locationType.indexOf('(Comp)', $scope.locationType.length - '(Comp)'.length) !== -1,
                    locationSelections: $scope.locationSelected,
                    merchandiseSelections: [$scope.nbrSubcatItem],
                    custMembSelections: $scope.customerSelected,
                    metricSelections: $scope.ItemsConst,
                    rankOrder: $scope.drillDownRecords


                }).success(function (data) {


                    console.log("drillDownCategortyItems OK");
                    $scope.loadViewCategories = true;
                    $scope.graphLoading = "white";
                    $scope.canvasContent = true;
                    $scope.exportDataCategoryItemDrillDown = data;
                    $scope.metricSelected = $scope.filters.baropt.displayName;

                    if (data.error) {
                        $scope.noData = "There are no results based on your selected dropdowns. Please change your selection.";
                        $scope.data = undefined;
                        $scope.hasError = true;
                    }

                    $scope.isExportDisabled = 'auto';

                    if (!$scope.drillDownCategoryItemCatList[$scope.nbrSubcatItem]) {

                        $scope.drillDownCategoryItemCatList[$scope.nbrSubcatItem] =  $scope.exportDataCategoryItemDrillDown.result;

                    }


                }).error(function (ret, status) {
                    console.log(ret, status);
                    if (status == 500 || status == 0) {
                        if (ret.error.indexOf("Filter criteria not found!") > -1 || ret.error.indexOf("Bad Selections!") > -1) {
                            $scope.errorMessage = "The combination of dropdowns you selected is invalid.  Please select a different combination.";
                        } else {
                            $scope.errorMessage = "The database may be down. Please contact Data Cafe Development Team - Applications for help.";
                        }
                    } else if (status == 400) {
                        $scope.errorMessage = "There was a bad request sent to the database. Please contact Data Cafe Development Team - Applications for help.";
                    } else {
                        $scope.errorMessage = "There was an error while trying to process your request. Please contact Data Cafe Development Team - Applications for help.";
                    }
                    $scope.hasError = true;
                    $scope.graphLoading = "white";
                    $scope.canvasContent = true;
                    $scope.showCaptions = false;
                }
            );
        }

        if($scope.nbrSubcatItem in $scope.drillDownCategoryItemCatList) {

            console.log("LIST FROM CATEGORY TO ITEM LEVEL: ", $scope.drillDownCategoryItemCatList);
        }

    }

正如我之前提到的,父母和孩子的水平都显示正常,所以我想如何嵌套ng-repeat以便让我的孙子级别显示?

1 个答案:

答案 0 :(得分:1)

这里有一些奇怪的事情,你在UI中需要一个表,但底层模型是一个三层嵌套的对象。 ng-repeat并不意味着嵌套在兄弟DOM级别上。

我建议实现这一点的方法是使用一个角度过滤器,将所有数据包装到一个可以称为ng-repeat on的数组中。

你的控制器里面有太多的逻辑。我认为这有助于解决困难。通常,控制器应负责显示由角度服务提供的数据。角度服务应该以易于控制器使用的方式提供数据。

如果您发现自己在控制器内部创建嵌套if语句,xhr请求和错误处理,那么您做错了。所有这些都应该重构为几个服务(每个服务应该有一个单一的责任)。然后你应该让其中一个服务向控制器提供易于使用的数据。

这是一个模糊的答案,而不是一个简单的答案。我认为你遇到的问题不是完全理解Angular如何实现MVC模式。