0

Can anyone guide me how to dynamically add input field in a form and on save of the form, the same number of columns should get added to the corresponding table. Can we do it by using Hibernate or can only be done by JDBC?

$(document).ready(function () {
    var counter = 1;
    var favorite = [];        

    $('#add').click(function () {
        var name = 'textName' + counter;
        $('#inputContainer').append("<div>" + name + "<input type='text' 
        id='" + name + "' /></div>");
        counter++;
    });

    $('#Save').click(function () {
        for (var i = 1; i < counter; i++) {
            var textValue = $('#textName' + i).val();
            favorite.push(textValue);
            //If you to save all data in one by one then Save function here
            //Save(textValue);
        }
        //If you to save all data in one go then call Save function here
        Save(favorite);
    });    });



function Save(dataToSave)
    {
    $.ajax({
        url: "AJAX_POST_URL",
        type: "POST",
        data: dataToSave,
        success: function (data, textStatus, jqXHR) {
            //data - response from server
        },
        error: function (jqXHR, textStatus, errorThrown) {

        }
    });
}
Appu
  • 51
  • 7
  • yes hibernate works of course. What have you tried? show us teh codez. – Jack Flamp Nov 10 '17 at 11:18
  • Is the JavaScript tag relevant? – evolutionxbox Nov 10 '17 at 11:19
  • @evolutionxbox, I was trying with JavaScript to add input fields so i added the tag. – Appu Nov 10 '17 at 11:39
  • @JackFlamp, If I try to develop the application using MVC, it will cause problems right? As, the model elements can't be changed dynamically? – Appu Nov 10 '17 at 11:41
  • what is the problem you are having? can you not receive the request on the server or is there a problem saving to DB? – Jack Flamp Nov 10 '17 at 11:46
  • no MVC is a separation of tasks in your program. – Jack Flamp Nov 10 '17 at 11:47
  • @JackFlamp I wanted to know which would be the best way to go about creating a application, for which if a new field is added to the form it should stay there and a corresponding column is added in the database too. – Appu Nov 10 '17 at 11:54

1 Answers1

0

ah ok, now I understand. you want to add new columns dynamically to a database table. you can use Hibernate. With the EntityManager you can run an ALTER TABLE statement probably. look here https://stackoverflow.com/a/9914086/3959856

Although, there might be better ways, like working with something you can call Attributes for example, if it would work for you. Create one table (and corresponding entity in Hibernate) called AttributeType and one called Attribute as an example. When a new field is added you create a new AttributeType (if it is a new type, else reuse and existing type). Then create a new Attribute which holds AttributeType and a value. I think that is a better way.

public class Attribute {
    private AttributeType type;
    private String value;

   //getters setters
}

public class AttributeType {
    private String name;

    //getter setter
}
Jack Flamp
  • 1,223
  • 1
  • 16
  • 32