admin管理员组

文章数量:1316018

I'm using DevExpress's Form widget to create two date editors: CheckInFromDate and CheckInToDate. I want to enable the second date box (CheckInToDate) only when the first date box (CheckInFromDate) has a valid date selected. However, calling form.getEditor('CheckInToDate') inside onValueChanged of the first date box returns undefined.

Here's a simplified version of my code:

$(function () {
  const formData = {};

  const form = $("#formContainer")
    .dxForm({
      formData: formData,
      items: [
        {
          itemType: "group",
          colCount: 2,
          items: [
            {
              dataField: "CheckInFromDate",
              label: { text: "Check-In from" },
              editorType: "dxDateBox",
              template: function (data, $itemElement) {
                const editorTemplate = $("<div>").dxDateBox({
                  dataField: "CheckInFromDate2",
                  showClearButton: true,
                  displayFormat: "dd/MM/yyyy",
                  onValueChanged: function (e) {
                    const minDate = e.value;
                    form.getEditor("CheckInToDate").option("min", minDate);
                    form
                      .getEditor("CheckInToDate")
                      .option("disabled", !minDate);
                  },
                });

                editorTemplate
                  .dxValidator({
                    validationGroup: "checkInValidator",
                    validationRules: [
                      {
                        type: "required",
                        message: "At least one valid date pair is required.",
                      },
                    ],
                  })
                  .appendTo($itemElement);

                editorTemplate.dxDateBox("instance");
              },
            },
            {
              dataField: "CheckInToDate",
              label: { text: "To" },
              template: function (data, $itemElement) {
                $("<div>")
                  .dxDateBox({
                    showClearButton: true,
                    displayFormat: "dd/MM/yyyy",
                    disabled: true,
                  })
                  .dxValidator({
                    validationGroup: "checkInValidator",
                    validationRules: [
                      {
                        type: "required",
                        message: "At least one valid date pair is required.",
                      },
                    ],
                  })
                  .appendTo($itemElement);
              },
            },
          ],
        },
      ],
    })
    .dxForm("instance");
});

I assumed that adding dataField: 'CheckInToDate' plus a custom template for the second item would make form.getEditor('CheckInToDate') return the editor instance.

I also attempted setting editorType: 'dxDateBox' for the second item, but the custom template overrides that.

My expectation is to retrieve the second date box instance via the form's getEditor method so I can enable/disable it dynamically..

I'm using DevExpress's Form widget to create two date editors: CheckInFromDate and CheckInToDate. I want to enable the second date box (CheckInToDate) only when the first date box (CheckInFromDate) has a valid date selected. However, calling form.getEditor('CheckInToDate') inside onValueChanged of the first date box returns undefined.

Here's a simplified version of my code:

$(function () {
  const formData = {};

  const form = $("#formContainer")
    .dxForm({
      formData: formData,
      items: [
        {
          itemType: "group",
          colCount: 2,
          items: [
            {
              dataField: "CheckInFromDate",
              label: { text: "Check-In from" },
              editorType: "dxDateBox",
              template: function (data, $itemElement) {
                const editorTemplate = $("<div>").dxDateBox({
                  dataField: "CheckInFromDate2",
                  showClearButton: true,
                  displayFormat: "dd/MM/yyyy",
                  onValueChanged: function (e) {
                    const minDate = e.value;
                    form.getEditor("CheckInToDate").option("min", minDate);
                    form
                      .getEditor("CheckInToDate")
                      .option("disabled", !minDate);
                  },
                });

                editorTemplate
                  .dxValidator({
                    validationGroup: "checkInValidator",
                    validationRules: [
                      {
                        type: "required",
                        message: "At least one valid date pair is required.",
                      },
                    ],
                  })
                  .appendTo($itemElement);

                editorTemplate.dxDateBox("instance");
              },
            },
            {
              dataField: "CheckInToDate",
              label: { text: "To" },
              template: function (data, $itemElement) {
                $("<div>")
                  .dxDateBox({
                    showClearButton: true,
                    displayFormat: "dd/MM/yyyy",
                    disabled: true,
                  })
                  .dxValidator({
                    validationGroup: "checkInValidator",
                    validationRules: [
                      {
                        type: "required",
                        message: "At least one valid date pair is required.",
                      },
                    ],
                  })
                  .appendTo($itemElement);
              },
            },
          ],
        },
      ],
    })
    .dxForm("instance");
});

I assumed that adding dataField: 'CheckInToDate' plus a custom template for the second item would make form.getEditor('CheckInToDate') return the editor instance.

I also attempted setting editorType: 'dxDateBox' for the second item, but the custom template overrides that.

My expectation is to retrieve the second date box instance via the form's getEditor method so I can enable/disable it dynamically..

Share Improve this question edited Jan 30 at 6:04 Phil 165k25 gold badges262 silver badges267 bronze badges asked Jan 30 at 6:02 Li ChangLi Chang 311 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

From the answer here

The dxForm.getEditor method is intended only for built-in editors. If you are using templates, you will need to find editors manually

So set the id on the editorTemplate div's and get the dtDateBox instance like this:

$("#CheckInToDate").dxDateBox("instance")

Full editorTemplate code:

const editorTemplate = $("<div id='CheckInFromDate'>").dxDateBox({
    dataField: "CheckInFromDate2",
    showClearButton: true,
    displayFormat: "dd/MM/yyyy",
    onValueChanged: function(e) {
        const minDate = e.value;

        const checkInToDate = $("#CheckInToDate").dxDateBox("instance")
        checkInToDate.option("min", minDate)
        checkInToDate.option("disabled", ! minDate)
    },
});

Note: You also need to set the id attribute on the CheckInToDate field.

...
    template: function(data, $itemElement) {
        $("<div id='CheckInToDate'>").dxDateBox({
        ...
        })
    }
...

Check this working JsFiddle Demo.

本文标签: