Js dynamic add delete background data of sample code

  • 2020-03-29 23:57:18
  • OfStack

Environment description: like you generally in the forum published articles, may be with attachments, the number of attachments is you manually add and delete!!

// index of growth
Var itemIndex = 1000;
/ / the number of
Var counter = 0;
/ / template
Var itemTemplate = ' ';
ItemTemplate + = '< Div id = "item_ # itemIndex#" style = "width: 100%;" > ';
ItemTemplate + = '    < Span style = "width: 80 px" > Form field < / span> ';
ItemTemplate + = '    < Input type="text" name="flowFormFieldCfgElement(#itemIndex#). Name "style="width: 205px;" / > ';
ItemTemplate + = '    Value types < Select name = "flowFormFieldCfgElement (# itemIndex#) valueType" > ';
ItemTemplate + = '            < The option value = "Java. Lang. String" selected> String < / option> ';
ItemTemplate + = '            < The option value = "Java. Lang. Integer" > Numbers (integers) < / option> ';
ItemTemplate + = '            < The option value = "Java. Util. Date" > Date (yyyy-mm-dd) < / option> ';
ItemTemplate + = '    < / select> ';
ItemTemplate + = '    < Input type="button" class="btnDelItem" onclick="delItem(#itemIndex#)" value=" delete "> ';
ItemTemplate + = '< / div> ';

/ / add
The function addItem () {
Var s = itemtemplate.replace (/#itemIndex#/g, itemIndex);
$(" # divFormFields "). Append (s);
ItemIndex + +;
Counter + +;
}

/ / delete
The function delItem (index) {
$(" # item_ "+ index), remove ();
Counter = counter -1;
}

 


Public class FlowFormConfigActionForm extends ActionForm {
Private int id;
Private String name;
Private String processName;

Private String formFillTemplatePath;
Private String formShowTemplatePath;
Private Map< String, FlowformFieldConfig> FlowFormFieldCfgMap = new TreeMap< String, FlowformFieldConfig> (a);


Public FlowformFieldConfig getFlowFormFieldCfgElement (String key) {
If (! FlowFormFieldCfgMap. Either containsKey (key)) {
FlowFormFieldCfgMap. Put (key, new FlowformFieldConfig ());
}
Return flowFormFieldCfgMap. Get (key);
}

* U * * * * *

 

Public class FlowformFieldConfig {
Private int id;
Private String name;
Private Class< ? > ValueType;

 

The main principle is: struts1.* in the JSP display, will first from the formbean through the get*** method to get the value of the property!!


FlowFormFieldCfgElement (#itemIndex#). Name this is the core. If flowFormFieldCfgElement(#itemIndex#) is empty, an error will occur
Public FlowformFieldConfig getFlowFormFieldCfgElement (String key) {
If (! FlowFormFieldCfgMap. Either containsKey (key)) {
FlowFormFieldCfgMap. Put (key, new FlowformFieldConfig ());
}
Return flowFormFieldCfgMap. Get (key);
}
Made a judgment!!
If still do not understand the person can give me message!!


Related articles: