Grid inline editing conditional


rows with 1 meal are not editable
dates with year less than 2012 are readonly
GridInlineEditDemo/ConditionalDemo.cshtml
@Html.InitDeletePopupForGrid("DinnersGrid2", "GridInlineEditDemo")

<div class="bar">
@Html.Awe().Button().Text("Create").OnClick("$('#DinnersGrid2').data('api').inlineCreate()").CssClass("mbtn")
</div>
@{
var isAdmin = false;
}
@(Html.Awe().Grid("DinnersGrid2")
.Mod(o => o.PageInfo().InlineEdit(Url.Action("Create"), Url.Action("Edit")))
.Url(Url.Action("GridGetItems"))
.Height(350)
.Resizable(true)
.Columns(
new Column { Bind = "Id", Width = 75 }.Mod(o => o.InlineId()),
new Column { Bind = "Name"}.Mod(o =>
{
if (isAdmin)
o.Inline(Html.Awe().TextBox("Name"));
else
o.InlineReadonly();// readonly, used if you set isAdmin = false
}),
new Column { Bind = "Date", Width = 170 }
.Mod(o => o.InlineFunc("dateCond",
f => f.Param("<input type='hidden' name='Date' value='#Value'/>#Value")
.Param(Html.Awe().DatePicker("Date"))
)),
new Column { Bind = "Chef.FirstName,Chef.LastName", ClientFormat = ".ChefName", Header = "Chef", Width = 200 }
.Mod(o => o.Inline(Html.Awe().Lookup("Chef").Controller("ChefLookup"), "ChefId")),
new Column { ClientFormat = ".Meals", Header = "Meals", Width = 220 }
.Mod(o => o.Inline(Html.Awe().AjaxCheckboxList("Meals").Multiselect().DataFunc("utils.getItems(meals)"), "MealsIds")),
new Column { Bind = "BonusMeal.Name", ClientFormat = ".BonusMeal", Header = "Bonus Meal" }
.Mod(o => o.Inline(Html.Awe().AjaxRadioList("BonusMealId").DataFunc("utils.getItems(meals)").Odropdown(), "BonusMealId")),
new Column { Bind = "Organic", Width = 100, ClientFormat = ".DispOrganic" }.Mod(o => o.InlineBool(cssClass: "btoggle")),
new Column { ClientFormatFunc = "editff", Width = 60 },
new Column { ClientFormat = GridUtils.InlineDeleteFormat("deleteDinnersGrid2"), Width = 70 }))
<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())";
}
</script>
Demos/Grid/GridInlineEditDemoController.cs
public class GridInlineEditDemoController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult ConditionalDemo()
{
return View();
}

public ActionResult MultiEditorsDemo()
{
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" : "",

// 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 < 2012
};
}

public ActionResult 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)
{
Key = "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 ActionResult Create(DinnerInput input)
{
if (ModelState.IsValid)
{
var dinner = new Dinner
{
Name = input.Name,
Date = input.Date.Value,
Chef = Db.Get<Chef>(input.Chef),
Meals = input.Meals.Select(mid => Db.Get<Meal>(mid)),
BonusMeal = Db.Get<Meal>(input.BonusMealId),
Organic = input.Organic ?? false
};

Db.Insert(dinner);

return Json(new { Item = MapToGridModel(dinner) });
}

return Json(ModelState.GetErrorsInline());
}

[HttpPost]
public ActionResult Edit(DinnerInput input)
{
if (ModelState.IsValid)
{
var dinner = Db.Get<Dinner>(input.Id);
dinner.Name = input.Name;
dinner.Date = input.Date.Value;
dinner.Chef = Db.Get<Chef>(input.Chef);

dinner.Meals = input.Meals.Select(mid => Db.Get<Meal>(mid));

dinner.BonusMeal = Db.Get<Meal>(input.BonusMealId);
dinner.Organic = input.Organic ?? false;
Db.Update(dinner);

return Json(new { });
}

return Json(ModelState.GetErrorsInline());
}

public ActionResult Delete(int id, string gridId)
{
var dinner = Db.Get<Dinner>(id);

return PartialView(new DeleteConfirmInput
{
Id = id,
Message = string.Format("Are you sure you want to delete dinner <b>{0}</b> ?", dinner.Name)
});
}

[HttpPost]
public ActionResult Delete(DeleteConfirmInput input)
{
Db.Delete<Dinner>(input.Id);
return Json(new { input.Id });
}

public ActionResult Popup()
{
return PartialView();
}
}