Awesome ASP.net Core and MVC Controls
Grid inline editing conditional
rows with 1 meal are not editable
dates with year less than 2015 are readonly
Organic is editable only if isAdmin set to true (set to false in code)
dates with year less than 2015 are readonly
Organic is editable only if isAdmin set to true (set to false in code)
GridInlineEditDemo/ConditionalDemo.cshtml
@{ var grid = "DinnersGrid2"; }
@Html.InitDeletePopupForGrid(grid, "GridInlineEditDemo")
<div class="bar">
@Html.InlineCreateButtonForGrid(grid, new { DispOrganic = "" })
</div>
@{
var isAdmin = false;
}
@(Html.Awe().Grid(grid)
.Mod(o => o.PageInfo().InlineEdit(Url.Action("Save")))
.Url(Url.Action("GridGetItems"))
.Height(350)
.Resizable()
.Columns(
new Column { Bind = "Id", Width = 75 }.Mod(o => o.InlineId()),
new Column { Bind = "Name" }.Mod(o => o.Inline(Html.Awe().TextBox("Name"))),
new Column { Bind = "Date", Width = 170 }
.Mod(o => o.InlineFunc("dateCond")
.Inline("<input type='hidden' name='Date' value='#Value'/>#Value")
.Inline(Html.Awe().DatePicker("Date"))),
new Column { Bind = "Chef.FirstName,Chef.LastName", ClientFormat = ".(ChefName)", Header = "Chef", MinWidth = 200 }
.Mod(o => o.Inline(Html.Awe().Lookup("Chef").Controller("ChefLookup"), "ChefId")),
new Column { ClientFormat = ".(Meals)", Header = "Meals", MinWidth = 200, Grow = 2}
.Mod(o => o.Inline(
Html.Awe().AjaxCheckboxList("Meals")
.Url(Url.Action("GetMealsImg", "Data"))
.Multiselect(), "MealsIds")),
new Column { Bind = "BonusMeal.Name", ClientFormat = ".(BonusMeal)", Header = "Bonus Meal" }
.Mod(o => o.Inline(
Html.Awe().AjaxRadioList("BonusMealId")
.Url(Url.Action("GetMealsImg", "Data"))
.Odropdown(), "BonusMealId")),
new Column { Bind = "Organic", Width = 100, ClientFormat = ".(DispOrganic)" }
.Mod(o =>
{
if (isAdmin)
o.Inline(Html.Awe().CheckBox("Organic").Otoggl());
else
o.InlineReadonly(); // readonly, used if you set isAdmin = false
}),
new Column { ClientFormatFunc = "editff", Width = 80 },
new Column { ClientFormatFunc = "delff", Width = 85 }))
<script>
function dateCond(o, params) {
if (o.DateReadOnly) return params[0];
return params[1];
}
function editff(o) {
return o.Editable === false ? '': '@Html.Raw(GridUtils.InlineEditFormat())';
}
function delff(o) {
var format = @Html.Raw(DemoUtils.Encode(Html.InlineDeleteFormatForGrid(grid)));
format = format.split(".(Id)").join(o.Id);
return o.Editable === false ? '': format;
}
</script>
Demos/Grid/GridInlineEditDemoController.cs
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using AweCoreDemo.Models;
using AweCoreDemo.ViewModels.Input;
using Omu.Awem.Utils;
using Omu.AwesomeMvc;
namespace AweCoreDemo.Controllers.Demos.Grid
{
public class GridInlineEditDemoController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult ConditionalDemo()
{
return View();
}
public IActionResult MultiEditorsDemo()
{
return View();
}
public IActionResult ClientSave()
{
return View();
}
private object MapToGridModel(Dinner o)
{
return new
{
o.Id,
o.Name,
Date = o.Date.ToShortDateString(),
ChefName = o.Chef.FirstName + " " + o.Chef.LastName,
Meals = string.Join(", ", o.Meals.Select(m => m.Name)),
BonusMeal = o.BonusMeal.Name,
o.Organic,
DispOrganic = o.Organic ? "Yes" : "No",
// below properties used for inline editing only
MealsIds = o.Meals.Select(m => m.Id).ToArray(),
ChefId = o.Chef.Id,
BonusMealId = o.BonusMeal.Id,
// for conditional demo
Editable = o.Meals.Count() > 1,
DateReadOnly = o.Date.Year < 2015
};
}
public IActionResult GridGetItems(GridParams g, string search)
{
search = (search ?? "").ToLower();
var items = Db.Dinners.Where(o => o.Name.ToLower().Contains(search)).AsQueryable();
var model = new GridModelBuilder<Dinner>(items, g)
{
KeyProp = o => o.Id, // needed for api select, update, tree, nesting, EF
GetItem = () => Db.Get<Dinner>(Convert.ToInt32(g.Key)), // called by the grid.api.update
Map = MapToGridModel,
}.Build();
return Json(model);
}
[HttpPost]
public IActionResult Save(DinnerInput input)
{
if (ModelState.IsValid)
{
var edit = input.Id.HasValue;
var ent = edit ? Db.Get<Dinner>(input.Id) : new Dinner();
ent.Name = input.Name;
ent.Date = input.Date.Value;
ent.Chef = Db.Get<Chef>(input.Chef);
ent.Meals = input.Meals.Select(mid => Db.Get<Meal>(mid));
ent.BonusMeal = Db.Get<Meal>(input.BonusMealId);
ent.Organic = input.Organic ?? false;
if (edit)
{
Db.Update(ent);
}
else
{
Db.Insert(ent);
}
return Json(new { Item = MapToGridModel(ent) });
}
return Json(ModelState.GetErrorsInline());
}
public IActionResult Delete(int id)
{
var dinner = Db.Get<Dinner>(id);
return PartialView(new DeleteConfirmInput
{
Id = id,
Type = "dinner",
Name = dinner.Name
});
}
[HttpPost]
public IActionResult Delete(DeleteConfirmInput input)
{
Db.Delete<Dinner>(input.Id);
// the PopupForm's success function utils.itemDeleted expects an obj with "Id" property
return Json(new { input.Id });
}
public IActionResult Popup()
{
return PartialView();
}
}
}
Comments