利用attributes将数据库中的所有数据转换成json并绑定到tree中
2013-07-22 11:49
369 查看
#region 将datatable转换成适合tree的json格式
StringBuilder result = new StringBuilder();
StringBuilder sb = new StringBuilder();
//其中idCol用来绑定tree的id,txtCol用来绑定tree的text,rela来表示父节点
public string GetTreeJsonByTable(DataTable table, string idCol, string txtCol, string rela, object pId)
{
result.Append(sb.ToString());
sb.Clear();
if (table.Rows.Count > 0)
{
sb.Append("[");
string filer = string.Format("{0}='{1}'", rela, pId);
DataRow[] rows = table.Select(filer);
if (rows.Length > 0)
{
foreach (DataRow row in rows)
{
sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"state\":\"open\",\"attributes\":{\"DictID\":\"" + row[0] + "\",\"ParentDictCode\":\"" + row[rela] + "\",\"Description\":\"" +
row[4] + "\",\"Note\":\"" + row[5] + "\"}");
if (table.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
{
sb.Append(",\"children\":");
GetTreeJsonByTable(table, idCol, txtCol, rela, row[idCol]);
result.Append(sb.ToString());
sb.Clear();
}
result.Append(sb.ToString());
sb.Clear();
sb.Append("},");
}
sb = sb.Remove(sb.Length - 1, 1);
}
sb.Append("]");
result.Append(sb.ToString());
sb.Clear();
return result.ToString();
}
else
return null;
}
#endregion
前台编辑tree节点
//编辑节点
function edit() {
var t = $('#tt');
var node = t.tree('getSelected');
if (node) {
$('#dlg').dialog('open').dialog('setTitle', '编辑字典');
$('#fm').form('load', {
DictID: node.attributes.DictID,
DictCode: node.id,
Name: node.text,
Description: node.attributes.Description,
ParentDictCode: node.attributes.ParentDictCode,
Note: node.attributes.Note
});
url = 'Ajax/Update_DictHandler.ashx?Name=' + node.text;
}
}
<div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">
数据字典</div>
<form id="fm" method="post" novalidate>
<table>
<div class="fitem">
<tr>
<td>
<label>
字典编码:</label>
</td>
<td>
<input name="DictCode" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
名称:</label>
</td>
<td>
<input name="Name" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
字典描述:</label>
</td>
<td>
<input name="Description">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
上级字典编码:</label>
</td>
<td>
<input name="ParentDictCode" text="123" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
备注:</label>
</td>
<td>
<input name="Note">
</td>
</tr>
</div>
</table>
</form>
</div>
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveDict()">保存</a>
<a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">
取消</a>
</div>
StringBuilder result = new StringBuilder();
StringBuilder sb = new StringBuilder();
//其中idCol用来绑定tree的id,txtCol用来绑定tree的text,rela来表示父节点
public string GetTreeJsonByTable(DataTable table, string idCol, string txtCol, string rela, object pId)
{
result.Append(sb.ToString());
sb.Clear();
if (table.Rows.Count > 0)
{
sb.Append("[");
string filer = string.Format("{0}='{1}'", rela, pId);
DataRow[] rows = table.Select(filer);
if (rows.Length > 0)
{
foreach (DataRow row in rows)
{
sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"state\":\"open\",\"attributes\":{\"DictID\":\"" + row[0] + "\",\"ParentDictCode\":\"" + row[rela] + "\",\"Description\":\"" +
row[4] + "\",\"Note\":\"" + row[5] + "\"}");
if (table.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
{
sb.Append(",\"children\":");
GetTreeJsonByTable(table, idCol, txtCol, rela, row[idCol]);
result.Append(sb.ToString());
sb.Clear();
}
result.Append(sb.ToString());
sb.Clear();
sb.Append("},");
}
sb = sb.Remove(sb.Length - 1, 1);
}
sb.Append("]");
result.Append(sb.ToString());
sb.Clear();
return result.ToString();
}
else
return null;
}
#endregion
前台编辑tree节点
//编辑节点
function edit() {
var t = $('#tt');
var node = t.tree('getSelected');
if (node) {
$('#dlg').dialog('open').dialog('setTitle', '编辑字典');
$('#fm').form('load', {
DictID: node.attributes.DictID,
DictCode: node.id,
Name: node.text,
Description: node.attributes.Description,
ParentDictCode: node.attributes.ParentDictCode,
Note: node.attributes.Note
});
url = 'Ajax/Update_DictHandler.ashx?Name=' + node.text;
}
}
<div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">
数据字典</div>
<form id="fm" method="post" novalidate>
<table>
<div class="fitem">
<tr>
<td>
<label>
字典编码:</label>
</td>
<td>
<input name="DictCode" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
名称:</label>
</td>
<td>
<input name="Name" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
字典描述:</label>
</td>
<td>
<input name="Description">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
上级字典编码:</label>
</td>
<td>
<input name="ParentDictCode" text="123" class="easyui-validatebox" required="true">
</td>
</tr>
</div>
<div class="fitem">
<tr>
<td>
<label>
备注:</label>
</td>
<td>
<input name="Note">
</td>
</tr>
</div>
</table>
</form>
</div>
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveDict()">保存</a>
<a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">
取消</a>
</div>
相关文章推荐
- 利用多叉树将数据库中的层次数据转换成树形结构的JSON字符串
- 从数据库取到数据转换json后,时间格式带上T的解决办法
- php数据库数据转换为js中的json对象,JSON.parse()和JSON.stringify()
- c#将数据库数据转换为json格式(使用extjs,easyui中的datagrid)
- json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)
- 使用java连接MySql,将所有数据转换成Json
- 利用GSON对json数据中的时间类型字符串进行处理,并且进行数据库查询
- 利用python将所有时序NDVI影像转换为DataFrame数据并进行归一化
- C#将datatable生成easyui的绑定tree 的json数据格式
- asp将数据库数据转换成JSON格式
- 将数据库中的数据转换成json格式
- C#将datatable生成easyui的绑定tree 的json数据格式
- 省市县从数据库读出来的list数据转换成json格式的数据
- php数据库数据转换为js中的json对象
- php 利用json_decode强制json数据转换成数组
- 利用python将json数据转换为csv格式的方法
- 【Servlet】在Servlet3.0中利用json+ajax把数据库查询出来的数据推向前台显示,无额外的json解析包
- java根据数据库中的数据 的list 生成 tree 型 json
- java 将数据库中的数据转化为easyUI treegrid json格式
- 利用ashx操作OleDb数据库返回json数据