Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create dynamic form controls.

where textbox, radio button can drag and drop in form.


I found drag and drop at here.

[^]

and below link exactly what I required but at here required drag and drop
http://jsfiddle.net/techbrij/uNNMr/

What I have tried:

@{
ViewBag.Title = "draganddrop3";
Layout = null;
}

<h2>draganddrop3</h2>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Droppable - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#box {
width: 100px;
height: 100px;
padding: 0.5em;
float: left;
margin: 10px 10px 10px 0;
}

.draggable {
float: left;
z-index: 99;
}

#droppable {
width: 300px;
height: 500px;
padding: 0.5em;
position: absolute;
left: 33%;
top: 15%;
margin: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function () {
$(".draggable").draggable();
$("#droppable").droppable({

drop: function (event, ui) {
debugger;
var draggableId = ui.draggable.attr("id");
//every time getfileldId get textbox (how can i get currend draggable id value)
if (ui.draggable.attr("id") == "textbox") {
$(this).append('<input id="Text1" type="text" />')
}
else if (ui.draggable.attr("id") == "chekbox") {
$(this).append('<input id="Checkbox1" type="checkbox" />')
}
else if (ui.draggable.attr("id") == "dropdown") {
$(this).append('<select name="dropdown">' +
'<option value="volvo">Volvo</option>' +
'<option value="saab">Saab</option>' +
'<option value="fiat">Fiat</option>' +
' <option value="audi">Audi</option>' +
' </select>')

}

}
});
});
</script>
</head>
<body>

<div id="" class="">
<ul class="draggable" id="textbox">Textbox</></ul><br />
<ul class="draggable" id="chekbox">chekbox</> </ul><br />
<ul class="draggable" id="dropdown">dropdown</></ul><br />


</div>

<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>


</body>
</html>
Posted
Updated 27-Jan-18 0:44am
v7
Comments
Karthik_Mahalingam 26-Jan-18 3:02am    
what you have tried?
show the code.
Jaydeep Shah 26-Jan-18 3:49am    
hey, @Karthik I update my full code here!

currently, the textbox is appended successfully.
but getfileldId alwasy gets "textbox" that's why my other controls are not working.


$("#droppable").droppable({
drop: function (event, ui) {
var getfileldId = $(".draggable").attr("id"); //every time getfileldId get textbox (how can i get currend draggable id value)
if ($(".draggable").attr("id") == "textbox") {
$(this).append('')
}
else if ($(".draggable").attr("id") == "chekbox") {
$(this).append('')
}
else {
//dropdown code.
}
}
});
});

my full code is in the "What I have tried:" section
thanks
Karthik_Mahalingam 26-Jan-18 4:41am    
can you post your working code in fiddle?
Jaydeep Shah 26-Jan-18 4:59am    
https://jsfiddle.net/shahjaydeep/jjamj2r7/

I have done drag and drop :)

now I required unique id of every fields control
suppose I drag 3-time textbox then all textbox id must be unique how can I get this?

and how can I save in DB using dynamic form . ?

1 solution

function getNewId(type) {
           var newId;
           if (type == "textbox")
               newId = $('#droppable').find(':text').length
           if (type == "chekbox")
               newId = $('#droppable').find(':checkbox').length
           if (type == "dropdown")
               newId = $('#droppable').find('select').length
           return type + (newId + 1);
       }


       $(function () {
           $(".draggable").draggable();
           $("#droppable").droppable({

               drop: function (event, ui) {
                   debugger;
                   var draggableId = ui.draggable.attr("id");
                   var newid = getNewId(draggableId);
                   if (draggableId == "textbox") {
                       $(this).append('<input id="' + newid + '" type="text" />')
                   }
                   else if (draggableId == "chekbox") {
                       $(this).append('<input id="' + newid + '"  type="checkbox" />')
                   }
                   else if (draggableId == "dropdown") {
                       $(this).append('<select   id="' + newid + '"   >' +
                        '<option value="volvo">Volvo</option>' +
                        '<option value="saab">Saab</option>' +
                        '<option value="fiat">Fiat</option>' +
                       ' <option value="audi">Audi</option>' +
                       ' </select>')

                   }

               }
           });
       });
 
Share this answer
 
Comments
Jaydeep Shah 29-Jan-18 8:41am    
Thanks.

and how can I change the label value (on label click label text change)
my current work... ( i add function EditLabel() but it does not work)
https://jsfiddle.net/shahjaydeep/tmtfw74q/

I got from here...(editable label)
https://www.aspsnippets.com/Articles/Editable-Label-Convert-a-Label-to-TextBox-when-clicked-using-jQuery-in-ASPNet.aspx
Karthik_Mahalingam 29-Jan-18 9:06am    
are you getting any error?
Karthik_Mahalingam 29-Jan-18 9:09am    
check this
demo[^]
Karthik_Mahalingam 29-Jan-18 9:25am    
check the above link
Jaydeep Shah 29-Jan-18 9:27am    
yes i check
https://plnkr.co/edit/RH6ZA4QakZmnzPah9vpb?p=preview

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900