Easyui dynamically generated Column properties

Source: Internet
Author: User

Requirements: Dynamically generate a DataGrid table from the Model class property

1. Define Columntitle Annotation Class

Package Com.mrchu.annotation;import Java.lang.annotation.retention;import Java.lang.annotation.RetentionPolicy; Import java.lang.annotation.target;/** * Column header Note class *  * @author Mrchu * @version 1.0 * @date January 12, 2015 * * @Target ({java.la Ng.annotation.ElementType.FIELD}) @Retention (retentionpolicy.runtime) public @interface columntitle {/** column header */string Title ();/** column sort */int sort ();}

2. Add annotations to the model attribute

Package Com.mrchu.model;import Com.mrchu.annotation.columntitle;public class User {@ColumnTitle (title = "Name", sort = 1) private string name, @ColumnTitle (title = "Gender", sort = 2) private string sex, @ColumnTitle (title = "Age", sort = 3) Private STR ing age;public String getName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;}}

3. Model attribute parsing

Package com.mrchu.controller;/** * Controller * * @author Mrchu * @version 1.0 * @date January 8, 2015 */public class Controller {/** Freeze column field collection */private static final list<string> frozenfields = arrays.aslist (new string[]{"name"});/** field Style property collection */@SuppressWarnings ("serial") private static final map<string, object> alignstyles = new hashmap<string, Object > () {put ("sex", "center");}};/ * * Initialize FORM * @param request * HttpServletRequest * @param response * HttpServletResponse * @param className * class's Fully qualified name * @p Aram Session * HttpSession * @throws ClassNotFoundException * Class load exception, loader did not find this class */@RequestMapping (value = "/inittable", meth OD = requestmethod.post) public void inittable (HttpServletRequest request, httpservletresponse response, String ClassName, HttpSession session) throws ClassNotFoundException {map<string, object> data = new hashmap<string, Ob Ject> (); class<? Extends object> clazz = Class.forName (className); if (clazz! = null) {field[] fields = CLAZZ.GETDECLAREDFIelds (); if (Fields! = NULL && fields.length! = 0) {list<map<string, object>> columns = new ARRAYLIST&L T Map<string, object>> (); list<map<string, object>> frozencolumns = new arraylist<map<string, object>> (); for (Field Field:fields) {if (Field.isannotationpresent (Columntitle.class)) {Columntitle Columntitle = field.getAnnotation ( Columntitle.class); if (columntitle! = null) {map<string, object> column = new hashmap<string, object> (); Column.put ("Field", Field.getname ()); Column.put ("title", Columntitle.title ()); if (Alignstyles.containskey ( Field.getname ())) {Column.put ("align", alignstyles.get (Field.getname ()));} if (!frozenfields.contains (Field.getname ())) {columns.add (column);} else {frozencolumns.add (column);}}} list<object> Columnarray = new arraylist<object> () columnarray.add (columns);d ata.put ("columns", Columnarray); list<object> Frozencolumnarray = new arraylist<object> (); Frozencolumnarray.add (FrozEncolumns);d ata.put ("FrozenColumns", Frozencolumnarray);}} Writejson (Request, response, data);}}

The data format that the Inittable method eventually returns:

[[        {field: ' Name ', title: ' Name '},         {field: ' Sex ', Title: ' Gender ', align: ' center '},         {field: ' Age ', Title: ' Ages '}]
4. Page rendering

$.ajax ({type: ' post ', url: ' Controller/inittable?classname=com.mrchu.model.user ', success:function (map) {var data = Jquery.parsejson (map); $ (' #grid '). DataGrid ({url: ' Data.json ', IDfield: ' id ', Columns:data.columns,frozencolumns: Data.frozencolumns});});

5, this way can realize the DataGrid table column properties of the dynamic switch, the code is truncated.







Easyui dynamically generated Column properties

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.