java - Tapestry updating DAO from editable grid with AJAX -


i'm trying make following work in tapestry.

i have dictionary<string, dictionary<string, object>> contains data.

i'm trying accomplish have 1 drop-down menu (select component) contains keys outer dictionary.

when selection changes, grid should updated keys , values selected sub-dictionary.

for example:

dictionary<string, dictionary<string, object>> dictionaries = new hashtable<string, dictionary<string, object>>();  dictionary<string, object> dict1 = new hashtable<string, object>(); dictionary<string, object> dict2 = new hashtable<string, object>();  dict1.put("k1", "d1v1"); dict1.put("k2", "d1v2");  dict2.put("k1", "d2v1"); dict2.put("k2", "d2v2");  dictionaries.put("d1", dict1); dictionaries.put("d2", dict2); 

would kind give me example or push me in right direction?

edit:

i managed first part working, grid contents changing depending on keys outer dictionary selected.

however, i'm having problems updating grid , saving changes:

i using own griddatasource:

public class entitydatasource<t> implements griddatasource {      private class<t> persistentclass;      protected list<t> data;     protected int count = -1;     protected int startindex = 0;       public entitydatasource(class<t> persistentclass, list<t> data) {         this.persistentclass = persistentclass;         this.data = data;     }      public static final <t> entitydatasource<t> create(             class<t> persistentclass, list<t> data) {         return new entitydatasource<t>(persistentclass, data);     }      public int getavailablerows() {         return this.data.size();     }      public void prepare(int startindex, int endindex,             list<sortconstraint> sortconstraints) {         this.startindex = startindex;         this.data = this.data.sublist(startindex, endindex + 1);     }      public object getrowvalue(int index) {         return this.data.get(index - this.startindex);     }      @suppresswarnings("rawtypes")     public class getrowtype() {         return this.persistentclass;     }  } 

my grid looks this:

<t:form t:id="configselectform">     <t:select t:id="storagekeyselecter"           t:model="storagekeymodel"           t:value="storagekey"           zone="configzone" /> </t:form>  <t:zone t:id="configzone" id="configzone">     <t:form t:id="configreviewform">         <table width="100%">             <t:grid t:source="configurationentrysource"                     t:add="update, delete"                     t:row="configurationentry"                     t:mixins="disablegridsorting"                     t:include="configkey, configvalue"                     t:encoder="configurationentryencoder">                 <p:configvaluecell>                     <input t:id="value" t:type="textfield" t:value="configurationentry.configvalue"                            t:validate="required" t:context="configurationentry.configvalue" />                 </p:configvaluecell>                  <p:updatecell>                     <t:actionlink t:id="update" zone="configzone" context="[configurationentry.configkey, configurationentry.configvalue]">update</t:actionlink>                 </p:updatecell>                  <p:deletecell>                     <t:actionlink t:id="delete" zone="configzone" context="configurationentry.configkey">delete</t:actionlink>                 </p:deletecell>              </t:grid>         </table>         <br></br>         <!-- <input type="submit" value="update" class="button" /> -->     </t:form> </t:zone> 

i have managed make delete link work, however, cannot seem update value arbitrary key.

when click link update, value text field not 1 being passed:

public object onactionfromupdate(string configkey, string configvalue) {      // stuff here      return request.isxhr() ? configzone.getbody() : null; } 

the configvalue value of entry exist , not 1 i'm trying change to.

is there way value? grid has an arbitrary number of rows.

edit2: more info provided

this tml:

<t:form t:id="configselectform">     <t:select t:id="storagekeyselecter"           t:model="storagekeymodel"           t:value="storagekey"           zone="configzone" /> </t:form>  <br/>  <t:zone t:id="configzone" id="configzone" elementname="div">     <form t:type="form" t:id="configreviewform" id="configreviewform" t:zone="configzone" zone="configzone">         <table width="100%">             <t:grid t:source="configurationentries"                     t:add="update, delete"                     t:row="configurationentry"                     t:mixins="disablegridsorting"                     t:include="configkey, configvalue"                     t:encoder="configurationentryencoder" >                  <p:configvaluecell>                     <input t:id="configvaluetextfield" t:type="textfield" t:value="configurationentry.configvalue"                            t:validate="required" />                 </p:configvaluecell>                  <p:updatecell>                     <t:actionlink id="update" t:id="update" zone="configzone" t:context="[configurationentry.configkey, configurationentry.configvalue]">update</t:actionlink>                 </p:updatecell>                  <p:deletecell>                     <t:actionlink id="delete" t:id="delete" zone="configzone" t:context="configurationentry.configkey">delete</t:actionlink>                 </p:deletecell>              </t:grid>         </table>         <br/>         <input type="submit" value="update" class="button" zone="configzone"/>     </form> </t:zone>  <br/> <br/>  <t:form t:id="addnewentryform">     <table width="100%">         <tr>             <td>                 <input t:id="newentrykey" t:type="textfield" t:value="newentry.configkey"                         t:validate="required" t:context="newentry.configkey" />             </td>              <td>                 <input t:id="newentryvalue" t:type="textfield" t:value="newentry.configvalue"                         t:validate="required" t:context="newentry.configvalue" />             </td>              <td>                 <input type="submit" value="add new entry" zone="configzone"/>             </td>         </tr>     </table> </t:form> 

this java:

public class propertyconfiguration {      @inject     private beanmodelsource beanmodelsource;      @component     private form configreviewform;      @property     private list<configurationentry> configurationentries;      private list<configurationentry> persistententries;      @property     private configurationentry configurationentry = new configurationentry("", "");      @property     private configurationentryencoder configurationentryencoder;      @injectcomponent     private zone configzone;      @injectcomponent     private textfield configvaluetextfield;      @inject     private configurationpersitancedao dao;      private griddatasource datasource;      @inject     private messages messages;      @property     private configurationentry newentry;      @inject     private request request;      @property     @validate("required")     @persist(persistenceconstants.session)     private string storagekey;      private stringselectmodel storagekeysselectmodel;      public valueencoder<configurationentry> getconfigurationentryencoder() {         initconfigurationentityencoder();          return this.configurationentryencoder;     }      public beanmodel<configurationentry> getmodel() {         return beanmodelsource.createdisplaymodel(configurationentry.class, messages);     }      public selectmodel getstoragekeymodel() {         if (storagekeysselectmodel == null) {             storagekeysselectmodel = new stringselectmodel(this.dao.getstoragekeys());         }          return storagekeysselectmodel;     }      private void initconfigurationentityencoder() {         if (this.configurationentryencoder == null) {             this.configurationentryencoder = new configurationentryencoder(dao, storagekey);         }     }      private void initzonedata() {         if (this.storagekey == null) {             this.storagekey = this.dao.getstoragekeys().get(0);         }         initconfigurationentityencoder();        }      public object onactionfromdelete(string configkey) {         system.out.println("deleting from: " + storagekey + " entry: " + configkey);         this.dao.deleteconfigurationentry(storagekey, configkey);          return request.isxhr() ? configzone.getbody() : null;     }      public object onactionfromupdate(string configkey, string configvalue) {         this.dao.updateconfigurationentry(storagekey, configkey, configvalue);          return request.isxhr() ? configzone.getbody() : null;     }      void onactivate(string storagekey) {         initzonedata();         this.newentry = new configurationentry("", "");     }      string onpassivate() {         this.newentry = new configurationentry("", "");         return this.storagekey;     }      object onrefresh() {         return request.isxhr() ? configzone.getbody() : null;     }      object onsuccessfromaddnewentryform() {         string configkey = this.newentry.getconfigkey();         string configvalue = this.newentry.getconfigvalue();          this.dao.addconfigurationentry(storagekey, configkey, configvalue);          return request.isxhr() ? configzone.getbody() : null;     }      void onvalidatefromaddnewentryform() {         return;     }      object onvaluechangedfromstoragekeyselecter(string storagekey) {         this.storagekey = storagekey;         initconfigurationentityencoder();         this.configurationentries = wrap(this.dao.getconfiguration(storagekey));         return configzone.getbody();     }      private void updatechangedconfigurations(list<configurationentry> changedentries) {         (configurationentry changedentry : changedentries) {             string configkey = changedentry.getconfigkey();             string configvalue = changedentry.getconfigvalue();              system.out.println("updated: [" + storagekey + ":" + configkey + ":" + configvalue + "]");              this.dao.updateconfigurationentry(storagekey, configkey, configvalue);         }     }      void onvalidatefromconfigreviewform() {         this.persistententries = wrap(this.dao.getconfiguration(storagekey));         list<configurationentry> tmplist = new arraylist<configurationentry>();          (configurationentry newentry : this.configurationentries) {             (configurationentry oldentry : this.persistententries) {                 system.out.println("newentry: " + newentry.tostring() + " vs. oldentry: " + oldentry.tostring());                 if (oldentry.getconfigkey().equals(newentry.getconfigkey())) {                     if (!oldentry.getconfigvalue().equals(newentry.getconfigvalue())) {                         newentry.setconfigvalue(newentry.getconfigvalue().trim());                         tmplist.add(newentry);                     }                 }             }         }          this.persistententries = tmplist;     }      object onsuccessfromconfigreviewform() {         updatechangedconfigurations(this.persistententries);          return request.isxhr() ? configzone.getbody() : null;     }      /**     *   wraps dictionary entries in instances of configurationentry     */     private list<configurationentry> wrap(         dictionary<string, object> rawconfiguration) {         set<string> keyset = new treeset<string>();         list<configurationentry> wrappedentries = new arraylist<configurationentry>();          enumeration<string> keys = rawconfiguration.keys();          while (keys.hasmoreelements()) {             string key = keys.nextelement();             keyset.add(key);         }          (string key : keyset) {             string value = (string) rawconfiguration.get(key);              configurationentry entry = new configurationentry(key, value);              wrappedentries.add(entry);         }          return wrappedentries;     } } 

for reason, when "update" action link clicked values passed public object onactionfromupdate(string configkey, string configvalue) aren't ones wrote designated textfield, ones fetched database when page rendered.

for example, if had these pairs initially:

key1 => value1 key2 => value2 key3 => value3 

and wanted change value key2 "newvalue2" parameters passed method "key2" , "value2" instead of "key2" , "newvalue2".

the same problem reason mass-update doesn't work. values in configurationentries shapshot values database instead of values written in grid.

so, question how can update db editable grid using ajax concrete example. i've tried many things suggested on , rest of internet, don't seem work , don't know why.

you need use zone parameter.

take @ selectzonedemo.tml , selectzonedemo.java select component's javadoc. gives example of updating zone when select changes.

for more complex interactions, might interested in this


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -