Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading jsGrid by calling Controller/WebService in MVC

Am really struggling to load the jsGrid using Controller service. Am not able to do it correctly.

I even tried the sample code from the jsGrid site demo but that too didn't work either it throws error at !this.data.length or grid doesn't load at all.

I get no data every time I try using below code.

Appreciate if anyone can help.

This is how am loading the jsGrid:

$(element).jsGrid({
   height: 300,
   width: "100%",
    filtering: true,
    sorting: true,
    paging: true,
    autoload: true,
    pageLoading: true,

    controller: {
        loadData: function (filter) {
            $.ajax({
                type: "GET",
                url: "../Common/GetData",
                data: filter,
                dataType: "JSON"
            });
        }
    },
    pageSize: 10,
    pageButtonCount: 5,
    pageIndex: 1,

    noDataContent: "No Record Found",
    loadIndication: true,
    loadIndicationDelay: 500,
    loadMessage: "Please, wait...",
    loadShading: true,

    fields: [
        { name: "Name", type: "textarea", width: 150 },
        { name: "Age", type: "number", width: 50 },
        { name: "Address", type: "text", width: 200 },
        { name: "Country", type: "select" },
         {
             name: "", type: "text", width: 50, sorting: false, filtering: false,
             itemTemplate: function (value) {
                 return '<div class="edit-container"><a class="edit-custom-field-link">Edit</a><div class="sort-icon-container"><div class="up-arrow-icon"></div><div class="down-arrow-icon"></div></div></div>';
             }
         }
        //{ name: "Married", type: "checkbox", title: "Is Married", sorting: false }
        //,{ type: "control" }
    ]
});
like image 667
Joshua I Avatar asked Feb 27 '16 11:02

Joshua I


2 Answers

You should use promises while loading data,

loadData: function(filter) {

  return $.ajax({
        type: "GET",
        url: "../Common/GetData",
        data: filter,
        dataType: "JSON"
    })

}

return $.ajax({}) does return a Promise. thank you

like image 177
irfan Avatar answered Sep 23 '22 10:09

irfan


I too was having problems with JSGrid. I was using the following snippet (as some have suggested):

`

loadData:   function(filter) {
    console.log("LoadData called....")
    var d = $.Deferred();
    $.ajax({
      type: "GET",
      url: "/secure/msgitems",
      data: filter
    }).done(function(response) {
      console.log(  response);
      d.resolve(response);
      return;
  });
 return d.promise();
 },
},

`

I could see the results coming back, but my jsGrid kept puking. It turns out the server must return the data in the following format:

{
data: [items], itemsCount: amountOfItems }

Here is where the developer discusses this topic: https://github.com/tabalinas/jsgrid/issues/35

It seems to work...FWIW

like image 40
Greg S Avatar answered Sep 23 '22 10:09

Greg S