Introduction
Using this code is possible to update fields in any HTML table, it's not necessary to open an edit view and confirm each modification, save action fires on blur event, as in Excel grid.
Background
To use this code, it's necessary to include this plugin: http://www.codeproject.com/Tips/665477/LINQ-to-JQuery.
All JavaScript files are included to download.
Server side is focused in Entity Framework and uses reflection.
Using the Code
Client Side: The View
How to use the plugin in client side:
@{
int i = 0;
foreach (var item in Model) {
<tr>
<td>
<input type="text"
value="@item.Name" data-row="@i"
data-col="Name" data-val="@item.Name" />
<input type="hidden" id="@i" value="@item.Id" />
i++;
}
}
<!-- Javascript files required -->
<script src="~/JS/linqToJquery.js"></script>
<!-- plugin must be downloaded before -->
<script src="~/JS/grid.js"></script>
<!-- The Grid Javascript class attached below -->
<!-- The javascript/Jquery code -->
<script>
var grid={};
$(document).ready(function () {
grid = new Grid("controllerName","actionName");
grid.fieldsValidators.push({
fieldName: "name"
, checkValidateFunc: function (value) { return (value.length > 0); }
, msg: "required field"
});
grid.bindFields();
});
</script>
Client Side: Grid JavaScript Class
The JavaScript plugin, it's not necessary to modify it.
function Grid(controller, action) {
var that = this;
this.controller = controller;
this.action = (action == null ? "UpdateField" : action);
this.fieldsValidators = [{}];
}
Grid.prototype.bindFields = function () {
var grid = this;
$("td input").focus(function () {
if ($(this).data("col") == undefined) return;
$(this).addClass("gridFieldEdit"); });
$("td input").blur(function () {
if ($(this).data("col") == undefined) return;
$(this).removeClass("gridFieldEdit");
if ($(this).val() == $(this).data("val")) return;
if (!grid.checkField($(this).data("col").toLowerCase(), $(this).val())) {
var msgError = grid.fieldsValidators.First("['fieldName']=='" +
$(this).data("col").toLowerCase() + "'").msg;
alert(msgError);
$(this).val($(this).data("val"));
$(this).focus();
return;
}
var idValue = $("#" + $(this).data("row")).val();
var that = this;
var callback = function () {
$(that).data("val", $(that).val());
};
grid.save(idValue, $(this).data("col"), $(this).val(), callback);
});
}
Grid.prototype.checkField = function (fieldName, fieldValue) {
var valid=true;
$.each(this.fieldsValidators.Where("['fieldName']=='" +
fieldName + "'"), function (index, item) {
if (!item.checkValidateFunc(fieldValue)) {
valid = false;
}
});
return valid;
}
Grid.prototype.save = function (id, fieldName, fieldValue,callback) {
var data = { id: id, fieldName: fieldName, fieldValue: fieldValue };
$.ajax({
url: this.controller + "/" + this.action
, type: "POST"
, data: data
, cache: false
, success: function (node) {
if (node.success) {
if (callback != null) callback();
}
}
});
}
Server Side: The Controller
It uses reflection, so that one action works for any field of entity.
[HttpPost]
public JsonResult UpdateField(int id, string fieldName, string fieldValue)
{
EntityBD entity;
entity= db.EntityBD.First(e => e.Id == id);
if (entity== null) return Json(new { success = false });
entity.GetType().GetProperty(fieldName).SetValue(entity, fieldValue);
db.Entry(entity).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true });
}
Screenshoot