CMS: Document Management View (4)

Source: Internet
Author: User

Now you can edit the category. I used to load data with a model, but now it is easier to load data with a form. Switch to the article management controller, add the onCategoryEdit method, and load data using forms. The Code is as follows: onCategoryEdit: function () {var me = this, tree = me. view. down ("treepanel"), rs = tree. getSelectionModel (). getSelection (); if (rs. length> 0) {rs = rs [0]; if (rs. data. id> 10000) {var win = SimpleCMS. view. content. categoryEdit; win. form. getForm (). url = "/Category/Edit"; win. setTitle ("Edit Article category"); win. form. load ({url: "Category/Details", params: {id: rs. data. Id}, success: function (form, action) {this. show () ;}, failure: SimpleCMS. formSubmitFailure, scope: win});} else {Ext. msg. alert ("info", "category" + rs. data. text + "" cannot be edited. ") ;}}, If the category id is greater than 10000, You can edit the category. Therefore, you must add a judgment here. After setting the submission address and title of the category editing window, call the load method to load data for the form. If yes, a window is displayed. Pay attention to setting the scope here. Here we set the scope to win. In this way, in the success method, the this pointer points to the window. Switch to the Category controller to complete the Details method. According to the example in section 12.5.2, return a JSON object in data. The keyword in the object is the name of the field in the form, which is quite simple. The Code is as follows: [AjaxAuthorize (Roles = "normal user, system administrator")] publicJObject Details () {int id = 0; int. tryParse (Request ["id"], outid); try {var q = dc. t_Category.SingleOrDefault (m => m. categoryId = id); if (q = null) {returnMyFunction. writeJObjectResult (false, 0, "category has been deleted or does not exist. ", Null);} else {return new JObject (newJProperty (" success ", true), new JProperty (" data ", new JObject (newJProperty (" CategoryId ", q. categoryId), newJProperty ("Content", q. content), newJProperty ("Image", q. image), newJProperty ("Title", q. title), newJProperty ("SortOrder", q. sortOrder), newJProperty ("ParentId", q. parentId = null? -1: q. parentId);} catch (Exception e) {returnMyFunction. writeJObjectResult (false, 0, e. message, null) ;}} note the ParentId here. If it is null, it must be set to-1 to correspond to the root node. The Edit button is still disabled. Therefore, you must listen to the selectionchange event of the category tree. Enable the edit and delete buttons when a node is selected and the node id is greater than 10000. Switch to the article management controller, first in me. add a statement bound to selectionchange on the control statement. The Code is as follows: me. view. down ("treepanel "). on ("selectionchange", me. onTreeSelect, me); then write the onTreeSelect method. Pay attention to the value assignment of setDisabled. The id value should be taken into account. The Code is as follows: onTreeSelect: function (model, sels) {var me = this, id = 0; if (sels. length> 0) {id = sels [0]. data. id;} me. getCategoryEdit (). setDisabled (sels. length = 0 | id <= 10000); me. getCategoryDelete (). setDisabled (sels. length = = 0 | id <= 10000);} Here, if there is a choice, the id value is obtained, and then in the setDisabled method, if the selected record length is equal to 0, to disable the button, or to disable the button if the id value is less than 10000. Now you can click the edit button to edit the category. The rest is the background code that completes the editing operation. This is slightly different from the Add method. The specific code is as follows: [HttpPost] [AjaxAuthorize (Roles = "regular user, system administrator")] publicJObject Edit (CategoryModel model) {bool success = false; JObject errors = new JObject (); if (ModelState. isValid) {try {var q = dc. t_Category.SingleOrDefault (m => m. categoryId = model. categoryId); if (q = null) {returnMyFunction. writeJObjectResult (false, 0, "the record to be modified has been deleted or does not exist. ", Null);} else {string old_fullpath = q. fullPath; q. content = model. content; q. title = model. title; q. sortOrder = model. sortOrder; if (model. parentId & gt; 10000 & dc. t_Category.Select (m => m. categoryId ). contains (model. parentId) {q. parentId = model. parentId;} else {q. parentId = null;} dc. saveChanges (); success = true; returnMyFunction. writeJObjectResult (true, 0, "", new JArray (new JObject (newJPrope Rty ("id", q. categoryId), newJProperty ("text", q. title), newJProperty ("parentId", q. parentId), newJProperty ("fullpath", q. parentId = null? "": Q. parent. fullPath);} catch (Exception e) {returnMyFunction. writeJObjectResult (false, 0, e. message, null) ;}} else {MyFunction. modelStateToJObject (ModelState, errors);} returnMyFunction. writeJObjectResult (success, errors);} note that if the parent id is not greater than 10000 or does not contain this id in the category database, set the parent id to null.Note: The trg_CategoryUpdate trigger in the T_Category table of the database has an error. You need to block the "set nocount on;" at the beginning to update fullpath and HierarchyLevel normally.Now, the editing function is complete. The deletion function is continued below, which is not much different from the code for deleting the file directory. Switch to the article management controller and complete the onCategoryDelete method. The Code is as follows: onCategoryDelete: function () {var me = this, tree = me. view. down ("treepanel"), rs = tree. getSelectionModel (). getSelection (); if (rs. length> 0) {rs = rs [0]; if (rs. data. id <= 10000) {Ext. msg. alert ("delete category", "category" "+ rs. data. text + "" cannot be deleted! "); Return;} var content =" are you sure you want to delete the category "+ rs. data. text + ""? <Br/> <p style = 'color: red'> Note: All sub-classes under this category will be deleted, and articles of this category and its sub-classes will be moved to unclassified. </P> "; Ext. msg. confirm ("delete category", content, function (btn) {if (btn = "yes") {var rs = this. getSelectionModel (). getSelection (); if (rs. length> 0) {rs = rs [0]; rs. remove (); this. store. sync ({success: function (e, opt) {var me = this; me. store. commitChanges (); me. view. select (0); me. view. focus (false) ;}, failure: function (e, opt) {var me = this; me. store. rejectChanges () Ext. msg. alert ("error occurred", e. exceptio Ns [0]. error) ;}, scope: tree}) ;}}, tree) }}. now, switch to the Category controller to complete the Delete method. In the method, you must first use fullpath to search all sub-classes at a time and set the state value to 1. Find the articles they contain and set the CategoryId of these articles to 10000. Finally, we can return the complete submitted data. The Code is as follows: [AjaxAuthorize (Roles = "normal user, system administrator")] publicJObject Delete () {bool success = false; string msg = ""; JArray ja = null; try {string data = Request ["data"]? ""; If (string. IsNullOrEmpty (data) {msg = "incorrect data submission. ";} Else {ja = JArray. parse (data); if (ja. count> 0) {JObject jo = (JObject) ja [0]; var q = dc. t_Category.SingleOrDefault (m => m. categoryId = (int) jo ["id"]); if (q! = Null) {q. state = 1; var sub = dc. t_Category.Where (m => m. fullPath. startsWith (q. fullPath); foreach (var c in sub) {c. state = 1;} var content = dc. t_Content.Where (m => sub. select (n => n. categoryId ). contains (m. categoryId); foreach (var c in content) {c. categoryId = 10000;} dc. saveChanges (); success = true;} else {msg = "This category has been deleted or does not exist. ";}} Else {msg =" incorrect data submission. ";}} Catch (Exception e) {msg = e. message;} returnHelper. myFunction. writeJObjectResult (success, 0, msg, ja);} In addition to processing data, the entire processing process is basically the same. data is obtained and converted from data first, then obtain data processing from the database. The remaining minor problem is to select a default node when the Tree is displayed, and this should be solved in solution to the problem in Ext JS 4.1 Tree selecting the first line during refresh, the Code is as follows: me. view. down ("treepanel "). getView (). on ("refresh", function () {var tree = this. view. down ("treepanel"); var node = tree. getRootNode (). firstChild; if (node) {tree. getView (). select (node) ;}}, me); now, the document classification operation is complete. Next, we will continue to complete article management. Source code: http://vdisk.weibo.com/s/iflJg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.