Datagrid for Nette Framework

repository·master·Indexed 43 days ago

https://github.com/contributte/datagrid

A powerful data grid component for the Nette Framework that simplifies the implementation of complex data tables. It features filtering, sorting, pagination, tree views, and translator support. Version 7.1.0 supports Nette 3.2+ and PHP 8.2+.

Tokens
13.9K
Snippets
56
Records
63
Agent score
54%

What's inside Datagrid

  1. Overview of datagrid features

    master

    Datagrid is a first-class datagrid solution designed for the Nette Framework. It provides a comprehensive set of features for managing and displaying data, including:

    • Filtering
    • Sorting
    • Pagination
    • Tree view
    • Table view
    • Translator support
  2. Rename session methods to storage methods in v7.1.x

    master

    In v7.1.x, session-related methods have been renamed to reflect a more generic storage abstraction. This affects applications using custom state management.

    Method Mapping:

    • setStrictSessionFilterValues() $\rightarrow$ setStrictStorageFilterValues()
    • getSessionData($key) $\rightarrow$ getStorageData($key, $defaultValue) (Note: getStorageData adds an optional $defaultValue parameter)
    • saveSessionData($key, $value) $\rightarrow$ saveStorageData($key, $value)
    • deleteSessionData($key) $\rightarrow$ deleteStorageData($key)
    // After (v7.1.x):
    $grid->setStrictStorageFilterValues(true);
    $data = $grid->getStorageData($key, $defaultValue);
    $grid->saveStorageData($key, $value);
    $grid->deleteStorageData($key);
  3. Migrate translation keys in v7.1.x

    master

    In v7.1.x, the automatic conversion of ublaboo_datagrid.* keys to contributte_datagrid.* has been removed. You must use the contributte_datagrid.* prefix directly in your translation files.

    Migration Steps:

    1. Update all ublaboo_datagrid.* translation keys to contributte_datagrid.*.
    2. Search your codebase for ublaboo_datagrid and replace it with contributte_datagrid.
    // After (v7.1.x):
    new SimpleTranslator([
        'contributte_datagrid.no_items' => 'No records found',
        'contributte_datagrid.choose' => 'Choose...',
        'contributte_datagrid.reset_filter' => 'Reset filter'
    ]);
  4. Upgrade from 6.x to 7.0.x

    master

    Version 7.0.x is a major modernization release. It requires PHP 8.2+ and removes legacy compatibility layers.

    System Requirements:

    • PHP: 8.2 or higher
    • Composer dependency: contributte/datagrid: ^7.0

    Breaking Changes:

    • Legacy Compatibility Removal: All Ublaboo $\rightarrow$ Contributte transition layers are gone. Ensure all code uses Contributte\Datagrid and update translation keys to use contributte_datagrid.*.
    • Modern PHP 8.2 Features: The codebase uses union types, intersection types, and Enums.

    Preparation & Installation:

    1. Ensure your 6.x installation is up to date: composer require contributte/datagrid:^6.10.
    2. Update PHP to 8.2+.
    3. Update dependencies:
      composer require php:^8.2
      composer require contributte/datagrid:^7.0
    {
        "require": {
            "php": "^8.2",
            "contributte/datagrid": "^7.0"
        }
    }
  5. Migrate from 4.x to 5.0.x

    master

    Upgrading to version 5.0.x requires updating your PHP environment and refactoring several core components due to architectural improvements in aggregation, multi-action columns, and filtering.

    System Requirements

    • PHP Version: Upgrade from PHP 5.5+ to PHP 5.6+.

    Breaking Changes

    1. Trait Refactoring (High Impact)

    Custom button implementations must replace the monolithic TButton trait with specific traits for icons, classes, text, and titles.

    Before (v4.x):

    use Ublaboo//DataGrid//Traits//TButton;
    
    class CustomAction {
        use TButton;
    }

    After (v5.0.x):

    use Ublaboo\DataGrid\Traits\TButtonIcon;
    use Ublaboo\DataGrid\Traits\TButtonClass;
    use Ublaboo\DataGrid\Traits\TButtonText;
    use Ublaboo\DataGrid\Traits\TButtonTitle;
    
    class CustomAction {
        use TButtonIcon, TButtonClass, TButtonText, TButtonTitle;
    }

    2. Method Signature Changes (Medium Impact)

    If you are extending the DataGrid class, update the setDefaultSort method signature.

    Before (v4.x):

    public function setDefaultSort($sort)

    After (v5.0.x):

    public function setDefaultSort($sort, $use_on_reset = true)

    3. Exception Handling (Medium Impact)

    Replace generic DataGridException catches with specific exception types for better error handling.

    Before (v4.x):

    try {
        $column = $grid->getColumn($key);
    } catch (DataGridException $e) {
        // handle error
    }

    After (v5.0.x):

    try {
        $column = $grid->getColumn($key);
    } catch (DataGridColumnNotFoundException $e) {
        // handle missing column
    } catch (DataGridFilterNotFoundException $e) {
        // handle missing filter
    }

    4. Method Name Corrections (Low Impact)

    Fix the typo in the session management method.

    • Old: $grid->deleteSesssionData($key);
    • New: $grid->deleteSessionData($key);

    Migration Steps

    1. Update PHP version to 5.6+.
    2. Replace TButton trait with specific button traits in custom action classes.
    3. Update exception handling to catch DataGridColumnNotFoundException and DataGridFilterNotFoundException.
    4. Update setDefaultSort() signatures if extending the class.
    5. Fix the deleteSessionData() typo.
    6. Thoroughly test all existing functionality.
    // Example of the new trait usage in v5.0.x
    use Ublaboo\DataGrid\Traits\TButtonIcon;
    use Ublaboo\DataGrid\Traits\TButtonClass;
    use Ublaboo\DataGrid\Traits\TButtonText;
    use Ublaboo\DataGrid\Traits\TButtonTitle;
    
    class CustomAction {
        use TButtonIcon, TButtonClass, TButtonText, TButtonTitle;
    }
  6. Remove class aliases in v7.1.x

    master

    Class aliases for the Ublaboo namespace have been removed. You must use the Contributte namespace directly.

    Migration Steps:

    1. Replace all use Ublaboo\DataGrid\... statements with use Contributte\Datagrid\....
    // After (v7.1.x):
    use Contributte\Datagrid\DataGrid;
    use Contributte\Datagrid\Column\ColumnText;
  7. Upgrade from 5.x to 6.0.x

    master

    Version 6.0.x is a major modernization requiring PHP 7.2+ and Nette 3.0+. It introduces namespace changes and requires Bootstrap 4+.

    Breaking Changes:

    • Namespace Migration: All Ublaboo\DataGrid namespaces must be updated to Contributte\Datagrid.
    • Asset Structure: Bootstrap 3 is no longer supported; Bootstrap 4 or 5 is required.
    • Template System: Requires Latte 2.8+.
    • Method Signatures: Some signatures have changed for better type safety.

    Migration Steps:

    1. Update Composer: composer require contributte/datagrid:^6.0 (remove ublaboo/datagrid).
    2. Search and replace Ublaboo\\DataGrid with Contributte\\Datagrid in all imports and service configurations.
    3. Update Bootstrap to version 4 or 5.
    4. Update custom templates for Latte 2.8+ syntax.
    {
        "require": {
            "contributte/datagrid": "^6.0"
        }
    }
  8. Update asset imports in v7.1.x

    master

    Legacy asset names have been removed. Update your TypeScript/JavaScript imports and build scripts.

    Migration Steps:

    1. Update asset imports from datagrid-all.* to datagrid-full.*.
    2. Update any build scripts that reference the old asset names.
    // After (v7.1.x):
    import 'datagrid-full';
    import 'datagrid-full.min';
  9. Remove HappyInputs in v7.1.x

    master

    HappyInputs integration has been removed in v7.1.x. Form inputs now use standard Bootstrap 5 classes automatically.

    Migration Steps:

    1. Remove all calls to useHappyComponents() and shouldUseHappyComponents().
    2. Update your CSS if you were relying on HappyInputs-specific styling.
    // These methods no longer exist in v7.1.x
    // $grid->useHappyComponents(true);
    // $shouldUse = $grid->shouldUseHappyComponents();
  10. Upgrade from 7.0.x to 7.1.x

    master
    Version 7.1.x introduces a pluggable state storage system and removes several backward compatibility features that were deprecated in 7.0.x. Key changes include the removal of ublaboo_datagrid translation keys, removal of HappyInputs integration, renaming of session-related methods to storage-related methods, and removal of legacy asset names and class aliases.
  11. Manage group actions with GroupActionCollection

    master

    The GroupActionCollection class is used to manage and register various types of group actions within a Datagrid. Group actions allow users to perform operations on selected items via a form. The collection supports two main UI patterns:

    1. Button Actions: Direct buttons that trigger an onClick event.
    2. Select Actions: A dropdown menu (select box) that, when a value is chosen, reveals specific input fields (like text, textarea, or further select boxes) for that action, followed by a submit button.

    To use this, you typically add actions to the collection and then call addToFormContainer($container) to inject the necessary Nette Form components into your datagrid's form container.

    <?php
    
    // Example of adding different types of actions to a collection
    $collection = new GroupActionCollection($datagrid);
    
    // 1. A direct button action
    $collection->addGroupButtonAction('Delete selected', 'btn-danger');
    
    // 2. A select-based action with a text input
    $collection->addGroupSelectAction('Change Status', ['active' => 'Active', 'inactive' => 'Inactive']);
    $collection->getGroupAction('Change Status')->onSelect(function ($ids, $value) {
        // Handle the logic for the selected status
    });
    
    // 3. A select-based action with a text input (using addGroupTextAction for the sub-input)
    $collection->addGroupTextAction('Add Note');
    $collection->getGroupAction('Add Note')->onSelect(function ($ids, $value) {
        // $value will be the text entered in the text field
    });