Kyle Giard-Chase
Kyle Giard-Chase

Reputation: 243

Updating cell data in ui-grid without page refresh

I have a table that I made using ui-grid. The table displays information about a list of news stories. In the final column, I am giving the user the ability to edit, delete, and/or make a news story.

Everything seems to be working fine with that - I am able to have a modal pop up, and after entering in the required information, I can see my data change on the server. If I refresh the page, I can also see my data change on the table. However, I would like the cell data to change without page refresh. It doesn't seem to be binding to my news data.

Here is the ui-grid:

$scope.news = getAdminNews.results;


    /**
     * ui-grid implementation
     */
    $scope.gridOptions = {
      data: 'news',
      enableColumnResizing: true,
      enableFiltering: true,
      enableGridMenu: false,
      enableColumnMenus: false,
      paginationPageSizes: [25,50,75],
      paginationPageSize: 25,
      rowHeight: 75
    };

    $scope.gridOptions.columnDefs = [{
        displayName: 'Title',
        field: 'title.rendered',
        filterCellFiltered: true,
        sortCellFiltered: true,
        width: '20%'
      }, {
        displayName: 'Body',
        field: 'body',
        width: '20%'
      }, {
        displayName: 'Created',
        field: 'created',
        type: 'date',
        cellFilter: 'date:"yyyy-MM-dd"',
        filterCellFiltered: true,
        sortCellFiltered: true,
        width: '20%',
        sort: {
          priority: 0
        }
      },
      {
        displayName: 'Actions',
        displayName: 'Actions',
        width: '20%',
        enableSorting: false,
        enableFiltering: false,
        field: 'actions',
        cellTemplate: 'table-cell-actions'
      }
    ];

    $scope.gridOptions.onRegisterApi = function(gridApi) {
      $scope.gridApi = gridApi;
    };

And one part of my controller which is working successfully:

    $scope.newPost = function() {
      ngDialog.openConfirm({
        template: 'modalPostNew',
        className: 'ngdialog-theme-default',
        scope: $scope
        }).then(function() {
          var newPost = {
            "id": 0,
            "title": $scope.newPostForm.title
          }
          AdminNews.save(newPost);
          toaster.pop('success', 'Updated', 'News item updated successfully.');
      });
    }

Upvotes: 0

Views: 1244

Answers (1)

Kyle Giard-Chase
Kyle Giard-Chase

Reputation: 243

I needed to update my $scope.news data that ui-grid was populating the table with. Here is what my controller looks like now:

 $scope.newPost = function() {
      ngDialog.openConfirm({
        template: 'modalPostNew',
        className: 'ngdialog-theme-default',
        scope: $scope
        }).then(function() {
          var newPost = {
            "id": 0,
            "title": $scope.newPostForm.title
          }
          AdminNews.save(newPost).$promise.then(function(response) {
            var myObj = {
              "id": response.data.id,
              "title": {
                "rendered": response.data.title
              }
            }
            $scope.news.push(myObj);
          });
          toaster.pop('success', 'Updated', 'News item updated successfully.');
      });
    }

Upvotes: 0

Related Questions