欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC异步获取和刷新ExtJS6 TreeStore

程序员文章站 2022-06-14 15:24:51
从数据库获取构造树结构是extjs treepanel的核心技术,常用方法是treestroe里配置proxy,这种方式的root成了一个不受控制的节点。 treestr...

从数据库获取构造树结构是extjs treepanel的核心技术,常用方法是treestroe里配置proxy,这种方式的root成了一个不受控制的节点。

treestroe的root实际是一个层叠json数据,大部分情况是直接写一些简单数据,但在实际应用中必定是要从数据库读取的。我的方法是先用ext.ajax.request获取root数据形成treestroe。定义一个全局的treestroe名字是mtreestore,用ext.ajax.request获得root数据。treestorerefresh函数与此类似,将mtreestore的root换为新值。treepanel的rootvisible属性必须为true,增加一个节点单击事件显示节点的信息。

var mtreestore = null;
ext.ajax.request({
  async: false,
  url: '/api/basicdata_api/getbasictablestreesource',
  method: 'get',
  success: function (response, options)
  {
   var treeroot = ext.decode(response.responsetext);
   mtreestore = ext.create('ext.data.treestore',
   {
    root: treeroot
   });
  },
  failure: function (response, options)
  {
   //var responsearray = ext.decode(response.responsetext);
   ext.msg.alert('服务器错误', '数据处理错误原因:\n\r' + response.responsetext);
  }
});

function treestorerefresh()
{
 ext.ajax.request({
  async: false,
  url: '/api/basicdata_api/getbasictablestreesource',
  method: 'get',
  success: function (response, options)
  {
   var treeroot = ext.decode(response.responsetext);
   if (mtreestore != null)
   {
    mtreestore.setroot(treeroot);
   }
  },
  failure: function (response, options)
  {
   //var responsearray = ext.decode(response.responsetext);
   ext.msg.alert('服务器错误', '数据处理错误原因:\n\r' + response.responsetext);
  }
 });
}

ext.define('treetoolbarcls', {
 extend: 'ext.toolbar.toolbar',
 padding:'0 0 0 0',
 items: [{
  text: '刷新',
  iconcls: 'refresh',
  handler: treestorerefresh,
  height: 30,
  width: 65
 }]
});

ext.define('u1treecls',
{
 extend: 'ext.tree.panel',
 xtype: 'u1tree_xtype',
 //title: '基础数据字典',
 rootvisible: true,
 width: 300,
 store: mtreestore,
 scrollable: true,
 tbar:ext.create('treetoolbarcls'),
 listeners:
 {
  itemclick: nodeclick
 }
});

function nodeclick(node, record, item, index, e, eopts)
{
 if (typeof (record.data) == "undefined")
 {
  return;
 }
 var message = ext.string.format('level={0}<br/>state={1}', record.data.level, record.data.state);
 ext.msg.alert("节点信息", message);
}

下面是后台c#代码

定义一个treenode类,包含treepanel节点固有的一些属性,也可以任意扩充,利用这个可以自定义许多附加数据,如我在里面定义level表示节点的级别。

 [authorize]
 [routeprefix("api/basicdata_api")]
 public class basicdata_apicontroller : apicontroller
 {
  [route("getbasictablestreesource")]
  public httpresponsemessage getbasictablestreesource(string condition = null)
  {
   list<treenode> lstf = new list<treenode>();
   zydadonet z = zydadonet.instance();
   string s1 = "select tablename,title from basedatatables order by tablename";
   string sqltext = s1;
   datatable dt1;
   string errmes;
   z.sql2dtreadonly(s1, out dt1, null, out errmes);
   treenode tnd;
   foreach (datarow drx in dt1.rows)
   {
    tnd = new treenode
    {
     id = drx["tablename"].tostring(),
     text = drx["title"].tostring(),
     level = 1,
     iconcls = "table_6",
     state = drx["tablename"].tostring() + " ok",
     leaf = true
    };
    lstf.add(tnd);
   }
   treenode root = new treenode
   {
    text = "基础数据字典",
    expanded = false,
    iconcls = "folder_close",
    level = 0,
    state = "rootoftree",
    leaf = true
   };
   if (lstf.count > 0)
   {
    root.expanded = true;
    root.leaf = false;
    root.iconcls = "folder_open";
    root.children = lstf;
   }

   string jsonstr;
   jsonstr = jsonconvert.serializeobject(root);
   httpresponsemessage response = request.createresponse(httpstatuscode.ok, "value");
   response.content = new stringcontent(jsonstr, encoding.getencoding("utf-8"), "application/json");
   response.headers.cachecontrol = new cachecontrolheadervalue()
   {
    maxage = timespan.fromminutes(10)
   };
   return response;
  }
 }

 internal class treenode
 {
  public string id { get; set; }
  public string text { get; set; }
  public string iconcls { get; set; }
  public string state { get; set; }
  public bool leaf { get; set; }
  public int level { get; set; }
  public bool expanded { get; set; }
  public list<treenode> children { get; set; }
 }

在nodeclick函数中断可以监视到更多的信息:

ASP.NET MVC异步获取和刷新ExtJS6 TreeStore

最后的运行效果:

ASP.NET MVC异步获取和刷新ExtJS6 TreeStore

然后更改数据表里的数据,点“刷新”就实现了treepanel节点的刷新。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。