您好,登錄后才能下訂單哦!
這篇文章主要介紹了ASP.NET MVC如何異步獲取和刷新ExtJS6 TreeStore,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
從數(shù)據(jù)庫獲取構(gòu)造樹結(jié)構(gòu)是ExtJS TreePanel的核心技術(shù),常用方法是TreeStroe里配置proxy,這種方式的root成了一個不受控制的節(jié)點(diǎn)。
TreeStroe的root實(shí)際是一個層疊json數(shù)據(jù),大部分情況是直接寫一些簡單數(shù)據(jù),但在實(shí)際應(yīng)用中必定是要從數(shù)據(jù)庫讀取的。我的方法是先用Ext.Ajax.request獲取root數(shù)據(jù)形成TreeStroe。定義一個全局的TreeStroe名字是mTreeStore,用Ext.Ajax.request獲得root數(shù)據(jù)。TreeStoreRefresh函數(shù)與此類似,將mTreeStore的root換為新值。TreePanel的rootVisible屬性必須為true,增加一個節(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ù)器錯誤', '數(shù)據(jù)處理錯誤原因:\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ù)器錯誤', '數(shù)據(jù)處理錯誤原因:\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); }
下面是后臺C#代碼
定義一個TreeNode類,包含TreePanel節(jié)點(diǎn)固有的一些屬性,也可以任意擴(kuò)充,利用這個可以自定義許多附加數(shù)據(jù),如我在里面定義Level表示節(jié)點(diǎn)的級別。
[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)的刷新。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ASP.NET MVC如何異步獲取和刷新ExtJS6 TreeStore”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。