JSFiddle - React, Tailwind, and code Playground
HTML
<form>
<input type="text" name="FormTitle" data-bind="value: FormTitle" placeholder="FormTitle">
<div>
<textarea name="FormDescription" data-bind="value: FormDescription" placeholder="FormDescription"></textarea>
</div>
<div data-bind="template: { name: 'FieldsTemplate', foreach: BlockWorkingFieldForm }"></div>
<script id="FieldsTemplate" type="text/html">
<fieldset>
<legend>Fields Form</legend>
<input type="text" name="FieldHeader" data-bind="value: FieldHeader" placeholder="FieldHeader">
<select required data-bind='options: FieldType, optionsText: "text", optionsValue: "value", optionsCaption: "Select..."'> </select>
<!-- Here you need to insert either div-placeholder (if 'Text 0', 'Text 1' is selected) or div-fieldListQuestions (if 'Text 2', 'Text 3' or 'Text 4' is selected).
And if 'Select...' is selected, do not display anything.
-->
<div data-bind="template: { name: 'placeholder ', foreach: $parent.Field }"></div>
<div data-bind="template: { name: 'fieldListQuestions', foreach: $parent.Field }"></div>
<label>
<input type="checkbox" name="FieldQuestion" checked data-bind="checked: FieldQuestion">Question
</label>
<div>
<a href="#" data-bind="click: function() { FormModel.removeBlock($data) }">- Remove field</a>
</div>
</fieldset>
</script>
<!-- This is template -->
<script id="placeholder" type="text/html">
<input type="text" name="placeholder" placeholder="text">
</script>
<script id="fieldListQuestions" type="text/html">
<textarea name="fieldListQuestions" placeholder="textarea"></textarea>
</script>
<button type="submit" name="btnSave" data-bind="submit: save">Save</button>
<a href="#" data-bind="click: addBlock">+ Add field</a>
</form>
JavaScript
function BlockField() {
return {
FieldHeader: ko.observable(),
FieldType: ko.observableArray([
{ value: '0', text: 'Text 1' },
{ value: '1', text: 'Text 2' },
{ value: '2', text: 'Text 3' },
{ value: '3', text: 'Text 4' },
{ value: '4', text: 'Text 5' }
]),
Field: ko.observable(),
FieldQuestion: ko.observable(false)
};
}
var FormModel = {
FormTitle: ko.observable(),
FormDescription: ko.observable(),
BlockWorkingFieldForm: ko.observableArray(),
addBlock: function () { this.BlockWorkingFieldForm.push(new BlockField()) },
removeBlock: function (blockField) { this.BlockWorkingFieldForm.remove(blockField) },
save: function() {
$.ajax({
url: '/Forms/SaveForm',
type: 'POST',
data: ko.toJSON(this),
dataType: 'json',
cache: false,
contentType: 'application/json',
});
}
};
ko.applyBindings(FormModel);