Search code examples
angularjsumbracoumbraco7

Reusing Umbraco datetime picker


Umbraco 7 has this date/time picker enter image description here

How do you reuse that in a custom control to avoid reinventing the wheel?

Currently I only have a plain textbox:

<input type='text' ng-model='validTo' />

Solution

  • Possibly the easiest way is to instantiate the right editor in the controller and bind it to the umbEditor directive. Your template might include something like this:

        <umb-property ng-if="validTo.hasValue" property="validTo.model">
            <umb-editor model="validTo.model"></umb-editor>
        </umb-property>
    

    while your controller might contain something like this:

    $scope.validTo = {
        model: null,
        existingValue: null, 
        hasValue: false
    };
    
    function buildDateTimePickerModel(alias, label, description) {
        return {
            editor: "Umbraco.DateTime",
            label: label,
            description: description,
            hideLabel: false,
            view: "datepicker",
            alias: alias,
            value: null,
            validation: {
                mandatory: false,
                pattern: ""
            },
            config: {
                format: "YYYY-MM-DD HH:mm:ss",
                pickDate: true,
                pickTime: true,
                useSeconds: true
            }
        };
    };
    
    $scope.validTo.model = buildDateTimePickerModel('validTo', 'Valid To', 'Enter the Valid To date');
    

    Or something like that. I've not tested it but have derived it from an existing project of mine that implements the ContentPicker on a custom UI in a similar fashion.

    You might also find this useful to get an idea of the available config options:

    https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web.UI.Client/src/views/propertyeditors/datepicker/datepicker.controller.js#L4-L16