3

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' />

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. That works. But I wish there was a way to bind just the textbox without the label.
You probably could; for instance, you could probably get rid of the umbProperty directive (iirc it just adds chrome); and you could create your own directive based on the umbEditor if you wanted to and use that instead.
I'd vote this up three times if I could - big thanks!
"config: {dateFormat ... " is incorrect, it should be "config: {format..."
Good pickup - Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.