I have previously shared a Beta version of the cascade search box query (although this query method is too unfriendly, it has to begin and end with it ). The link is as follows:Technical implementation-a Demo that links menus and maintains data.
In the above article, I only gave a detailed explanation of the technical implementation ideas, and did not consider its practicality and universality. At the same time, because I have little business logic here, I did not encapsulate it in time. We hope you will understand the delay.
In the Demo above, if you want to use this function, you must implement the following in the background:
// Inject the corresponding service private ICommonServiceBean evaluateInfoService;/*** @ MethodName: getTeacherByCollegeId * @ Description: query the instructor * @ param collegeId * @ return */public String getTeacherByCollegeId (String collegeId) {String out = ""; List
Teachers = evaluateInfoService. findTeacherByCollegeId (collegeId); if (teachers! = Null & teachers. isEmpty () {out = "[{\" id \ ": \" "+" "+" \ ", \" name \": \ "" + "-- select --" + "\"}, "; for (int I = 0; I <teachers. size (); I ++) {out + = "{\" id \ ": \" "+ teachers. get (I ). getTeacherId () + "\", \ "name \": \ "" + teachers. get (I ). getTeacherName () + "\"}, ";}out = out. substring (0, out. length ()-1) + "]";} return out ;}
The above code is used to query teachers based on the school Id. If you want to query courses based on the instructor Id, you need to write a method similar to the above method, and the code is highly repetitive, code reuse is not implemented. It is difficult to modify, which is not conducive to unified management.
The following is my encapsulation. The central idea of this encapsulation is to use the Java reflection mechanism to retrieve Java objects from generics.
Private ICalendarServiceBean calendarService; // inject the corresponding service private ICommonServiceBean evaluateInfoService;/*** @ MethodName: getChildByFatherId * @ Description: query sub-level information by parent Id * @ param id parent Id * @ param evaluateInfoService incoming injection Service * @ return concatenates a complete String */public String getChildByFatherId (String id, ICommonServiceBean evaluateInfoService) {return getChildByFatherId (id, evaluateInfoService, "id", "name");}/*** @ MethodName: getChildByFatherId * @ Description: query sub-level information based on the parent Id * @ param id parent Id * @ param evaluateInfoService passed in the injected Service * @ param oId Object id attribute name, the default value is the name attribute name in the id * @ param oName object. The default value is name * @ return, which Concatenates the complete String */public String getChildByFatherId (String id, ICommonServiceBean evaluateInfoService, String oId, string oName) {String out = ""; List
InfoLists = evaluateInfoService. findTeacherByCollegeId (id); if (infoLists! = Null & infoLists. isEmpty () {out = "[{\" id \ ": \" "+" "+" \ ", \" name \": \ "" + "-- select --" + "\"}, "; for (int I = 0; I <infoLists. size (); I ++) {try {// obtain the Field ids = infoLists element named "id" in the object. get (I ). getClass (). getDeclaredField (oId); // obtain the Field names = infoLists element in the object named "name. get (I ). getClass (). getDeclaredField (oName); if (! Ids. isAccessible () {ids. setAccessible (true);} if (! Names. isAccessible () // determines whether the object can access {names. setAccessible (true); // set to accessible} String strId = ids. get (infoLists. get (I )). toString (); String strName = names. get (infoLists. get (I )). toString (); out + = "{\" id \ ": \" "+ strId +" \ ", \" name \": \ "" + strName + "\"}, ";} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (SecurityException e1) {e1.printStackTrace ();} catch (NoSuchFieldException e1) {e1.printStackTrace ();} out = out. substring (0, out. length ()-1) + "]" ;}} return out ;}
With the encapsulation here, we only need:
/*** @ MethodName: getTeacherByCollegeId * @ Description: query the instructor by the school Id * @ param collegeId * @ return */public String getTeacherByCollegeId (String collegeId) {return getChildByFatherId (collegeId, evaluateInfoService);}/*** @ MethodName: getCourseByTeacherId * @ Description: Query course information based on the instructor Id * @ param teacherId * @ return */public String getCourseByTeacherId (String teacherId) {return getChildByFatherId (teacherId, evaluateInfoService, "teacherId", "teacherName ");}
The above is my encapsulation of the background implementation. You can still optimize the front-end JavaScript call and write more beautiful code.
Through reflection, we encapsulate unchanged things and embrace changes. People say, "Reflection and reflection make programmers happy ." Are you happy today?