ASP.NET MVC異步獲取和刷新ExtJS6 TreeStore
從數(shù)據(jù)庫(kù)獲取構(gòu)造樹(shù)結(jié)構(gòu)是ExtJS TreePanel的核心技術(shù),常用方法是TreeStroe里配置proxy,這種方式的root成了一個(gè)不受控制的節(jié)點(diǎn)。
TreeStroe的root實(shí)際是一個(gè)層疊json數(shù)據(jù),大部分情況是直接寫一些簡(jiǎn)單數(shù)據(jù),但在實(shí)際應(yīng)用中必定是要從數(shù)據(jù)庫(kù)讀取的。我的方法是先用Ext.Ajax.request獲取root數(shù)據(jù)形成TreeStroe。定義一個(gè)全局的TreeStroe名字是mTreeStore,用Ext.Ajax.request獲得root數(shù)據(jù)。TreeStoreRefresh函數(shù)與此類似,將mTreeStore的root換為新值。TreePanel的rootVisible屬性必須為true,增加一個(gè)節(jié)點(diǎn)單擊事件顯示節(jié)點(diǎn)的信息。
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('服務(wù)器錯(cuò)誤', '數(shù)據(jù)處理錯(cuò)誤原因:\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('服務(wù)器錯(cuò)誤', '數(shù)據(jù)處理錯(cuò)誤原因:\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: '基礎(chǔ)數(shù)據(jù)字典', 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("節(jié)點(diǎn)信息", message); }
下面是后臺(tái)C#代碼
定義一個(gè)TreeNode類,包含TreePanel節(jié)點(diǎn)固有的一些屬性,也可以任意擴(kuò)充,利用這個(gè)可以自定義許多附加數(shù)據(jù),如我在里面定義Level表示節(jié)點(diǎn)的級(jí)別。
[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 = "基礎(chǔ)數(shù)據(jù)字典", 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函數(shù)中斷可以監(jiān)視到更多的信息:
最后的運(yùn)行效果:
然后更改數(shù)據(jù)表里的數(shù)據(jù),點(diǎn)“刷新”就實(shí)現(xiàn)了TreePanel節(jié)點(diǎn)的刷新。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- .NET 6 中的隱式命名空間引用
- .NET6中哈希算法的簡(jiǎn)化用法的實(shí)現(xiàn)
- CefSharp v62修改方法(支持.net4.0)
- ASP.NET Core項(xiàng)目配置教程(6)
- ASP.NET Core部署前期準(zhǔn)備 使用Hyper-V安裝Ubuntu Server 16.10
- ASP.NET MVC5+EF6+EasyUI 后臺(tái)管理系統(tǒng)(81)-數(shù)據(jù)篩選(萬(wàn)能查詢)實(shí)例
- ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng) 微信公眾平臺(tái)開(kāi)發(fā)之資源環(huán)境準(zhǔn)備
- ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng) 微信公眾平臺(tái)開(kāi)發(fā)之消息管理
- .NET 6 即將到來(lái)的新特性 隱式命名空間引用
相關(guān)文章
禁用aspx頁(yè)面的客戶端緩存(防止頁(yè)面被修改)
默認(rèn)情況下,IE打開(kāi)一個(gè)網(wǎng)頁(yè),會(huì)在本地進(jìn)行緩存,在某些時(shí)候也會(huì)帶來(lái)了弊端,比如修改信息的頁(yè)面等等因?yàn)閁RL并沒(méi)有改變,所以IE會(huì)讀取本地緩存,這種情況特別容易出現(xiàn)在彈出對(duì)話框或窗口進(jìn)行修改的方式感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02asp.net XMLHttpRequest實(shí)現(xiàn)用戶注冊(cè)前的驗(yàn)證
用戶注冊(cè)前的驗(yàn)證,提高用戶體驗(yàn)。2009-10-10asp.net下 jquery jason 高效傳輸數(shù)據(jù)
jquery jason 高效傳輸數(shù)據(jù)轉(zhuǎn)自網(wǎng)上稍有修改2009-03-03.NET Core自定義項(xiàng)目模板的全過(guò)程
這篇文章主要給大家介紹了關(guān)于.NET Core自定義項(xiàng)目模板的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04ASP.NET泛型一之泛型簡(jiǎn)介與基本語(yǔ)法
這篇文章介紹了ASP.NET泛型的簡(jiǎn)介與基本語(yǔ)法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08驗(yàn)證一個(gè)ASP.NET應(yīng)用程序和頁(yè)面的生命周期的實(shí)現(xiàn)代碼
我們知道ASP.NET Page的生命周期實(shí)際上是ASP.NET Application的生命周期的一部分。這個(gè)周期經(jīng)歷了HTTP Module => HTTP Handler => ASP.NET Page => Http Module這樣一個(gè)過(guò)程2012-04-04ASP.NET?Core實(shí)時(shí)庫(kù)SignalR簡(jiǎn)介及使用
這篇文章介紹了ASP.NET?Core實(shí)時(shí)庫(kù)SignalR簡(jiǎn)介及使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Asp.net中時(shí)間格式化的6種方法詳細(xì)總結(jié)
數(shù)據(jù)控件綁定時(shí)格式化日期方法/用DataBinder.Eval進(jìn)行數(shù)據(jù)綁定時(shí)/直接用ToString方法轉(zhuǎn)換日期顯示格式/用String類轉(zhuǎn)換日期顯示格式等等,感興趣的你了解下哦,或許對(duì)你學(xué)習(xí)時(shí)間格式化有所幫助2013-02-02