0

I have recently started converting a pure jquery mobile app into angularjs and ionic. However there is one thing I am struggling to figure out, and that is how to create a truly dynamic form. I have seen examples using ng-repeat where you can dynamically add one type of input. However the forms I am building come from a server so I need to dynamically add any number and combination of different types of inputs (dates, dropdowns, textareas etc) based on the information I retrieve from the server. Does anyone know where I could find an example of how to do this? Here is a sample of my jquery mobile code to give you an idea of what I am trying to achieve:

if (field.Display_Type == "Combo" || field.Display_Type== "LocationIndexLookup" || field.Display_Type == "Country" || field.Display_Type== "UserLookup"||field.Display_Type=="CustomDataLookup")
    { addCombo(div, field); }

    else if(field.Display_Type=="Checkbox")
    { addCheckbox(div, field);}

    else if (field.Display_Type == "MultiValue")//Check multicombo
    { addMultiCombo(div, field); }

    else if (field.Display_Type == "Text")
    { addText(div, field); }

    else if (field.Display_Type == "Date")
    { addDate(div, field); }

    else if (field.Display_Type=="DateTime")
    {addDateTime(div,field); }

    else if (field.Display_Type == "Notes")
    { addNote(div, field); }

    else if(field.Display_Type=="Numeric")
    { addNumeric(div, field); }

    else if (field.Display_Type =="Time")
    { addTime(div, field); }

    else if (field.Display_Type== "ContactsLookup"||field.Display_Type=="RecordDataLookup")
    {
        addContact(div, field);
    }

    else if (field.Display_Type=="Rating")
    { addRating(div, field) }

    else if(field.Display_Type=="Signature")
    { addSignature(div, field); }

    else if(field.Display_Type=="Hidden") 
    {
        //alert("hidden");
    }

    else if(field.Display_Type=="ProjectsLookup")
    {
        addProjectsLookup(div, field);
    }

    else if (field.Display_Type=="DocumentsLookup")
    {
        addDocumentsLookup(div, field);
    }

    else 
    { addText(div, field); }

    if(field.HelpNotes!=null && field.HelpNotes!="")
    { addHelpNote(div, field);}

}

each of these calls a function to add the appropriate dom element such as:

function addCombo(div, data) {
$("#" + div).append("<b style=\"align:left !important; text-align:left !important;\">" + data.Caption + ": <br> <Select id=\"Field" + data.OwnID + "\" class=\"" + data.Required + "\"></Select>").trigger("create");
$("#Field" + data.OwnID).append($('<option>', {
    value: "",
    text: ""
})).trigger("create");
for (i in data.Options) {
    //display the data in the key-value pair
    $("#Field" + data.OwnID).append($('<option>', {
        value: data.Options[i].Value,
        text: data.Options[i].Caption
    })).trigger("create");
}
$("#Field" + data.OwnID).selectmenu("refresh");

}

some of the fields that are added have custom behaviours that I have coded, so I do not want to use an existing form builder library for angular.

Thanks in advance

Suzie
  • 73
  • 1
  • 4
  • You should make custom directive for the dynamic form. check this out http://stackoverflow.com/questions/24248098/angularjs-dynamic-form-from-json-data-different-types – Hmahwish Sep 06 '16 at 12:13
  • @neda Thank you that is exactly what I was looking for! – Suzie Sep 06 '16 at 12:22

0 Answers0