You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 4 Kendo Dialog:TotalUnits<0时如何禁用Save按钮?

How to Disable Kendo Dialog's Save Button When TotalUnits < 0

Got it, let's figure out how to disable that Save button in your Kendo Dialog when TotalUnits drops below 0. Here's a step-by-step solution tailored to your code:

Step 1: Add a Change Emitter in Your AllocationComponent

First, your AllocationComponent (the dialog's content) needs to notify the parent component when TotalUnits changes. We'll use an EventEmitter for this:

// allocation.component.ts
import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-allocation',
  templateUrl: './allocation.component.html'
})
export class AllocationComponent {
  // Assume this is your TotalUnits property, bound to your UI inputs
  totalUnits: number = 0;
  
  // Emitter to send updates to the parent component
  @Output() totalUnitsUpdated = new EventEmitter<number>();

  // Example method triggered when TotalUnits changes (e.g., from an input)
  onTotalUnitsChange(newValue: number) {
    this.totalUnits = newValue;
    this.totalUnitsUpdated.emit(this.totalUnits);
  }
}

Step 2: Update the Dialog Setup in Your Main Component

Now, in your main component's AllocationDialog method, we'll:

  • Grab the instance of AllocationComponent from the dialog
  • Listen for TotalUnits updates
  • Dynamically toggle the Save button's disabled state

Here's the modified code:

AllocationDialog(data: any) {
  const dialog: DialogRef = this.component.dialogService.open({
    title: ' Allocations',
    content: AllocationComponent,
    actions: [
      { text: 'Save', primary: true, data, disabled: false }, // Initial state
    ],
    width: 500,
    height: 500
  });

  // Get the instance of the AllocationComponent inside the dialog
  const allocationContent = dialog.contentInstance as AllocationComponent;

  // Helper function to update Save button state
  const updateSaveButtonState = (units: number) => {
    const saveAction = dialog.instance.actions.find(action => action.text === 'Save');
    if (saveAction) {
      saveAction.disabled = units < 0;
      // Refresh the dialog to apply the button state change
      dialog.instance.setOptions({ 
        actions: [...dialog.instance.actions] 
      });
    }
  };

  // Set initial button state based on current TotalUnits
  updateSaveButtonState(allocationContent.totalUnits);

  // Listen for TotalUnits updates from the content component
  allocationContent.totalUnitsUpdated.subscribe((units) => {
    updateSaveButtonState(units);
  });

  dialog.result.subscribe((dialogResult) => {
    if (dialogResult instanceof DialogCloseResult) {
      console.log('close');
    } else {
      console.log('action', dialogResult);
      // Your existing save logic here
    }
  });
}

How This Works

  • The totalUnitsUpdated emitter in AllocationComponent sends every change of TotalUnits to the main component.
  • The updateSaveButtonState helper checks if TotalUnits is less than 0, then updates the Save button's disabled property.
  • We use dialog.instance.setOptions with a spread of the existing actions to force the dialog to re-render and apply the new button state.
  • We also run updateSaveButtonState once initially to handle cases where TotalUnits starts below 0.

Quick Notes

  • Make sure your AllocationComponent actually updates totalUnits and emits the event whenever the value changes (e.g., when a user types in an input or adjusts a slider).
  • If you're using a reactive form in AllocationComponent, you could subscribe to form value changes instead of a manual method like onTotalUnitsChange.

内容的提问来源于stack exchange,提问作者user3194721

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:19:47