【问题标题】:How to add custom component ngFormBuilder Form.io如何添加自定义组件 ngFormBuilder Form.io
【发布时间】:2019-05-23 07:38:16
【问题描述】:

我尝试将自定义组件添加到 ngFormBuilder,但我找不到成功 我正在使用 angularjs 这个例子https://github.com/formio/ngFormBuilder 不适合我

我发现这个问题https://github.com/formio/ngFormBuilder/issues/135 但无法正常工作,当我运行此代码时所有表格都清晰

请帮我解决这个问题,谢谢

【问题讨论】:

  • 你为什么投票不好?
  • “不工作”是什么意思?您能否分享您的尝试以及对错误的清晰描述?
  • “不工作”我的意思是我的组件没有显示在其他组件中,也没有显示错误。也许你可以帮我找例子?

标签: angularjs formbuilder formio


【解决方案1】:

您给出的描述很简短,无法知道您的确切问题是什么?你能举一些代码例子吗? 无论如何,我为您提供了一个小“路要走”的例子。 这是定义你的 Angular 应用程序。

 var app = angular.module('formioApp', ['ui.bootstrap', 'ui.select', 'formio', 'ngFormBuilder', 'ngJsonExplorer', 'ngFileUpload']);

这是 from 的架构。您可以根据需要对其进行自定义。

  var formSchemaJSON = { "components": [{ "input": true, "tableView": true, "inputType": "text", "inputMask": "", "label": "Name", "key": "name", "placeholder": "", "prefix": "", "suffix": "", "multiple": false, "defaultValue": "", "protected": false, "unique": false, "persistent": true, "validate": { "required": false, "minLength": "", "maxLength": "", "pattern": "", "custom": "", "customPrivate": false }, "conditional": { "show": "", "when": null, "eq": "" }, "type": "textfield", "tags": [] }, { "input": true, "label": "Submit", "tableView": false, "key": "submit1", "size": "md", "leftIcon": "", "rightIcon": "", "block": false, "action": "submit", "disableOnInvalid": false, "theme": "primary", "type": "button", "tags": [], "conditional": { "show": "", "when": null, "eq": "" } }], "display": "form", "page": 0, "numPages": 1 };

这是你的角度控制器

 app.controller('formioAppCtrl', ['$scope', '$http', '$rootScope', 'Upload', '$compile', 'formioComponents', '$timeout', function ($scope, $http, $rootScope, Upload, $compile, formioComponents, $timeout) {
    $rootScope.form = formSchemaJSON;

现在你必须把它放在你希望你的表单构建器出现在的 html 页面中

<form-builder form="form" options="{ noSubmit: true }"></form-builder>

现在如果你想添加一个自定义组件,你最好创建一个新的 js 文件并将它们添加到一个新的组中,像这样并注册组件。

 app.config(['formioComponentsProvider', function (formioComponentsProvider) {

        formioComponentsProvider.addGroup('amazing', { title: 'Custom Template Components' });
    formioComponentsProvider.register('todaysDate', {
            title: 'Date Fields',
            //template: 'formio/components/todaysDate/todaysDate.html',
            template: 'formio/components/todaysDate.html',
            controller: ['$scope', '$rootScope', function ($scope, $rootScope) {
                $scope.setValue = function (value) {
                    $scope.data[$scope.component.key] = value;
                };
            }],

            group: 'amazing',
            icon: 'fa fa-calendar',
            settings: {
                input: true,
                label: '',
                tableView: true,
                key: 'todaysDate',
                // key:'columns',
                size: 'md',
                leftIcon: '',
                rightIcon: '',
                block: false,
                //action: 'submit',
                disableOnInvalid: false,
                theme: 'primary',
                type: 'todaysDate',
                dataSource: "",
                columns: [{
                    components: [{ "input": true, "tableView": true, "inputType": "text", "inputMask": "", "label": "", "key": "todaysDate", "dataSource": "", "placeholder": "", "prefix": "", "suffix": "", "multiple": true, "defaultValue": "", "protected": false, "unique": false, "persistent": true, "validate": { "required": false, "minLength": "", "maxLength": "", "pattern": "", "custom": "", "customPrivate": false }, "conditional": { "show": "", "when": null, "eq": "" }, "type": "textfield", "tags": [] }]
                }

                ],
                validate: {
                    required: false,
                    multiple: '',
                    custom: ''
                },
                conditional: {
                    show: null,
                    when: null,
                    eq: ''
                }


            },
            controller: ['$scope', '$rootScope', function ($scope, $rootScope) {
                var settings = $scope.component;
                //settings.hideLabel = true;
            }],
            views: [
                {
                    name: 'Display',
                    template: 'formio/components/todaysDate/DateFieldDisplay.html'
                },
                {
                    name: 'Validation',
                    template: 'formio/components/todaysDate/validate.html'
                }
            ]
        });
}






app.run([
              '$templateCache',
              'FormioUtils',
              function ($templateCache, FormioUtils) {
     $templateCache.put('formio/components/todaysDate.html', FormioUtils.fieldWrap('<div id ="todaysDateDirective" class ="todaysDateDirective" todays-Date></div>'));

    }

现在是你的指令

app.directive('todaysDate', function ($rootScope, $compile) {
return {
link:
controller:
template:

}

});

希望这会有所帮助!

【讨论】:

  • 如何在这个输入中添加 ng-if、ng-model、validation ?
猜你喜欢
  • 2021-03-05
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2020-08-11
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多