Grid inline editing conditional


rows with 1 meal are not editable
dates with year less than 2015 are readonly
GridInlineEditConditional.aspx
<%  var gridId = DinnersGrid2.ClientID;  %>
<%=Page.InitDeletePopupForGrid(gridId, "GridInlineEditDemo") %>

<div class="bar">
<%=Page.InlineCreateButtonForGrid(gridId, new { DispOrganic = "" }) %>
</div>

<%
var isAdmin = false;
%>
<% DinnersGrid2.Grid<Dinner>()
.Main()
.ColumnsSelector(false)
.InlEdit(new InlEditOpt
{
SaveUrl = Page.Url().Action("Save", "GridInlineEditDemo")
})
.Url(Page.Url().Action("GridGetItems", "GridInlineEditDemo"))
.Height(350)
.Columns(b =>{
b.Add(o => o.Id).Width(75).InlId();

b.Add(o => o.Name).InlString();

b.Add(o => o.Date).Width(170)
.Mod(o => o.InlineFunc("dateCond")
.Inline("<input type='hidden' name='Date' value='#Value'/>#Value")
.Inline(Page.Awe().DatePicker("Date")));

b.Add(o => new { o.Chef.FirstName, o.Chef.LastName }, new Column { Header = "Chef", MinWidth = 200 })
.Inl(Page.Awe().Lookup("ChefId").Controller("ChefLookup"), "ChefId");

b.Add(o => o.Meals.Select(m => m.Name))
.Header("Meals")
.MinWidth(200)
.Grow(2)
.InlMultiselect(new MultiselectOpt { Name = "MealsIds", Url = Page.Url().Action("GetMealsImg", "Data") }.ImgItem());

b.Add(o => o.BonusMeal.Name)
.Header("Bonus Meal")
.InlDropdownList(new DropdownListOpt { Name = "BonusMealId", Url = Page.Url().Action("GetMealsImg", "Data") }.ImgItem());

b.Add(o => o.Organic)
.Width(100)
.Prop("DispOrganic")
.Mod(o =>
{
if (isAdmin)
o.Inline(Page.Awe().Toggle(new ToggleOpt { Name = "Organic" }));
else
o.InlineReadonly(); // readonly, used if you set isAdmin = false
});

b.Add(new Column { ClientFormatFunc = "editff", Width = GridUtils.EditBtnWidth });
b.Add(new Column { ClientFormatFunc = "delff", Width = GridUtils.DeleteBtnWidth });
}); %>
<awe:Ocon runat="server" ID="DinnersGrid2" />
<script>
var meals = <%=DemoUtils.Encode(Db.Meals.Select(o => new KeyContent(o.Id, o.Name))) %>;

function dateCond(o, params) {
if (o.DateReadOnly) return params[0];
return params[1];
}

function editff(o) {
return o.Editable === false ? '' : '<%=GridUtils.InlineEditFormat() %>';
}

function delff(o) {
var format = <%=DemoUtils.Encode(Page.InlineDeleteFormatForGrid(gridId)) %>;
format = format.split(".(Id)").join(o.Id);
return o.Editable === false ? '' : format;
}
</script>
Demos/Grid/GridInlineEditDemoController.cs
using System;
using System.Linq;
using AwesomeWebFormsDemo.Data;
using AwesomeWebFormsDemo.Models;
using AwesomeWebFormsDemo.ViewModels.Input;
using System.Web.Mvc;


using Omu.AwemWebForms.Utils;
using Omu.AwesomeWebForms;

namespace AwesomeWebFormsDemo.Controllers.Demos.Grid
{
public class GridInlineEditDemoController : Controller
{
public ActionResult GridGetItems(GridParams g, string search)
{
var gmb = new GridModelBuilder<Dinner>(Db.Dinners.AsQueryable(), g)
{
KeyProp = o => o.Id, // needed for api select, update, tree, nesting, EF
};

gmb.FilterContainsStr(o => new { o.Name, o.Chef.FirstName, o.Chef.LastName }, search);

gmb.Prop(o => o.Organic, "DispOrganic", o => o ? "Yes" : "No");

// props used for inline editing controls values
gmb.PropVal(o => o.Meals.Select(m => m.Id), "MealsIds");
gmb.PropVal(o => o.Chef.Id, "ChefId");
gmb.PropVal(o => o.BonusMeal.Id, "BonusMealId");

// for conditional demo
gmb.Prop(o => o.Meals.Count(), "Editable", count => count > 1);
gmb.Prop(o => o.Date.Year, "DateReadOnly", year => year < 2015);

// for multi editors demo
gmb.PropVal(o => o.Name, "Name");
gmb.PropVal(o => o.Date, "Date", o => o.ToShortDateString());

gmb.Prop(o => o.Meals.Select(m => m.Name), "Meals");
gmb.Prop(o => o.BonusMeal.Name, "BonusMeal");

return this.AweJson(gmb.Build());
}

[HttpPost]
public ActionResult Save(DinnerInput input)
{
// custom validation example
if (ModelState.IsValid && input.Name.Contains("asdf"))
{
ModelState.AddModelError("Name", "Name can't contain asdf");
}

if (ModelState.IsValid)
{
var isCreate = !input.Id.HasValue;

var ent = isCreate ? new Dinner() :
Db.Dinners.FirstOrDefault(o => o.Id == input.Id);

if (ent == null)
{
throw new Exception("Item doesn't exist anymore, id:" + input.Id);
}

ent.Name = input.Name;
ent.Date = input.Date.Value;
ent.Chef = Db.Find<Chef>(input.ChefId);

// ToList req for EF
ent.Meals = Db.Meals
.Where(o => input.MealsIds.Contains(o.Id)).ToList();

ent.BonusMeal = Db.Find<Meal>(input.BonusMealId);
ent.Organic = input.Organic ?? false;

if (isCreate)
{
Db.Add(ent);
}

return Json(new { });
}

return Json(ModelState.GetErrorsInline());
}

public ActionResult Delete(int id)
{
var dinner = Db.Find<Dinner>(id);

return PartialView(new DeleteConfirmInput
{
Id = id,
Type = "dinner",
Name = dinner.Name
});
}

[HttpPost]
public ActionResult Delete(DeleteConfirmInput input)
{
Db.Remove(Db.Find<Dinner>(input.Id));

// the PopupForm's success function aweUtils.itemDeleted expects an obj with "Id" property
return Json(new { input.Id });
}

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

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

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

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

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



Comments