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

所属分类: 网络编程 / ASP.NET 阅读数: 1960
收藏 0 赞 0 分享

从数据库获取构造树结构是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函数中断可以监视到更多的信息:

最后的运行效果:

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

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

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多