Grid Client Side API





Client side api is called by doing $('#gridId').data('api') and an api method

  • .load({ reset: true }) - will bring the grid back to the initial state defined in the markup
  • .load({group, sort, params, oparams}) - loads the grid with the specified grouping rules, sorting and additional parameters, if a property is omitted than the grid won't change it's state for that property; for example calling .load({group: ['Food', 'Location']}) will change the grouping but won't affect the current sorting rules
  • .clearpersist() - clears the persistence data
GridDemo/ClientSideApi.cshtml
@(Html.Awe().Grid("ApiDemoGrid")
.Mod(o => o.Main())
.Columns(
new Column { Bind = "Id", Width = 75 },
new Column { Bind = "Person" },
new Column { Bind = "Food.Name" },
new Column { Bind = "Price", Width = 100 },
new Column { Bind = "Date", Width = 130 },
new Column { Bind = "Location" })
.Height(350)
.Url(Url.Action("ApiGrid")))
Awesome/Grid/LunchGridController.cs
public class LunchGridController : Controller
{
public IActionResult GetItems(GridParams g, int? country, string person, string food)
{
var query = Db.Lunches.AsQueryable();

if (person != null) query = query.Where(o => o.Person.ToLower().Contains(person.ToLower()));
if (food != null) query = query.Where(o => o.Food.Name.ToLower().Contains(food.ToLower()));
if (country.HasValue) query = query.Where(o => o.Country.Id == country);

var gmb = new GridModelBuilder<Lunch>(query.AsQueryable(), g);

gmb.KeyProp = o => o.Id;

// grid row model
// columns bound to Food.Name by default will display rowModel.FoodName
// or specify in the view Column.Prop ="FooName", or ClientFormat = ".(FoodName)"
gmb.Map = o => new
{
o.Id,
o.Person,
FoodName = o.Food.Name,
FoodPic = o.Food.Pic,
o.Location,
o.Price,
Date = o.Date.ToShortDateString(),
CountryName = o.Country.Name,
ChefName = o.Chef.FullName
};

return Json(gmb.Build());
}

public IActionResult GetItemsEFExample(GridParams g, int? country, string person, string food)
{
var query = Db.Lunches.AsQueryable();

// Filter - custom extension in demo code
// equivalent to .Where(o => o.Food.Name.Contains(food) || ...)

query = query
.Filter(food, o => o.Food.Name)
.Filter(person, o => o.Person);

if (country.HasValue) query = query.Where(o => o.Country.Id == country);

var gmb = new GridModelBuilder<Lunch>(query, g);
gmb.KeyProp = o => o.Id;
gmb.Map = o => new
{
o.Id,
o.Person,
FoodName = o.Food.Name,
FoodPic = o.Food.Pic,
o.Location,
o.Price,
Date = o.Date.ToShortDateString(),
CountryName = o.Country.Name,
ChefName = o.Chef.FullName
};

return Json(gmb.Build());
}
}

Events




Comments