Angular 4 Kendo Dialog:TotalUnits<0时如何禁用Save按钮?
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
AllocationComponentfrom the dialog - Listen for
TotalUnitsupdates - 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
totalUnitsUpdatedemitter inAllocationComponentsends every change ofTotalUnitsto the main component. - The
updateSaveButtonStatehelper checks ifTotalUnitsis less than 0, then updates the Save button'sdisabledproperty. - We use
dialog.instance.setOptionswith a spread of the existing actions to force the dialog to re-render and apply the new button state. - We also run
updateSaveButtonStateonce initially to handle cases whereTotalUnitsstarts below 0.
Quick Notes
- Make sure your
AllocationComponentactually updatestotalUnitsand 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 likeonTotalUnitsChange.
内容的提问来源于stack exchange,提问作者user3194721
相关产品推荐
相关产品推荐

