Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
70
Adding number with comma into editable grid cell when igx-column type is number
posted

Hello,

Is it possible to add number with comma for example 2,5 into editable grid cell when igx-column type has to be number.

Regards,

Parents
  • 420
    Offline posted

    Hello,

    Thank you for your question regarding handling numeric inputs with commas in the igx-grid component. I understand that you want to allow users to enter values like 2,5 and automatically convert them to 2.5 to comply with the number data type.

    Key Solution Points

    1. Standard Number Format: By default, igx-grid expects numbers to be entered with a dot (.) as the decimal separator. However, users often use a comma (,), which is not recognized as a valid number format.
    2. Custom Editing Template: Using an ng-template with igxCellEditor allows you to create a custom input field within the cell. This gives you full control over how user input is handled and displayed.
    3. Automatic Conversion: By implementing an event handler (onEditChange), you can automatically replace commas with dots, ensuring the input is parsed correctly as a number.
    4. Input Focus and Selection: The input field is automatically focused and its content selected when a cell enters edit mode, providing a smoother user experience.

    Sample Implementation

    Below is a concise version of the implementation, which demonstrates how to allow input with commas and convert it to a valid number format:

    HTML Template

    <igx-column
            field="UnitPrice"
            header="Price"
            dataType="number"
            [editable]="true"
          >
            <!-- Custom Cell Editor Template -->
            <ng-template igxCellEditor let-cell="cell">
              <!-- Input with igxInput -->
              <igx-input-group>
                <input
                  igxInput
                  type="text"
                  [igxFocus]="true"
                  [(ngModel)]="cell.editValue"
                  (ngModelChange)="onEditChange($event, cell)"
                  (focus)="selectInputContent($event)"
                />
              </igx-input-group>
            </ng-template>
          </igx-column>

    TypeScript Component

    // Event handler for changes in the input
      public onEditChange(value: string, cell) {
        // Replace comma with dot for decimal handling (if necessary)
        const formattedValue = value.replace(',', '.');
        
        // Parse the formatted value to a number
        const parsedValue = parseFloat(formattedValue);
        
        // Assign back the formatted value to show in the input
        // Keeps the original value if parsing results in NaN
        cell.editValue = isNaN(parsedValue) ? value : formattedValue;
      }
    
      // Method to select all input content when input is focused
      public selectInputContent(event: FocusEvent) {
        (event.target as HTMLInputElement).select();
      }

    The described scenario could be observed here:

     

    Interactive Sample

    To see this implementation in action, please refer to this interactive example on StackBlitz:

    Interactive StackBlitz Example

    Summary

    With this approach:

    • Users can input values using a comma (,), and it will automatically be converted to a valid dot (.) format.
    • The cell uses a custom input template defined with ng-template and igxCellEditor to handle user input.
    • This setup provides a more user-friendly editing experience while ensuring data consistency and validation within the grid.

    Please let me know if you have any further questions or need additional assistance!

    Regards,

    Georgi Anastasov

    Entry Level Software Developer

    Infragistics

Reply Children
No Data