【问题标题】:jqGrid->treeGrid doesn't work with my angular directivejqGrid->treeGrid 不适用于我的角度指令
【发布时间】:2015-12-06 23:29:56
【问题描述】:

我正在使用带有 angularJS 指令的 JqGrid。 例如,我正在使用来自jqgrid loading json datas from server into treegrid doesn't display datas的代码

这是指令:

.directive('ngJqGrid', function () {
    return {
        restrict: 'E',
        scope: {
            config: '=',
            data: '=',
        },
        link: function (scope, element, attrs) {
            var table;

            scope.$watch('config', function (newValue) {
                element.children().empty();
                table = angular.element('<table></table>');
                element.append(table);
                $(table).jqGrid(newValue);
            });

            scope.$watch('data', function (newValue, oldValue) {
                var i;
                for (i = oldValue.length - 1; i >= 0; i--) {
                    $(table).jqGrid('delRowData', i);
                }
                for (i = 0; i < newValue.length; i++) {
                    $(table).jqGrid('addRowData', i, newValue[i]);
                }
            });
        }
    };
});

这是来自角度控制器的代码:

$scope.data = {};
            $http.get('home/GetDataJson').success(function (data, status, headers, config) {
                $scope.config = {
                    datatype: "json",
                    colNames: ['id', 'Prestations'],
                    colModel: [
                        { name: 'id', width: 100, key: true },
                        { name: 'name', width: 785, sortable: false }
                    ],
                    sortname: 'id',
                    sortorder: "asc",
                    treeGrid: true,
                    treeGridModel: "adjacency",
                    ExpandColumn: 'name',
                    ExpandColClick: true,
                    jsonReader: { repeatitems: false, root: function (obj) { return obj; } },
                    height: "auto"
                }

                $scope.data = data;
            });

这是 JSON 数据:

[
    {
        "id": "1",
        "name": "ECHANGEUR",
        "level": "0",
        "parent": "null",
        "isLeaf": false,
        "expanded": false,
        "loaded": true
    },
    {
        "id": "1_1",
        "name": "Intervention Aller sur Site",
        "level": "1",
        "parent": "1",
        "isLeaf": false,
        "expanded": false,
        "loaded": true
    },
    {
        "id": "1_1_1",
        "name": "Date et heure d'arrivée sur le site",
        "level": "2",
        "parent": "1_1",
        "isLeaf": true,
        "expanded": true,
        "loaded": true
    },
    {
        "id": "1_1_2",
        "name": "Consignation de l'échangeur",
        "level": "2",
        "parent": "1_1",
        "isLeaf": true,
        "expanded": true,
        "loaded": true
    }
]

出现网格,但 treeGrid 功能不起作用。而不是所有的行都被展开,所以我们可以认为它不仅仅是grid,而不是treeGrid。 指令中可能有问题。 请帮我! 谢谢!

要查看我的问题,请打开此链接http://plnkr.co/edit/50dagqDV2hWE2UxG9Zjo?p=preview 并替换 2 个文件 HTML 和 JavaScript JS:

var myApp = angular.module('myApp', []);

myApp.directive('ngJqGrid', function() {
  return {
    restrict: 'E',
    scope: {
      config: '=',
      data: '=',
    },
    link: function(scope, element, attrs) {
      var table;

      scope.$watch('config', function(newValue) {
        element.children().empty();
        table = angular.element('<table></table>');
        element.append(table);
        $(table).jqGrid(newValue);
      });

      scope.$watch('data', function(newValue, oldValue) {
        var i;
        for (i = oldValue.length - 1; i >= 0; i--) {
          $(table).jqGrid('delRowData', i);
        }
        for (i = 0; i < newValue.length; i++) {
          $(table).jqGrid('addRowData', i, newValue[i]);
        }
      });
    }
  };
});

myApp.controller('MyController', function($scope) {

  $scope.config = {
    datatype: "local",
    colNames: ['id', 'Prestations'],
    colModel: [{
      name: 'id',
      width: 100,
      key: true
    }, {
      name: 'name',
      width: 785,
      sortable: false
    }],
    sortname: 'id',
    sortorder: "asc",
    treeGrid: true,
    treeGridModel: "adjacency",
    ExpandColumn: 'name',
    ExpandColClick: true,
    jsonReader: {
      repeatitems: false,
      root: function(obj) {
        return obj;
      }
    },
    height: "auto"
  }

  $scope.data = [{
    "id": "1",
    "name": "ECHANGEUR",
    "level": "0",
    "parent": "null",
    "isLeaf": false,
    "expanded": false,
    "loaded": true
  }, {
    "id": "1_1",
    "name": "Intervention Aller sur Site",
    "level": "1",
    "parent": "1",
    "isLeaf": false,
    "expanded": false,
    "loaded": true
  }, {
    "id": "1_1_1",
    "name": "Date et heure d'arrivée sur le site",
    "level": "2",
    "parent": "1_1",
    "isLeaf": true,
    "expanded": true,
    "loaded": true
  }, {
    "id": "1_1_2",
    "name": "Consignation de l'échangeur",
    "level": "2",
    "parent": "1_1",
    "isLeaf": true,
    "expanded": true,
    "loaded": true
  }];
});

HTML:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <link data-require="jqgrid@*" data-semver="4.5.2" rel="stylesheet" href="//cdn.jsdelivr.net/jqgrid/4.5.2/css/ui.jqgrid.css" />
    <script data-require="jqgrid@*" data-semver="4.5.2" src="//cdn.jsdelivr.net/jqgrid/4.5.2/jquery.jqGrid.js"></script>
    <script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>

    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="myApp" ng-controller="MyController">
    <ng-jq-grid config="config" data="data"></ng-jq-grid>
  </body>

</html>

对不起,我的小经历 :) 谢谢!

【问题讨论】:

  • 您使用哪个版本的 jqGrid 和哪个分支(free jqGrid、Guriddo jqGrid JS 或版本 datatype?选项datatype: "json" 表示从url 参数加载。可能您想使用datatype: "local" 或datatype: "jsonstring"?使用addRowData 填充 TreeGrid 不是最佳选择,尤其是您使用 0,1,... 作为 id 而不是数据中的使用 id。
  • 求解答! jqGrid 版本 - 4.6.0 我在我的应用程序中尝试了这个示例stackoverflow.com/questions/15204930/…。一切正常。但是如果我通过 Angular 使用 jqGrid,那么它就可以在没有树的情况下工作
  • @Oleg,请参阅说明。我修复了内容。请帮我。非常感谢!
  • 我已经解决了这个问题。刚刚更改指令:scope.$watch('data', function (newValue, oldValue) { table[0].addJSONData({ rows: newValue }); });
  • 首先考虑使用datatype: "local"。要替换本地网格中的数据,您可以首先获取对包含 jqGrid 的 all 选项的内部对象的 引用:var p = $(table).jqGrid("getGridParam"); 然后您可以使用 p.data = []; 来使用p.data = newValue; 清除数据或将数据替换为新的项目数组,您可以根据需要以任何方式修改数组p.data(删除一些项目或推送一些新项目)。最后,您需要$(table).trigger("reloadGrid");,它将执行所有必需的操作,包括数据排序和调用addJSONData。

标签: javascript jquery angularjs json jqgrid


【解决方案1】:
scope.$watch('data', function (newValue, oldValue) {
                table[0].addJSONData({
                    rows: newValue
                });

            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多