写在前面
在项目的开发过程中,我们使用最多的是表单的序列化。而有关以Table的序列化成Json的方法不太常见。
在做功能的时候发现,没有提交如何把Table序列化成Json的解决方案。以下,是个人对Table序列化的解决方法。
希望能给遇到同样问题的”童鞋“提供一点思路。
具体操作步骤
前端写法:
<fieldset class="layui-elem-field" style=" 99%;margin: 10px auto;border: 1px solid #009688;border-radius:5px">
<legend>配置信息</legend>
<div class="layui-field-box">
<div class="layui-form">
<div class="layui-btn-group">
<button class="layui-btn" id="bntAddRow"><i class="layui-icon"></i>增行</button>
<button class="layui-btn layui-btn-danger" id="btnDelRow"><i class="layui-icon"></i>删行</button>
</div>
<div style="99%;margin:0 auto">
<table class="layui-table">
<thead>
<tr>
<th><input type="checkbox" lay-filter="allChoose" /></th>
<th>英文字段</th>
<th>中文字段</th>
<th>表名</th>
<th>表别名</th>
<th>是否显示</th>
<th>是否链接</th>
<th>排序</th>
<th>点击主键</th>
<th>是否省略</th>
<th>省略字段</th>
<th>字段表名</th>
<th>界面绑定id</th>
<th>选择器</th>
<th>文本类型</th>
<th>查询类型</th>
<th>权限类型</th>
</tr>
</thead>
<tbody id="bind_List"></tbody>
</table>
</div>
<div class="layui-form" style="text-align: center;">
<button class="layui-btn" id="btnSave">保存</button>
<button class="layui-btn layui-btn-primary" id="btnclose">取消</button>
</div>
</div>
</div>
</fieldset>
//引用Jquery
$(function(){
var Json = SerializeTable("bind_List");
$.post("" + _RequestUrl + ".ashx", { act: "SavePage", Json: Json, opuser: _opuser }, function (msg) {
if (msg > 0) {
parent.layer.msg("保存成功", { icon: 1 });
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
}
else { layer.msg("保存失败", { icon: 2 }); return; }
});
});
//BindDiv 为绑定的Id
function SerializeTable(BindDiv) {
var tb_Json = "[";
//首先,循环行
$("#" + BindDiv + " tr").each(function (i, rows) {
tb_Json += "{";
//其次,循环每一行的列
$.each(rows.cells, function (i, columns) {
//这里去除input标签类型是 checkbox
if (i > 0) {
//判断name值不能位空
if ($(columns).children().attr("name") == "" && $(columns).children().attr("name") == undefined) { }
else { tb_Json += """ + $(columns).children().attr("name") + "":"" + $(columns).children().val() + "","; }
}
});
//去除最后一位逗号
tb_Json = tb_Json.substring(0, tb_Json.length - 1);
tb_Json += '},';
});
tb_Json = tb_Json.substring(0, tb_Json.length - 1);
tb_Json += "]";
return tb_Json;
}
后台接收
public static int SavePower(string Json, string Powerid, string opuser)
{
int result = 0;
using (var conn = ConnectionFactory.MySqlCon())
{
string sql = "";
List<sys_menurolefunc> mrf;
mrf = JsonConvert.DeserializeObject<List<sys_menurolefunc>>(Json);
List<sys_menurolefunc> lstmrf = new List<sys_menurolefunc> { };
//查询是否存在
string query = "select count(*) from sys_menurolefunc where role_id=?ids";
DynamicParameters pr = new DynamicParameters();
pr.Add("?ids", Powerid);
int isHave = Convert.ToInt32(conn.ExecuteScalar(query, pr));
if (isHave > 0)
{
string sqldel = "delete from sys_menurolefunc where role_id=?role_ids";
DynamicParameters prs = new DynamicParameters();
prs.Add("?role_ids", Powerid);
int isHaves = conn.Execute(sqldel, prs);
DalCommon.LogsInfo(opuser, "操作日志", "删除权限", "操作表:sys_menurolefunc;删除角色:" + Powerid);//日志记录
}
foreach (var item in mrf)
{
sys_menurolefunc smrf = new sys_menurolefunc();
smrf.menu_id = item.menu_id;
smrf.func_id = item.func_id;
smrf.role_id = item.role_id;
lstmrf.Add(smrf);
}
using (var trans = conn.BeginTransaction())
{
try
{
sql = "insert into sys_menurolefunc(menu_id,func_id,role_id)";
sql += "values (?menu_id,?func_id,?role_id)";
int count = conn.Execute(sql, lstmrf);
trans.Commit();
result = 1;
DalCommon.LogsInfo(opuser, "操作日志", "分配权限", "操作表:sys_menurolefunc;");//日志记录
}
catch (Exception e)
{
/*回滚事务*/
trans.Rollback();
result = 0;
string error = e.Message;
DalCommon.LogsInfo(opuser, "系统日志", "错误日志", "错误:" + error);//日志记录
throw;
}
}
}
return result;
}