1

I am learning Angular and using the UI grid where I populate the grid from a simple loop method in the ShopplingList controller. The grid is displayed with the column headings but the data is not and I have no errors in chrome when I hit F12. Any help on where I am going wrong!!

My HTML and Javascript

var app = angular.module('app', ['ui.grid']);

app.controller('MainCtrl', ['$scope', '$http',
  function($scope, $http) {

    $scope.gridOptions1 = {};

    $scope.gridOptions1.columnDefs = [{
      name: 'ID'
    }, {
      name: 'Shopping Item'
    }];

    $http.get("/ShoppingList/getShoppingItems/")
      .success(function(data) {
        $scope.gridOptions1.data = data;
      }).error(function(error) {
        console.log(error);
      });

  }
]);
body {} .grid {
  width: 1500px;
  height: 450px;
}
<!DOCTYPE html>

<html ng-app="app">

<head>
  <meta name="viewport" content="width=device-width" charset="UTF-8" />
  <title>ShoppingList</title>
</head>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/ui-grid.min.js"></script>
<script src="~/Scripts/Irlca/ShoppingList.js"></script>
<link href="~/Content/ui-grid.css" rel="stylesheet" type="text/css" />
<link href="~/Content/main.css" rel="stylesheet" type="text/css" />

<body>
  <div ng-controller="MainCtrl">
    <div id="grid1" ui-grid="gridOptions1" class="grid"></div>
  </div>
</body>

</html>

My Model

public class ShoppingListModel
{
    public int id { get; set; }
    public string shoppingItem { get; set; }
}

My Controller public class ShoppingListController : Controller { // // GET: /ShoppingList/ public ActionResult ShoppingList() { return View("ShoppingList"); }

    public ActionResult getShoppingItems()
    {
        List<ShoppingListModel> lstRes = new List<ShoppingListModel>();

        for (int i = 0; i < 10; i++)
        {
            ShoppingListModel tsk = new ShoppingListModel();
            tsk.id = i * 10;
            tsk.shoppingItem = "Milk" + i.ToString();
            lstRes.Add(tsk);
        }

        return Json(lstRes, JsonRequestBehavior.AllowGet);
    }
}
1
  • I have an assumption but first... can you debug the get function and make sure you get the right response? (a list if items) Commented Jul 24, 2016 at 11:40

1 Answer 1

1

Assuming your get method receives the requested entities, there is one thing missing in your ui-grid configuration: the entity's field. This is set in columnDefs property and is in charge of connecting the name property (which will be the column header) to the actual entity field.

Try this:

$scope.gridOptions1.columnDefs = [{
      name: 'ID',
      field: 'id'
    }, {
      name: 'Shopping Item',
      field: 'shoppingItem'
    }];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.