Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-grid when using $http to load json data

I am using ag-grid plugin in a project. I get json data using $http service. But the grid shows blank in web page. But when json data is applied directly, grid works. I think this is probably due to delay in getting the data from $http. But as per angular concept, the grid should be updated when data comes. Is there any solution to show the html page only when data comes from the server.

Below is my javascript file 'fileBrowser.js':

var fileBrowserModule = angular.module('fileBrowser', ['agGrid']);

fileBrowserModule.controller('fileBrowserController', function($scope, $http) {

    $scope.rowData=[
                ];

    $http.get("http://localhost:8080/KKR/flow/sin/allSins.json")
    .success(function(data) {


    $scope.rowData=JSON.parse("["+JSON.stringify(data)+"]");
        console.log($scope.rowData);
    });



 /*  
 $scope.rowData=[                
{"group":true,"data":{"name":"joe"},
    "children":[
                {"group":true,"data":{"name":"pat"},
                    "children":[{"group":true,
                        "data":{"name":"maya"},
                        "children":[{"group":false,
                            "data":{"name":"brook"},
                            "children":[]},{"group":true,
                                "data":{"name":"kery"},
                                "children":[{"group":false,
                                    "data":{"name":"santosh"},
                                    "children":[]}]}]}]},
                                    {"group":false,
                                        "data":{"name":"aravind"},"children":[]}]}
 ]
           */


    var columnDefs = [
        {headerName: "Name", field: "name", width: 250,
            cellRenderer: {
                renderer: 'group',
                innerRenderer: innerCellRenderer
            }},
        {headerName: "Size", field: "size", width: 70, cellStyle: sizeCellStyle},
        {headerName: "Type", field: "type", width: 150},
        {headerName: "Date Modified", field: "dateModified", width: 150}
       ];

    $scope.gridOptions = {
        columnDefs: columnDefs,
        rowData: $scope.rowData,
        rowSelection: 'multiple',
        rowsAlreadyGrouped: true,
        enableColResize: true,
        enableSorting: true,
        rowHeight: 20,
        icons: {
            groupExpanded: '<i class="fa fa-minus-square-o"/>',
            groupContracted: '<i class="fa fa-plus-square-o"/>'
        },
        onRowClicked: rowClicked
    };

    $scope.selectedFile = 'Select a file below...';



    function rowClicked(params) {
        var node = params.node;
        var path = node.data.name;
        while (node.parent) {
            node = node.parent;
            path = node.data.name + '\\' + path;
        }
        $scope.selectedFile = path;
    }

    function sizeCellStyle() {
        return {'text-align': 'right'};
    }

    function innerCellRenderer(params) {
        var image;
        if (params.node.group) {
            image = params.node.level === 0 ? 'disk' : 'folder';
        } else {
            image = 'file';
        }
        var imageFullUrl = '/example-file-browser/' + image + '.png';
        return '<img src="'+imageFullUrl+'" style="padding-left: 4px;" /> ' + params.data.name;
    }

});

Below is my html file:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="styles/angular/fileBrowser.css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <!--   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
  --> 
     <script src=" https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>


    <!-- you don't need ignore=notused in your code, this is just here to trick the cache -->
    <script src="scripts/angular/ag-grid.js?ignore=notused10"></script>
    <link rel="stylesheet" type="text/css" href="styles/angular/ag-grid.css?ignore=notused10">
    <link rel="stylesheet" type="text/css" href="styles/angular/theme-fresh.css?ignore=notused10">

    <script src="scripts/angular/fileBrowser.js"></script>

</head>

<body ng-app="fileBrowser">

    <div ng-controller="fileBrowserController"
         style="border: 1px solid darkgrey;
                width: 600px;
                background-color: lightgrey;
                border-radius: 5px;
                padding: 3px;">
        <div style="border: 1px solid darkgrey; margin-bottom: 2px; background-color: white;"> &nbsp; {{selectedFile}}</div>
        <div style="width: 100%; height: 400px;"
             ag-grid="gridOptions"
             class="ag-file-browser">
        </div>

    </div>

</body>
</html>
like image 808
Sajeev Zacharias Avatar asked Oct 02 '15 10:10

Sajeev Zacharias


2 Answers

Use the ag-Grid API for setting the row data.

Look at this example "Further Example Starting Point" to see.

Code is

$scope.gridOptions.api.setRows(res.data);
like image 168
Niall Crosby Avatar answered Sep 23 '22 17:09

Niall Crosby


I have modified previous answer. Instead of setRows we can use setRowData. This works for me:

$scope.gridOptions.api.setRowData(res.data);
like image 23
prem Avatar answered Sep 21 '22 17:09

prem