Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="col-12">
    <div class="boxlist margin-top-30" id="training-box">
      <div class="box-header bg-green-blue">
        <h3 class="box-title">Simple Form Examples</h3>
      </div>
      <div class="panel-group" id="accordion">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                <span id="oneSpan" class="glyphicon glyphicon-menu-right text-success"></span> The First Form
              </a>
            </h4>
          </div>
          <div id="collapseOne" class="panel-collapse collapse in">
            <div class="panel-body">
              <p>In this first example, we automatically generate a form from a simple JSON schema. The schema is read from <i><b>schema.js</b></i>, which directly assigns the schema to a variable called <i><b>dataModel</b></i>, that is used as input for the Metadata Editor Form. In addition, the form type is provided, which is in our case <i><b>CREATE</b></i>, which means we start with an empty form where all fields are editable. Thus, the parameter object looks as follows:
              </p>
              <pre>
var options = {
	operation: "CREATE",
	dataModel: this.dataModel
};</pre>
              Using these parameters, we can now insert the form into the page. It takes the <i><b>options</b></i> object for parameterization and renders where the <i><b>&lt;form id="plain"&gt;&lt;/form&gt;</b></i> tags are located in this HTML file:
              <pre>
$('#plain').metadataeditorForm(options, (value) => {
	var resource = value;
	JSON.parse(JSON.stringify(resource));
	Console.log(resource);
});</pre>
              Furthermore, the form received a callback function, which prints the resulting object at the console as soon as <i><b>CREATE</b></i> is clicked.</p>
              <hr />
              <div class="box-body">
                <form id="plain"></form>
              </div>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <span id="twoSpan" class="glyphicon glyphicon-menu-right text-info"></span> A More Appealing Form
              </a>
            </h4>
          </div>
          <div id="collapseTwo" class="panel-collapse collapse">
            <div class="panel-body">
              <p>Now, it's time to work on a more user-friendly representation of the form. There are two things we are doing:
              <ul>
                <li>Apply a UI Schema</li>
                <li>Overwrite the default submit button label</li>
              </ul>
              The UI Schema we include from the file <i><b>schema.js</b></i>, which assigns the schema to a variable called <i><b>uiSchema</b></i>. The resulting <i><b>options</b></i> object now looks as follows:
              <pre>
var options = {
	operation: "CREATE",
	dataModel: this.dataModel
	uiForm: this.uiSchema,
	buttonTitle: "Submit"
};</pre>
              In addition to providing the <i><b>uiForm</b></i> property you also see, that we provide <i><b>buttonTitle</b></i> which overwrites the default label of the submit button. Having this done, the form is rendered where the <i><b>&lt;form id="with-schema&gt;&lt;/form&gt;</b></i> tags are located using the following JavaScript call:
              <pre>
$('#with-schema').metadataeditorForm(options, (value) => {
	var resource = value;
	JSON.parse(JSON.stringify(resource));
	Console.debug(resource);
});</pre>
              </p>
              <hr />
              <div class="box-body">
                <form id="with-schema"></form>
              </div>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                <span id="threeSpan" class="glyphicon glyphicon-menu-right text-info"></span> A Form with Data
              </a>
            </h4>
          </div>
          <div id="collapseThree" class="panel-collapse collapse">
            <div class="panel-body">
              <p>Finally, we will learn how to add some data to show in the form. Therefore, we have a file <i><b>data.js</b></i> which contains a variable <i><b>resource</b></i> defining the input of our form. Furthermore, we have to change the options for parameterizing our form as follows:
              <pre>
var options = {
	operation: "READ",
	dataModel: this.dataModel,
	uiForm: this.uiSchema,
	resource: this.resource,
	buttonTitle: "Update"
};</pre>
              As you can see, we changed to operation to <i><b>READ</b></i>, as operation <i><b>CREATE</b></i> does not accept any inputs. This input is provided by property <i><b>resource</b></i> pointing to <i><b>this.resource</b></i> which comes from <i><b>data.js</b></i>.
              The result is rendered where the <i><b>&lt;form id="with-data"&gt;&lt;/form&gt;</b></i> tags are located using the following JavaScript call:
              <pre>
$('#with-data').metadataeditorForm(options, () => {});</pre>
              </p>
              For <i><b>READ</b></i> forms no callbacks are supported as they are purely for visualization. Thus, the callback function remains empty in this example.
              <hr />
              <div class="box-body">
                <form id="with-data"></form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                $("body").initializeModals();

var resource = {    	
		"familyName": "Doe",
		"givenName": "John",
		"age": 42,
		"gender": "Male"
		};

var dataModel = {
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "$defs": {
        "Gender": {
            "type": "string",
            "enum": ["Male", "Female", "Transgender", "Non-binary", "Not provided"]
        }
    },
	"type": "object",
    "properties": {
        "familyName": {
                "type": "string"
                },
                "givenName": {
                    "type": "string"
                },
				"age":{
					"type": "integer"
				},
                "gender": {
                   "$ref": "#/$defs/Gender"
                }
    }
};

var uiSchema = 
{
           "type": "fieldset",
           "legend": "Person",
            "items": [
				{"notitle": true, "placeholder": "Organization", "key":"givenName"},
				{"notitle": true, "placeholder": "Family Name", "key": "familyName"},
				{"notitle": true, "placeholder": "Age", "key": "age"},
				{"notitle": true, "key": "gender"}
			]

};

var view = {
  template:
    "<div>" +
    '<input  class="form-control typeahead" type="text" placeholder="<%=escape(node.placeholder)%>">' +
    '<input id="ror-id-01" name="<%= node.name %>" value="<%= escape(node.value) %>" type="hidden">' +
    "</div>",

  inputfield: true,
  array: false,
  fieldtemplate: true,

  getElement: function (el) {
    return $(el).get(1);
  },

  onBeforeRender: function (data, node) {},

  onInsert: function (evt, node) {
    var ROR_API_URL = "https://api.ror.org/organizations?affiliation=";

    $(".typeahead").typeahead(
      {
        hint: false,
        highlight: true,
        minLength: 3
      },
      {
        limit: 50,
        async: true,
        source: function (query, processSync, processAsync) {
          url = ROR_API_URL + encodeURIComponent(query);
          return $.ajax({
            url: url,
            type: "GET",
            dataType: "json",
            success: function (json) {
              orgs = json.items;
              return processAsync(orgs);
            }
          });
        },
        templates: {
          pending: [
            '<div class="empty-message">',
            "Fetching organizations list",
            "</div>"
          ].join("\n"),
          suggestion: function (data) {
            return (
              "<p><strong>" +
              data.organization.name +
              "</strong><br>" +
              data.organization.types[0] +
              ", " +
              data.organization.country.country_name +
              "</p>"
            );
          }
        },
        display: function (data) {
          return data.organization.name;
        },
        value: function (data) {
          return data.organization.identifier;
        }
      }
    );

    $(".typeahead").bind("typeahead:select", function (ev, suggestion) {
      $("#ror-id-01").val(suggestion.organization.id);
    });
  }
};

JSONForm.fieldTypes["ror-organization"] = view;

$("#accordion").on("show.bs.collapse", function (element) {
  var id =
    "#" + element.target.id.replace("collapse", "").toLowerCase() + "Span";
  $(id).addClass("glyphicon-menu-down");
});
$("#accordion").on("hidden.bs.collapse", function (element) {
  var id =
    "#" + element.target.id.replace("collapse", "").toLowerCase() + "Span";
  $(id).removeClass("glyphicon-menu-down");
});

resolveRefs(dataModel, dataModel.$defs);

var options = {
  operation: "CREATE",
  dataModel: this.dataModel
};
$("#plain").metadataeditorForm(options, (value) => {
  var resource = value;
  JSON.parse(JSON.stringify(resource));
  console.log(resource);
  alert(resource);
});
var options = {
  operation: "CREATE",
  dataModel: this.dataModel,
  uiForm: this.uiSchema,
  buttonTitle: "Submit"
};
$("#with-schema").metadataeditorForm(options, (value) => {
  var resource = value;
  JSON.parse(JSON.stringify(resource));
  console.log(resource);
  alert(resource);
});
var options = {
  operation: "READ",
  dataModel: this.dataModel,
  uiForm: this.uiSchema,
  resource: this.resource,
  buttonTitle: "OK"
};
$("#with-data").metadataeditorForm(options, () => {});

              
            
!
999px

Console