string到DataTable
/// <summary>
/// string 到 DataTable
/// </summary>
/// <param name="strdata"></param>
/// <returns></returns>
public static DataTable StringToDataTable(string strdata)
{
if (string.IsNullOrEmpty(strdata))
{
return null;
}
DataTable dt = new DataTable();
string[] strSplit = { "@&@" };
string[] strRow = { "#$%" }; //分解行的字符串
string[] strColumn = { "^&*" }; //分解字段的字符串
string[] strArr = strdata.Split(strSplit, StringSplitOptions.None);
StringReader sr = new StringReader(strArr[0]);
dt.ReadXmlSchema(sr);
sr.Close();
string strTable = strArr[1]; //取表的数据
if (!string.IsNullOrEmpty(strTable))
{
string[] strRows = strTable.Split(strRow, StringSplitOptions.None); //解析成行的字符串数组
for (int rowIndex = 0; rowIndex < strRows.Length; rowIndex++) //行的字符串数组遍历
{
string vsRow = strRows[rowIndex]; //取行的字符串
string[] vsColumns = vsRow.Split(strColumn, StringSplitOptions.None); //解析成字段数组
dt.Rows.Add(vsColumns);
}
}
return dt;
}
//初始化方法
$(function(){
//加载全部节点
$("#tree_menu").tree({
url: '<%=request.getContextPath() %>/manage/menu/getChildren',
animate:true,
lines:true,
onClick: function (node) {
//右侧跳转到指定页面
addTab(node.text,node.menuUrl);
}
});
});
<ul id="tree_menu" class="easyui-tree" > </ul>
[{"id":1,"text":"系统菜单","state":"closed",pid:0},
{"id":2,"text":"管理员","state":"open",pid:1},
{"id":3,"text":"数据字典","state":"open",pid:1}]
$.parser.parse();
$.ajax({
url:my.bp()+'/main/menuaction!createMenu.action',
type:'post',
async: false,
dataType:'json',
success:function(data){
$.each(data, function(i, item){
$('#main_west_accordion').accordion('add',item);
$('#'+item.id).html('<ul id="ctrltree'+item.id+'" style="margin-top: 5px;"></ul>');
$('#ctrltree'+item.id).tree({
url:my.bp()+'/main/menuaction!treeMenu.action?id='+item.id,
});
$('#ctrltree'+item.id).tree({
onLoadSuccess:function(){
$(this).tree('options').url = my.bp()+'/main/menuaction!treeMenu.action';
},
onClick : function(node){
//add(tab)
// console.info(node);
},
onDblClick : function(node){
if (node.state == 'closed'){
$(this).tree('expand', node.target);
}else{
$(this).tree('collapse', node.target);
}
}
});
});
}
});
function convert(rows){
function exists(rows, parentId){
for(var i=0; i<rows.length; i++){
if (rows[i].id == parentId) return true;
}
return false;
}
var nodes = [];
// get the top level nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (!exists(rows, row.parentId)){
nodes.push({
id:row.id,
text:row.name
});
}
}
var toDo = [];
for(var i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){
var node = toDo.shift(); // the parent node
// get the children nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (row.parentId == node.id){
var child = {id:row.id,text:row.name};
if (node.children){
node.children.push(child);
} else {
node.children = [child];
}
toDo.push(child);
}
}
}
return nodes;
}
$('#tt').tree({
url: 'data/tree6_data.json',
loadFilter: function(rows){
return convert(rows);
}
});
[
{"id":1,"parendId":0,"name":"Foods"},
{"id":2,"parentId":1,"name":"Fruits"},
{"id":3,"parentId":1,"name":"Vegetables"},
{"id":4,"parentId":2,"name":"apple"},
{"id":5,"parentId":2,"name":"orange"},
{"id":6,"parentId":3,"name":"tomato"},
{"id":7,"parentId":3,"name":"carrot"},
{"id":8,"parentId":3,"name":"cabbage"},
{"id":9,"parentId":3,"name":"potato"},
{"id":10,"parentId":3,"name":"lettuce"}
]
/// <summary>
/// string 到 DataTable
/// </summary>
/// <param name="strdata"></param>
/// <returns></returns>
public static DataTable StringToDataTable(string strdata)
{
if (string.IsNullOrEmpty(strdata))
{
return null;
}
DataTable dt = new DataTable();
string[] strSplit = { "@&@" };
string[] strRow = { "#$%" }; //分解行的字符串
string[] strColumn = { "^&*" }; //分解字段的字符串
string[] strArr = strdata.Split(strSplit, StringSplitOptions.None);
StringReader sr = new StringReader(strArr[0]);
dt.ReadXmlSchema(sr);
sr.Close();
string strTable = strArr[1]; //取表的数据
if (!string.IsNullOrEmpty(strTable))
{
string[] strRows = strTable.Split(strRow, StringSplitOptions.None); //解析成行的字符串数组
for (int rowIndex = 0; rowIndex < strRows.Length; rowIndex++) //行的字符串数组遍历
{
string vsRow = strRows[rowIndex]; //取行的字符串
string[] vsColumns = vsRow.Split(strColumn, StringSplitOptions.None); //解析成字段数组
dt.Rows.Add(vsColumns);
}
}
return dt;
}
//初始化方法
$(function(){
//加载全部节点
$("#tree_menu").tree({
url: '<%=request.getContextPath() %>/manage/menu/getChildren',
animate:true,
lines:true,
onClick: function (node) {
//右侧跳转到指定页面
addTab(node.text,node.menuUrl);
}
});
});
<ul id="tree_menu" class="easyui-tree" > </ul>
[{"id":1,"text":"系统菜单","state":"closed",pid:0},
{"id":2,"text":"管理员","state":"open",pid:1},
{"id":3,"text":"数据字典","state":"open",pid:1}]
$.parser.parse();
$.ajax({
url:my.bp()+'/main/menuaction!createMenu.action',
type:'post',
async: false,
dataType:'json',
success:function(data){
$.each(data, function(i, item){
$('#main_west_accordion').accordion('add',item);
$('#'+item.id).html('<ul id="ctrltree'+item.id+'" style="margin-top: 5px;"></ul>');
$('#ctrltree'+item.id).tree({
url:my.bp()+'/main/menuaction!treeMenu.action?id='+item.id,
});
$('#ctrltree'+item.id).tree({
onLoadSuccess:function(){
$(this).tree('options').url = my.bp()+'/main/menuaction!treeMenu.action';
},
onClick : function(node){
//add(tab)
// console.info(node);
},
onDblClick : function(node){
if (node.state == 'closed'){
$(this).tree('expand', node.target);
}else{
$(this).tree('collapse', node.target);
}
}
});
});
}
});
function convert(rows){
function exists(rows, parentId){
for(var i=0; i<rows.length; i++){
if (rows[i].id == parentId) return true;
}
return false;
}
var nodes = [];
// get the top level nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (!exists(rows, row.parentId)){
nodes.push({
id:row.id,
text:row.name
});
}
}
var toDo = [];
for(var i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){
var node = toDo.shift(); // the parent node
// get the children nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (row.parentId == node.id){
var child = {id:row.id,text:row.name};
if (node.children){
node.children.push(child);
} else {
node.children = [child];
}
toDo.push(child);
}
}
}
return nodes;
}
$('#tt').tree({
url: 'data/tree6_data.json',
loadFilter: function(rows){
return convert(rows);
}
});
[
{"id":1,"parendId":0,"name":"Foods"},
{"id":2,"parentId":1,"name":"Fruits"},
{"id":3,"parentId":1,"name":"Vegetables"},
{"id":4,"parentId":2,"name":"apple"},
{"id":5,"parentId":2,"name":"orange"},
{"id":6,"parentId":3,"name":"tomato"},
{"id":7,"parentId":3,"name":"carrot"},
{"id":8,"parentId":3,"name":"cabbage"},
{"id":9,"parentId":3,"name":"potato"},
{"id":10,"parentId":3,"name":"lettuce"}
]