gitana / alpaca

Alpaca provides the easiest way to generate interactive HTML5 forms for web and mobile applications. It uses JSON Schema and simple Handlebars templates to generate great looking, dynamic user interfaces on top of Twitter Bootstrap, jQuery UI, jQuery Mobile and HTML5.
http://www.alpacajs.org
Other
1.29k stars 371 forks source link

Array with objects: colorpicker field doesn't render #619

Closed roypardi closed 6 years ago

roypardi commented 6 years ago

I have been trying to use an object with colorpicker field in an array without success. I can make an array of colorpickers – so I have all the correct code hooked up.

I am looking to create an array of objects of varying types. For a simple example:

name - string
age - number
favorite color - colorpicker
birthday - datetime-picker

I've got a stripped down jsfiddle here which demonstrates the issue: I am able to create a colorpicker – but the same code does not work when in an object within an array.

Am I misunderstanding what an array field can contain?


Example code from the jsfiddle:

var test = {
  view: 'bootstrap-edit-horizontal',
  schema: {
    "title": "Can't put a color picker in array?",
    "type": "object",
    "properties": {
      "testColorPicker": {
        "type": "string",
        "default": "#ff0000"
      },
      "categories": {
        "type": "array",
        "items": {
          "title": "Category",
          "type": "object",
          "properties": {
            "categoryID": {
              "type": "string",
              "title": "Category ID"
            },
            "categoryColor": {
              "type": "string",
              "title": "Where is my color picker?"
            }
          }

        }
      }
    }
  },
  options: {
    fields: {
      "testColorPicker": {
        "type": "colorpicker",
        "label": "Test Color"
      },
      "category": {
        "type": "object",
        "items": {
          "categoryID": {
            "label": "Category ID"
          },
          "categoryColor": {
            "type": "colorpicker",
            "label": "Category Color"
          }
        }
      }
    }
  }
};

$('#form').alpaca(test);
roypardi commented 6 years ago

Finally figured this out

Working jsfiddle: https://jsfiddle.net/ut8wgtk5/60/

var data = {
    view: {
        "parent": "bootstrap-edit-horizontal"
    },
    schema: {
        "title": "Test",
        "type": "object",
        "properties": {
            "testColorPicker": {
                "type": "string",
                "default": "#000000"
            },
            "testSummernote": {
                "type": "string"
            },

            "testArray": {
                "type": "array",
                "title": "Array",
                "items": {
                    "title": "Array Item",
                    "type": "object",
                    "properties": {
                        "categorySummernote": {
                            "type": "string",
                        },
                        "categoryColor": {
                            "type": "string",
                            "default": "#000000"
                        }
                    }
                },
            }

        }
    },
    options: {
        "fields": {
            "testColorPicker": {
                "type": "colorpicker",
                "component": true,
                "label": "Test Color",
                useAlpha: false
            },
            "testSummernote": {
                "label": "Test Summernote",
                "type": "summernote",
                "summernote": {
                    "toolbar": [
                        ['style', ['bold', 'italic', 'underline', 'clear']],
                        ['font', ['strikethrough', 'superscript', 'subscript']],
                        ['fontsize', ['fontsize']],
                        ['color', ['color']],
                        ['para', ['ul', 'ol', 'paragraph']],
                        ['height', ['height']]
                    ]
                }
            },
            "testArray": {
                "toolbarSticky": true,
                "actionbarStyle": "bottom",
                "label": "Array",
                "items": {
                    "fields": {
                        "categorySummernote": {
                            "label": "Test Summernote",
                            "type": "summernote",
                            "summernote": {
                                "toolbar": [
                                    ['style', ['bold', 'italic', 'underline', 'clear']],
                                    ['font', ['strikethrough', 'superscript', 'subscript']],
                                    ['fontsize', ['fontsize']],
                                    ['color', ['color']],
                                    ['para', ['ul', 'ol', 'paragraph']],
                                    ['height', ['height']]
                                ]
                            }
                        },
                        "categoryColor": {
                            "type": "colorpicker",
                            "component": true,
                            "label": "Test Color"
                        }
                    }
                }
            }
        }
    }
};

$('#form').alpaca(data);

function getJSON() {
    var result = $('#form').alpaca().getValue();
    console.log('result', result);
}