├── .phpunit.result.cache ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── livewire-datatables.php ├── migration_to_v2.md ├── resources ├── images │ └── screenshot.png └── views │ ├── .DS_Store │ ├── github.blade.php │ ├── icons │ ├── arrow-circle-left.blade.php │ ├── arrow-left.blade.php │ ├── arrow-right.blade.php │ ├── check-circle.blade.php │ ├── chevron-down.blade.php │ ├── chevron-up.blade.php │ ├── cog.blade.php │ ├── copy.blade.php │ ├── excel.blade.php │ ├── trash.blade.php │ └── x-circle.blade.php │ └── livewire │ └── datatables │ ├── boolean.blade.php │ ├── checkbox.blade.php │ ├── complex-query-group.blade.php │ ├── complex-query-rule.blade.php │ ├── complex-query.blade.php │ ├── datatable.blade.php │ ├── delete.blade.php │ ├── editable.blade.php │ ├── filters │ ├── boolean.blade.php │ ├── checkbox.blade.php │ ├── date.blade.php │ ├── datetime.blade.php │ ├── editable.blade.php │ ├── number.blade.php │ ├── select.blade.php │ ├── string.blade.php │ └── time.blade.php │ ├── header-inline-hide.blade.php │ ├── header-no-hide.blade.php │ ├── hide-column-multiselect.blade.php │ ├── highlight.blade.php │ ├── label.blade.php │ ├── link.blade.php │ ├── style-width.blade.php │ ├── tailwind-pagination.blade.php │ ├── tailwind-simple-pagination.blade.php │ └── tooltip.blade.php └── src ├── .DS_Store ├── Action.php ├── BooleanColumn.php ├── Column.php ├── ColumnSet.php ├── Commands ├── ComponentParser.php ├── DatatableMakeCommand.php ├── MakeDatatableCommand.php ├── datatable-model.stub └── datatable.stub ├── DateColumn.php ├── DatetimeColumn.php ├── Exports └── DatatableExport.php ├── Http └── Controllers │ └── FileExportController.php ├── JsonColumn.php ├── LabelColumn.php ├── Livewire ├── ComplexQuery.php └── LivewireDatatable.php ├── LivewireDatatablesServiceProvider.php ├── NumberColumn.php ├── TimeColumn.php └── Traits ├── CanExport.php ├── CanPinRecords.php ├── WithCallbacks.php ├── WithPresetDateFilters.php └── WithPresetTimeFilters.php /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | {"version":1,"defects":{"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":3,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":3,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #0":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #1":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #2":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #3":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #4":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #5":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #6":3,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #7":3,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_delete_column":4,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #2":4,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":7,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":7,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_mount_using_the_class":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_set_a_default_sort":4,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_show_and_hide_a_column":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_order_results":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_text":8,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_boolean":8,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_strings_as_a_boolean":8,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_selects":8,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_numbers":8,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_base_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_one_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_a_has_one_column":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_column_with_specific_aggregate":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_has_many_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_belongs_to_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_many_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a__where_query_for_belongs_to_many_relation_columns":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mount_from_the_default_template_with_a_model":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_header_can_be_hidden_with_a_property":4,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_pagination_bar_can_be_hidden_with_a_property":4,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_per_page_with_a_property":4,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_include_columns_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_exclude_columns_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_hide_columns_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_date_format_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_time_format_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_sort_from_a_property":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::component_is_created_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::dot_nested_component_is_created_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::forward_slash_nested_component_is_created_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::multiword_component_is_created_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::pascal_case_component_is_automatically_converted_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command_on_nested_component":7,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::new_component_model_name_matches_option":7,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#0":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#1":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#2":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#3":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#4":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#5":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#6":8,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#7":8},"times":{"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_generate_an_array_of_columns_from_a_model":0.127,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #0":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #1":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #2":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #3":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #5":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #6":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #7":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_exclude_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_include_columns":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_rename_columns":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_table_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_scope":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_delete_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #0":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #1":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #2":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #3":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_mount_using_the_class":0.054,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_set_a_default_sort":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_show_and_hide_a_column":0.039,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_order_results":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_text":0.026,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_boolean":0.026,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_strings_as_a_boolean":0.029,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_selects":0.025,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_numbers":0.059,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_base_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_one_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_a_has_one_column":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_column_with_specific_aggregate":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_has_many_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_relation_columns":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_belongs_to_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_many_relation_columns":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a__where_query_for_belongs_to_many_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mount_from_the_default_template_with_a_model":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_header_can_be_hidden_with_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_pagination_bar_can_be_hidden_with_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_per_page_with_a_property":0.023,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_include_columns_from_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_exclude_columns_from_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_hide_columns_from_a_property":0.021,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_date_format_from_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_time_format_from_a_property":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_sort_from_a_property":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::component_is_created_by_make_command":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::dot_nested_component_is_created_by_make_command":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::forward_slash_nested_component_is_created_by_make_command":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::multiword_component_is_created_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::pascal_case_component_is_automatically_converted_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command_on_nested_component":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::new_component_model_name_matches_option":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::a_component_is_not_created_with_a_reserved_class_name":0.013,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_generate_an_array_of_columns_from_a_model":0.031,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #0":0.026,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #1":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #2":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #3":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #4":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #5":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #6":0.025,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #7":0.03,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_exclude_columns":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_include_columns":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_rename_columns":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_table_column":0.006,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_scope":0.006,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_delete_column":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #0":0.024,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #1":0.024,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #2":0.024,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #3":0.024,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #4":0.024,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":0.013,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":0.007,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_mount_using_the_class":0.085,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_set_a_default_sort":0.036,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_show_and_hide_a_column":0.037,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_order_results":0.042,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_text":0.071,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_boolean":0.068,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_strings_as_a_boolean":0.066,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_selects":0.069,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_numbers":0.107,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_base_columns":0.01,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_one_relation_columns":0.011,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_a_has_one_column":0.012,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_columns":0.01,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_column_with_specific_aggregate":0.011,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_has_many_relation_columns":0.012,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_relation_columns":0.01,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_belongs_to_relation_columns":0.012,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_many_relation_columns":0.012,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a__where_query_for_belongs_to_many_relation_columns":0.011,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mount_from_the_default_template_with_a_model":0.037,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_header_can_be_hidden_with_a_property":0.036,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_pagination_bar_can_be_hidden_with_a_property":0.032,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_per_page_with_a_property":0.342,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_include_columns_from_a_property":0.027,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_exclude_columns_from_a_property":0.035,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_hide_columns_from_a_property":0.036,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_date_format_from_a_property":0.037,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_time_format_from_a_property":0.038,"Arm092\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_sort_from_a_property":0.036,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::component_is_created_by_make_command":0.011,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::dot_nested_component_is_created_by_make_command":0.011,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::forward_slash_nested_component_is_created_by_make_command":0.013,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::multiword_component_is_created_by_make_command":0.01,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::pascal_case_component_is_automatically_converted_by_make_command":0.012,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command":0.011,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command_on_nested_component":0.012,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::new_component_model_name_matches_option":0.012,"Arm092\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::a_component_is_not_created_with_a_reserved_class_name":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#0":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#1":0.006,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#2":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#3":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#4":0.011,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#5":0.011,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#6":0.01,"Arm092\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model#7":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters#0":0.005,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters#1":0.005,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters#2":0.007,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters#3":0.006,"Arm092\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters#4":0.005}} -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | enabled: 4 | - concat_with_spaces 5 | 6 | disabled: 7 | - concat_without_spaces 8 | - single_class_element_per_statement 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `livewire-datatables` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX ( to be released in the future... ) 6 | 7 | - initial release 8 | 9 | ## 0.9.0 ( 2022-03-22 ) 10 | 11 | - Breaking Change: 'unsortable' has been renamed to 'sortable', which is more intuitive. Please adjust your overwritten views, if any (thyseus). 12 | 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Mark Salmon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Livewire Datatables 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/arm092/livewire-datatables.svg?style=flat-square)](https://packagist.org/packages/arm092/livewire-datatables) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/arm092/livewire-datatables.svg?style=flat-square)](https://packagist.org/packages/arm092/livewire-datatables) 5 | 6 | ### Features 7 | - Use a model or query builder to supply data 8 | - Mutate and format columns using preset or custom callbacks 9 | - Sort data using column or computed column 10 | - Filter using booleans, times, dates, selects or free text 11 | - Create complex combined filters using the [complex query builder](#complex-query-builder) 12 | - Show / hide columns 13 | - Column groups 14 | - Mass Action (Bulk) Support 15 | 16 | ## [Live Demo App](https://livewire-datatables.com) 17 | 18 | ## [Demo App Repo](https://github.com/MedicOneSystems/demo-livewire-datatables) 19 | 20 | ![screenshot](resources/images/screenshot.png "Screenshot") 21 | 22 | ## Requirements 23 | - [Laravel 10|11|12](https://laravel.com/docs/11.x) 24 | - [Livewire 3](https://laravel-livewire.com/) 25 | - [Tailwind](https://tailwindcss.com/) 26 | - [Alpine JS](https://github.com/alpinejs/alpine) 27 | 28 | ## Installation 29 | 30 | You can install the package via composer: 31 | 32 | ```bash 33 | composer require arm092/livewire-datatables 34 | ``` 35 | 36 | ### Optional 37 | You don't need to, but if you like you can publish the config file and blade template assets: 38 | ```bash 39 | php artisan vendor:publish --provider="Arm092\LivewireDatatables\LivewireDatatablesServiceProvider" 40 | ``` 41 | This will enable you to modify the blade views and apply your own styling, the datatables views will be published to resources/livewire/datatables. The config file contains the default time and date formats used throughout 42 | > - This can be useful if you're using Purge CSS on your project, to make sure all the livewire-datatables classes get included 43 | 44 | Several of the built-in dynamic components use Alpine JS, so to remove flickers on page load, make sure you have 45 | ```css 46 | [x-cloak] { 47 | display: none; 48 | } 49 | ``` 50 | somewhere in your CSS 51 | 52 | ## Basic Usage 53 | 54 | - Use the ```livewire-datatable``` component in your blade view, and pass in a model: 55 | ```html 56 | ... 57 | 58 | 59 | 60 | ... 61 | ``` 62 | 63 | ## Template Syntax 64 | - There are many ways to modify the table by passing additional properties into the component: 65 | ```html 66 | 72 | ``` 73 | 74 | *Attention*: Please note that having multiple datatables on the same page _or_ more than one datatable of the same 75 | type on different pages needs to have a unique `name` attribute assigned to each one so they do not conflict with each 76 | other as in the example above. 77 | 78 | ### Props 79 | | Property | Arguments | Result | Example | 80 | |---------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------|----| 81 | | **model** |*String* full model name| Define the base model for the table | ```model="App\Post"```| 82 | | **include** |*String\| Array* of column definitions |specify columns to be shown in table, label can be specified by using \| delimter | ```include="name, email, dob\|Birth Date, role"```| 83 | | **exclude** |*String\| Array* of column definitions |columns are excluded from table| ```:exclude="['created_at', 'updated_at']"```| 84 | | **hide** |*String\| Array* of column definitions |columns are present, but start hidden|```hidden="email_verified_at"```| 85 | | **dates** |*String\| Array* of column definitions [ and optional format in \ | delimited string]|column values are formatted as per the default date format, or format can be included in string with \| separator | ```:dates="['dob\|lS F y', 'created_at']"```| 86 | | **times** |*String\| Array* of column definitions [optional format in \ | delimited string]|column values are formatted as per the default time format, or format can be included in string with \| separator | ```'bedtime\|g:i A'```| 87 | | **searchable** |*String\| Array* of column names | Defines columns to be included in global search | ```searchable="name, email"```| 88 | | **sortIndex** |*String* of column definition [and optional 'asc' or 'desc' (default: 'desc') in \| delimited string] |Specifies the column and direction for initial table sort. Default is column 0 descending | ```sort="name\|asc"```| 89 | | **hide-header** |*Boolean* default: *false*| The top row of the table including the column titles is removed if this is ```true``` | | 90 | | **hide-pagination** |*Boolean* default: *false*| Pagination controls are removed if this is ```true``` | | 91 | | **per-page** |*Integer* default: 10| Number of rows per page | ```per-page="20"``` | 92 | | **exportable** |*Boolean* default: *false*| Allows table to be exported | `````` | 93 | | **hideable** | _String_ | gives ability to show/hide columns, accepts strings 'inline', 'buttons', or 'select' | `````` | 94 | | **buttonsSlot** | _String_ | blade view to be included immediately after the buttons at the top of the table in the component, which can therefore access public properties | | 95 | | **beforeTableSlot** | _String_ | blade view to be included immediately before the table in the component, which can therefore access public properties | | 96 | | **afterTableSlot** | _String_ | blade view to be included immediately after the table in the component, which can therefore access public properties | [demo](https://livewire-datatables.com/complex) | 97 | --- 98 | 99 | 100 | ## Component Syntax 101 | 102 | ### Create a livewire component that extends ```Arm092\LivewireDatatables\LivewireDatatable``` 103 | > ```php artisan make:livewire-datatable foo``` --> 'app/Livewire/Datatables/Foo.php' 104 | 105 | > ```php artisan make:livewire-datatable foo.bar``` --> 'app/Livewire/Datatables/Foo/Bar.php' 106 | 107 | ### Provide a datasource by declaring public property ```$model``` **OR** public method ```builder()``` that returns an instance of ```Illuminate\Database\Eloquent\Builder``` 108 | > ```php artisan make:livewire-datatable users-table --model=user``` --> 'app/Livewire/Datatables/UsersTable.php' with ```public $model = User::class``` 109 | 110 | ### Declare a public method ```columns``` that returns an array containing one or more ```Arm092\LivewireDatatables\Column``` 111 | 112 | ## Columns 113 | Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains. 114 | There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering: 115 | 116 | | Class | Description | 117 | |---|---| 118 | |Column|Generic string-based column. Filter will be a text input| 119 | |NumberColumn| Number-based column. Filters will be a numeric range| 120 | |BooleanColumn| Values will be automatically formatted to a yes/no icon, filters will be yes/no| 121 | |DateColumn| Values will be automatically formatted to the default date format. Filters will be a date range| 122 | |TimeColumn| Values will be automatically formatted to the default time format. Filters will be a time range| 123 | |LabelColumn| Fixed header string ("label") with fixed content string in every row. No SQL is executed at all| 124 | ___ 125 | 126 | ```php 127 | 128 | use Arm092\LivewireDatatables\Column; 129 | use Illuminate\Database\Eloquent\Model; 130 | use Illuminate\Database\Eloquent\Builder; 131 | use Arm092\LivewireDatatables\DateColumn; 132 | use Arm092\LivewireDatatables\LabelColumn; 133 | use Arm092\LivewireDatatables\NumberColumn; 134 | use Arm092\LivewireDatatables\BooleanColumn; 135 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 136 | 137 | class ComplexDemoTable extends LivewireDatatable 138 | { 139 | 140 | public function builder(): Builder 141 | { 142 | return User::query(); 143 | } 144 | 145 | public function getColumns(): array|Model 146 | { 147 | return [ 148 | NumberColumn::name('id') 149 | ->label('ID') 150 | ->linkTo('job', 6), 151 | 152 | BooleanColumn::name('email_verified_at') 153 | ->label('Email Verified') 154 | ->format() 155 | ->filterable(), 156 | 157 | Column::name('name') 158 | ->defaultSort('asc') 159 | ->group('group1') 160 | ->searchable() 161 | ->hideable() 162 | ->filterable(), 163 | 164 | Column::name('planet.name') 165 | ->label('Planet') 166 | ->group('group1') 167 | ->searchable() 168 | ->hideable() 169 | ->filterable($this->planets), 170 | 171 | // Column that counts every line from 1 upwards, independent of content 172 | Column::index($this); 173 | 174 | DateColumn::name('dob') 175 | ->label('DOB') 176 | ->group('group2') 177 | ->filterable() 178 | ->hide(), 179 | 180 | (new LabelColumn()) 181 | ->label('My custom heading') 182 | ->content('This fixed string appears in every row'), 183 | 184 | NumberColumn::name('dollars_spent') 185 | ->format(2, '.', ',') // 2 decimal places, comma for thousands, dot for decimal 186 | ->enableSummary(), 187 | ]; 188 | } 189 | } 190 | ``` 191 | 192 | ### Column Methods 193 | | Method | Arguments | Result | Example | 194 | |----|----|----|----| 195 | |_static_ **name**| *String* $column |Builds a column from column definition, which can be eith Eloquent or SQL dot notation (see below) |```Column::name('name')```| 196 | |_static_ **raw**| *String* $rawSqlStatement|Builds a column from raw SQL statement. Must include "... AS _alias_"|```Column::raw("CONCAT(ROUND(DATEDIFF(NOW(), users.dob) / planets.orbital_period, 1) AS `Native Age`")```| 197 | |_static_ **callback**|*Array\|String* $columns, *Closure\|String* $callback| Passes the columns from the first argument into the callback to allow custom mutations. The callback can be a method on the table class, or inline | _(see below)_| 198 | |_static_ **scope**|*String* $scope, *String* $alias|Builds a column from a scope on the parent model|```Column::scope('selectLastLogin', 'Last Login')```| 199 | |_static_ **delete**|[*String* $primaryKey default: 'id']|Adds a column with a delete button, which will call ```$this->model::destroy($primaryKey)```|```Column::delete()```| 200 | |_static_ **checkbox**|[*String* $column default: 'id']|Adds a column with a checkbox. The component public property ```$selected``` will contain an array of the named column from checked rows, |```Column::checkbox()```| 201 | |**label**|*String* $name|Changes the display name of a column|```Column::name('id')->label('ID)```| 202 | |**group**|*String* $group|Assign the column to a group. Allows to toggle the visibility of all columns of a group at once|```Column::name('id')->group('my-group')```| 203 | |**format**|[*String* $format]|Formats the column value according to type. Dates/times will use the default format or the argument |```Column::name('email_verified_at')->filterable(),```| 204 | |**hide**| |Marks column to start as hidden|```Column::name('id')->hidden()```| 205 | |**sortBy**|*String\|Expression* $column|Changes the query by which the column is sorted|```Column::name('dob')->sortBy('DATE_FORMAT(users.dob, "%m%d%Y")'),```| 206 | |**truncate**|[*Integer* $length (default: 16)]Truncates column to $length and provides full-text in a tooltip. Uses ```view('livewire-datatables::tooltip)```|```Column::name('biography)->truncate(30)```| 207 | |**linkTo**|*String* $model, [*Integer* $pad]|Replaces the value with a link to ```"/$model/$value"```. Useful for ID columns. Optional zero-padding. Uses ```view('livewire-datatables::link)```|```Column::name('id')->linkTo('user')```| 208 | |**link**|*String* $href, [*String* $slot]|Let the content of the column render as a link. You may use {{ }} syntax to fill the url with any attributes of the current row. Uses ```view('livewire-datatables::link)```|```Column::name('first_name')->link('/users/{{slug}}/edit', 'edit {{first_name}} {{last_name}}')```| 209 | |**round**|[*Integer* $precision (default: 0)]|Rounds value to given precision|```Column::name('age')->round()```| 210 | |**defaultSort**|[*String* $direction (default: 'desc')]|Marks the column as the default search column|```Column::name('name')->defaultSort('asc')```| 211 | |**searchable**| |Includes the column in the global search|```Column::name('name')->searchable()```| 212 | |**hideable**| |The user is able to toggle the visibility of this column|```Column::name('name')->hideable()```| 213 | |**filterable**|[*Array* $options], [*String* $filterScope]|Adds a filter to the column, according to Column type. If an array of options is passed it wil be used to populate a select input. If the column is a scope column then the name of the filter scope must also be passed|```Column::name('allegiance')->filterable(['Rebellion', 'Empire'])```| 214 | |**unwrap**| | Prevents the content of the column from being wrapped in multiple lines |```Column::name('oneliner')->unwrap()```| 215 | |**filterOn**|*String/Array* $statement|Allows you to specify a column name or sql statement upon which to perform the filter (must use SQL syntax, not Eloquent eg. ```'users.name'``` instead of ```'user.name'```). Useful if using a callback to modify the displayed values. Can pass a single string or array of strings which will be combined with ```OR```|```Column::callback(['name', 'allegiance'], function ($name, $allegiance) { return "$name is allied to $allegiance"; })->filterable(['Rebellion', 'Empire'])->filterOn('users.allegiance')```| 216 | |**view**|*String* $viewName| Passes the column value, whole row of values, and any additional parameters to a view template | _(see below)_| 217 | |**editable**| | Marks the column as editable | _(see below)_| 218 | |**alignCenter**| | Center-aligns column header _and_ contents |```Column::delete()->alignCenter()```| 219 | |**alignRight**| | Right-aligns column header _and_ contents |```Column::delete()->alignRight()```| 220 | |**contentAlignCenter**| | Center-aligns column contents |```Column::delete()->contentAlignCenter()```| 221 | |**contentAlignRight**| | Right-aligns column contents |```Column::delete()->contentAlignRight()```| 222 | |**headerAlignCenter**| | Center-aligns column header |```Column::delete()->headerAlignCenter()```| 223 | |**headerAlignRight**| | Right-aligns column header |```Column::delete()->headerAlignRight()```| 224 | |**editable**| | Marks the column as editable | _(see below)_| 225 | |**exportCallback**| Closure $callback | Reformats the result when exporting | _(see below)_ | 226 | |**excludeFromExport**| | Excludes the column from export |```Column::name('email')->excludeFromExport()```| 227 | |**unsortable**| | Prevents the column being sortable |```Column::name('email')->unsortable()```| 228 | ___ 229 | 230 | ### Listener 231 | The component will listen for the ```refreshLivewireDatatable``` event, which allows you to refresh the table from external components. 232 | 233 | ### Eloquent Column Names 234 | Columns from Eloquent relations can be included using the normal eloquent dot notation, eg. ```planet.name```, Livewire Datatables will automatically add the necessary table joins to include the column. If the relationship is of a 'many' type (```HasMany```, ```BelongsToMany```, ```HasManyThrough```) then Livewire Datatables will create an aggregated subquery (which is much more efficient than a join and group. Thanks [@reinink](https://eloquent-course.reinink.ca/)). By default, the aggregate type will be ```count``` for a numeric column and ```group_concat``` for a string column, but this can be over-ridden using a colon delimeter; 235 | 236 | ```php 237 | NumberColumn::name('students.age:sum')->label('Student Sum'), 238 | 239 | NumberColumn::name('students.age:avg')->label('Student Avg'), 240 | 241 | NumberColumn::name('students.age:min')->label('Student Min'), 242 | 243 | NumberColumn::name('students.age:max')->label('Student Max'), 244 | ``` 245 | 246 | ### Column Groups 247 | 248 | When you have a very big table with a lot of columns, it is possible to create 'column groups' that allows the user to toggle the visibility of a whole group at once. Use `->group('NAME')` at any column to achieve this. 249 | You can human-readable labels and translations of your groups via the `groupLabels` property of your table: 250 | 251 | ```php 252 | 253 | use Arm092\LivewireDatatables\Column; 254 | use Illuminate\Database\Eloquent\Model; 255 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 256 | 257 | class GroupDemoTable extends LivewireDatatable 258 | { 259 | public array $groupLabels = [ 260 | 'group1' => 'app.translation_for_group_1', 261 | 'group2' => 'app.translation_for_group_2', 262 | ]; 263 | 264 | public function getColumns(): array|Model 265 | { 266 | return [ 267 | Column::name('planets.name') 268 | ->group('group1') 269 | ->label('Planet'), 270 | 271 | Column::name('planets.name') 272 | ->group('group2') 273 | ->label('Planet'), 274 | ``` 275 | 276 | ### Summary row 277 | If you need to summarize all cells of a specific column, you can use `enableSummary()`: 278 | 279 | ```php 280 | public function getColumns(): array|Model 281 | { 282 | return [ 283 | Column::name('dollars_spent') 284 | ->label('Expenses in Dollar') 285 | ->enableSummary(), 286 | 287 | Column::name('euro_spent') 288 | ->label('Expenses in Euro') 289 | ->enableSummary(), 290 | ``` 291 | 292 | ### Mass (Bulk) Action 293 | 294 | If you want to be able to act upon several records at once, you can use the `buildActions()` method in your Table: 295 | 296 | ```php 297 | public function buildActions(): array 298 | { 299 | return [ 300 | 301 | Action::value('edit')->label('Edit Selected')->group('Default Options')->callback(function ($mode, $items) { 302 | // $items contains an array with the primary keys of the selected items 303 | }), 304 | 305 | Action::value('update')->label('Update Selected')->group('Default Options')->callback(function ($mode, $items) { 306 | // $items contains an array with the primary keys of the selected items 307 | }), 308 | 309 | Action::groupBy('Export Options', function () { 310 | return [ 311 | Action::value('csv')->label('Export CSV')->export('SalesOrders.csv'), 312 | Action::value('html')->label('Export HTML')->export('SalesOrders.html'), 313 | Action::value('xlsx')->label('Export XLSX')->export('SalesOrders.xlsx')->styles($this->exportStyles)->widths($this->exportWidths) 314 | ]; 315 | }), 316 | ]; 317 | } 318 | ``` 319 | 320 | ### Mass Action Style 321 | 322 | If you only have small style adjustments to the Bulk Action Dropdown you can adjust some settings here: 323 | 324 | ```php 325 | public function getExportStylesProperty(): array 326 | { 327 | return [ 328 | '1' => ['font' => ['bold' => true]], 329 | 'B2' => ['font' => ['italic' => true]], 330 | 'C' => ['font' => ['size' => 16]], 331 | ]; 332 | } 333 | 334 | public function getExportWidthsProperty(): array 335 | { 336 | return [ 337 | 'A' => 55, 338 | 'B' => 45, 339 | ]; 340 | } 341 | ``` 342 | 343 | ### Pin Records 344 | 345 | If you want to give your users the ability to pin specific records to be able to, for example, compare 346 | them with each other, you can use the CanPinRecords trait. Ensure to have at least one Checkbox Column 347 | so the user can select records: 348 | 349 | ```php 350 | 351 | use Arm092\LivewireDatatables\Column; 352 | use Illuminate\Database\Eloquent\Model; 353 | use Arm092\LivewireDatatables\Traits\CanPinRecords; 354 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 355 | 356 | class RecordTable extends LivewireDatatable 357 | { 358 | use CanPinRecords; 359 | 360 | public string|null|Model $model = Record::class; 361 | 362 | public function getColumns(): array|Model 363 | { 364 | return [ 365 | Column::checkbox(), 366 | 367 | // ... 368 | 369 | ``` 370 | 371 | ### Custom column names 372 | It is still possible to take full control over your table, you can define a ```builder``` method using whatever query you like, using your own joins, groups whatever, and then name your columns using your normal SQL syntax: 373 | 374 | ```php 375 | 376 | public function builder(): Builder 377 | { 378 | return User::query() 379 | ->leftJoin('planets', 'planets.id', 'users.planet_id') 380 | ->leftJoin('moons', 'moons.id', 'planets.moon_id') 381 | ->groupBy('users.id'); 382 | } 383 | 384 | public function getColumns(): array|Model 385 | { 386 | return [ 387 | NumberColumn::name('id') 388 | ->filterable(), 389 | 390 | Column::name('planets.name') 391 | ->label('Planet'), 392 | 393 | Column::raw('GROUP_CONCAT(planets.name SEPARATOR " | ") AS `Moon`') 394 | ->setCallback(static fn($value) => strtolower($value)) 395 | 396 | ... 397 | } 398 | 399 | ``` 400 | 401 | ### Callbacks 402 | Callbacks give you the freedom to perform any mutations you like on the data before displaying in the table. 403 | - The callbacks are performed on the paginated results of the database query, so shouldn't use a ton of memory 404 | - Callbacks will receive the chosen columns as their arguments. 405 | - Callbacks can be defined inline as below, or as public methods on the Datatable class, referenced by passing the name as a string as the second argument to the callback method. 406 | - If you want to format the result differently for export, use ```->exportCallback(Closure $callback)```. 407 | ```php 408 | 409 | use Arm092\LivewireDatatables\Column; 410 | use Illuminate\Database\Eloquent\Model; 411 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 412 | 413 | class CallbackDemoTable extends LivewireDatatable 414 | { 415 | public string|null|Model $model = User::class 416 | 417 | public function getColumns(): array|Model 418 | { 419 | return [ 420 | Column::name('users.id'), 421 | 422 | Column::name('users.dob')->format(), 423 | 424 | Column::callback(['dob', 'signup_date'], function ($dob, $signupDate) { 425 | $age = $signupDate->diffInYears($dob); 426 | return $age > 18 427 | ? '' . $age . '' 428 | : $age; 429 | })->exportCallback(function ($dob, $signupDate), { 430 | return $age = $signupDate->diffInYears($dob); 431 | }), 432 | 433 | ... 434 | } 435 | } 436 | ``` 437 | 438 | ### Default Filters 439 | 440 | If you want to have a default filter applied to your table, you can use the `defaultFilters` property. The `defaultFilter` should be an Array of column names and the default filter value to use for. When a persisted filter (`$this->persistFilters` is true and session values are available) is available, it will override the default filters. 441 | 442 | In the example below, the table will by default be filtered by rows where the _deleted_at_ column is false. If the user has a persisted filter for the _deleted_at_ column, the default filter will be ignored. 443 | 444 | ```php 445 | 446 | use Arm092\LivewireDatatables\Column; 447 | use Illuminate\Database\Eloquent\Model; 448 | use Illuminate\Database\Eloquent\Builder; 449 | use Arm092\LivewireDatatables\BooleanColumn; 450 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 451 | 452 | class CallbackDemoTable extends LivewireDatatable 453 | { 454 | public array $defaultFilters = [ 455 | 'deleted_at' => '0', 456 | ]; 457 | 458 | public function builder(): Builder 459 | { 460 | return User::query()->withTrashed(); 461 | } 462 | 463 | public function getColumns(): array|Model 464 | { 465 | return [ 466 | Column::name('id'), 467 | BooleanColumn::name('deleted_at')->filterable(), 468 | ]; 469 | } 470 | } 471 | ``` 472 | 473 | ### Views 474 | You can specify that a column's output is piped directly into a separate blade view template. 475 | - Template is specified using ususal laravel view helper syntax 476 | - Views will receive the column's value as ```$value```, and the whole query row as ```$row``` 477 | ```php 478 | 479 | use Arm092\LivewireDatatables\Column; 480 | use Illuminate\Database\Eloquent\Model; 481 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 482 | 483 | class CallbackDemoTable extends LivewireDatatable 484 | { 485 | public string|null|Model $model = User::class 486 | 487 | public function getColumns(): array|Model 488 | { 489 | return [ 490 | Column::name('users.id'), 491 | 492 | Column::name('users.dob')->view('tables.dateview'), 493 | 494 | Column::name('users.signup_date')->format(), 495 | ]; 496 | } 497 | ``` 498 | ```html 499 | 'tables/dateview.blade.php' 500 | 501 | 502 | 503 | ``` 504 | 505 | ### Editable Columns 506 | You can mark a column as editable using ```editable``` 507 | This uses the ```view()``` method above to pass the data into an Alpine/Livewire compnent that can directly update the underlying database data. Requires the column to have ```column``` defined using standard Laravel naming. This is included as an example. Much more comprehensive custom editable columns with validation etc can be built using the callback or view methods above. 508 | 509 | ```php 510 | 511 | use Arm092\LivewireDatatables\Column; 512 | use Illuminate\Database\Eloquent\Model; 513 | use Arm092\LivewireDatatables\Livewire\LivewireDatatable; 514 | 515 | class EditableTable extends LivewireDatatable 516 | { 517 | public string|null|Model $model = User::class; 518 | 519 | public function getColumns(): array|Model 520 | { 521 | return [ 522 | Column::name('id') 523 | ->label('ID') 524 | ->linkTo('job', 6), 525 | 526 | Column::name('email') 527 | ->editable(), 528 | 529 | ... 530 | ]; 531 | } 532 | } 533 | ``` 534 | 535 | # Complex Query Builder 536 | Just add ```$complex = true``` to your Datatable Class and all filterable columns will be available in the complex query builder. 537 | 538 | **Features** 539 | - Combine rules and groups of rules using AND/OR logic 540 | - Drag and drop rules around the interface 541 | 542 | ![image](https://user-images.githubusercontent.com/7000886/128855344-25035758-ca90-42d2-bd19-518c9de45148.png) 543 | --- 544 | **Persisting Queries** (Requires AlpineJS v3 with $persist plugin) 545 | - Add ```$persistComplexQuery = true``` to your class and queries will be stored in browser localstorage. 546 | - By default, the localstorage key will be the class name. You can provide your own by setting the public property ```$persistKey``` or overriding ```getPersistKeyProperty()``` on the Datatable Class 547 | - eg: for user-specific persistence: 548 | 549 | ```php 550 | public function getPersistKeyProperty() 551 | { 552 | return Auth::id() . '-' . parent::getPersistKeyProperty(); 553 | } 554 | ``` 555 | --- 556 | **Saving Queries** 557 | 558 | If you want to permanently save queries you must provide 3 methods for adding, deleting and retrieving your saved queries using whatever logic you like: 559 | 560 | - ```public function saveQuery(string $name, array $rules)``` 561 | - ```public function deleteQuery(int $id)``` 562 | - ```public function getSavedQueries()``` 563 | 564 | * In your save and delete methods, be sure to dispatch an ```updateSavedQueries``` livewire event and pass a fresh array of results (see example below) 565 | 566 | ### Example: 567 | This example shows saving queries using a conventional Laravel ComplexQuery model, that belongsTo a User 568 | 569 | ```php 570 | /* Migration */ 571 | 572 | use Illuminate\Database\Migrations\Migration; 573 | use Illuminate\Database\Schema\Blueprint; 574 | use Illuminate\Support\Facades\Schema; 575 | 576 | return new class extends Migration 577 | { 578 | public function up() 579 | { 580 | Schema::create('complex_queries', static function (Blueprint $table) { 581 | $table->id(); 582 | $table->unsignedInteger('user_id'); 583 | $table->string('table'); 584 | $table->json('rules'); 585 | $table->string('name'); 586 | $table->timestamps(); 587 | }); 588 | } 589 | 590 | public function down(): void 591 | { 592 | Schema::dropIfExists('complex_queries'); 593 | } 594 | }; 595 | 596 | 597 | /* Model */ 598 | 599 | class ComplexQuery extends Model 600 | { 601 | protected $casts = ['rules' => 'array']; 602 | 603 | public function user() 604 | { 605 | return $this->belongsTo(User::class); 606 | } 607 | } 608 | 609 | /* Datatable Class */ 610 | 611 | class TableWithSaving extends LivewireDatatable 612 | { 613 | ... 614 | 615 | public function saveQuery($name, $rules): void 616 | { 617 | Auth::user()->complex_queries()->create([ 618 | 'table' => $this->name, 619 | 'name' => $name, 620 | 'rules' => $rules 621 | ]); 622 | 623 | $this->dispatch('updateSavedQueries', $this->getSavedQueries()); 624 | } 625 | 626 | public function deleteQuery($id): void 627 | { 628 | ComplexQuery::destroy($id); 629 | 630 | $this->dispatch('updateSavedQueries', $this->getSavedQueries()); 631 | } 632 | 633 | public function getSavedQueries(): void 634 | { 635 | return Auth::user()->complex_queries()->where('table', $this->name)->get(); 636 | } 637 | 638 | ... 639 | } 640 | ``` 641 | 642 | 643 | # Styling 644 | I know it's not cool to provide a package with tons of opionated markup and styling. Most other packages seem to have gone down the route of passing optional classes around as arguments or config variables. My take is that because this is just blade with tailwind, you can publish the templates and do whatever you like to them - it should be obvious where the Livewire and Alpine moving parts are. 645 | 646 | There are methods for applying styles to rows and cells. ```rowClasses``` receives the ```$row``` and the [laravel loop variable](https://laravel.com/docs/8.x/blade#the-loop-variable) as parameters. ```cellClasses``` receives the ```$row``` and ```$column``` 647 | 648 | For example: 649 | ```php 650 | public function rowClasses($row, $loop): string 651 | { 652 | return 'divide-x divide-gray-100 text-sm text-gray-900 ' . ($this->rowIsSelected($row) ? 'bg-yellow-100' : ($row->{'car.model'} === 'Ferrari' ? 'bg-red-500' : ($loop->even ? 'bg-gray-100' : 'bg-gray-50'))); 653 | } 654 | 655 | public function cellClasses($row, $column): string 656 | { 657 | return 'text-sm ' . ($this->rowIsSelected($row) ? ' text-gray-900' : ($row->{'car.model'} === 'Ferrari' ? ' text-white' : ' text-gray-900')); 658 | } 659 | ``` 660 | 661 | You can change the default CSS classes applied by the ```rowClasses``` and the ```cellClasses``` methods by changing ```default_classes``` in the ```livewire-datatables.php``` config file. 662 | 663 | You could also override the render method in your table's class to provide different templates for different tables. 664 | 665 | 666 | ## Credits and Influences 667 | - [Laravel](https://laravel.com/) 668 | - [Laravel Livewire](https://laravel-livewire.com/docs/quickstart/) 669 | - [Tailwind CSS](https://tailwindcss.com/) 670 | - [AlpineJS](https://github.com/alpinejs/alpine) 671 | - [livewire-datatables by MedicOneSystems](https://github.com/MedicOneSystems/livewire-datatables) 672 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "arm092/livewire-datatables", 3 | "description": "Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS", 4 | "keywords": [ 5 | "php", 6 | "laravel", 7 | "livewire", 8 | "datatables", 9 | "tall" 10 | ], 11 | "homepage": "https://github.com/arm092/livewire-datatables", 12 | "license": "MIT", 13 | "type": "library", 14 | "authors": [ 15 | { 16 | "name": "Mark Salmon", 17 | "email": "mark.salmon@mediconesystems.com", 18 | "role": "Developer" 19 | }, 20 | { 21 | "name": "Shane Burrell", 22 | "email": "shane@shaneburrell.com", 23 | "role": "Developer" 24 | }, 25 | { 26 | "name": "Herbert Maschke", 27 | "email": "thyseus@pm.me", 28 | "role": "Developer" 29 | }, 30 | { 31 | "name": "Arman Khachatryan", 32 | "email": "arman.khachatryan@apricode.am", 33 | "role": "Developer" 34 | } 35 | ], 36 | "require": { 37 | "php": "^8.1", 38 | "illuminate/support": "^10.0|^11.0|^12.0", 39 | "livewire/livewire": "^3.6", 40 | "maatwebsite/excel": "^3.1.64", 41 | "psr/simple-cache": "^3.0.0", 42 | "reedware/laravel-relation-joins": "^7.0|^8.0" 43 | }, 44 | "require-dev": { 45 | "laravel/legacy-factories": "^1.4.1", 46 | "orchestra/testbench": "^8.0|^9.0|^10.0", 47 | "phpunit/phpunit": "^11.0" 48 | }, 49 | "autoload": { 50 | "psr-4": { 51 | "Arm092\\LivewireDatatables\\": "src" 52 | } 53 | }, 54 | "autoload-dev": { 55 | "psr-4": { 56 | "Arm092\\LivewireDatatables\\Tests\\": "tests" 57 | } 58 | }, 59 | "scripts": { 60 | "test": "vendor/bin/phpunit", 61 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 62 | }, 63 | "config": { 64 | "sort-packages": true 65 | }, 66 | "extra": { 67 | "laravel": { 68 | "providers": [ 69 | "Arm092\\LivewireDatatables\\LivewireDatatablesServiceProvider" 70 | ], 71 | "aliases": { 72 | "LivewireDatatables": "Arm092\\LivewireDatatables\\LivewireDatatablesFacade" 73 | } 74 | } 75 | }, 76 | "minimum-stability": "dev", 77 | "prefer-stable": true 78 | } 79 | -------------------------------------------------------------------------------- /config/livewire-datatables.php: -------------------------------------------------------------------------------- 1 | 'H:i', 15 | 'default_date_format' => 'd/m/Y', 16 | 'default_datetime_format' => 'd/m/Y H:i', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Default Carbon Formats 21 | |-------------------------------------------------------------------------- 22 | | The default formats that are used for TimeColumn & DateColumn. 23 | | You can use the formatting characters from the PHP DateTime class. 24 | | More info: https://www.php.net/manual/en/datetime.format.php 25 | | 26 | */ 27 | 28 | 'default_time_start' => '0000-00-00', 29 | 'default_time_end' => '9999-12-31', 30 | 31 | // Defaults that work with smalldatetime in SQL Server 32 | // 'default_time_start' => '1900-01-01', 33 | // 'default_time_end' => '2079-06-06', 34 | 35 | /* 36 | |-------------------------------------------------------------------------- 37 | | Surpress Search Highlights 38 | |-------------------------------------------------------------------------- 39 | | When enabled, matching text won't be highlighted in the search results 40 | | while searching. 41 | | 42 | */ 43 | 44 | 'suppress_search_highlights' => false, 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Per Page Options 49 | |-------------------------------------------------------------------------- 50 | | Sets the options to choose from in the `Per Page`dropdown. 51 | | 52 | */ 53 | 54 | 'per_page_options' => [10, 25, 50, 100], 55 | 56 | /* 57 | |-------------------------------------------------------------------------- 58 | | Default Per Page 59 | |-------------------------------------------------------------------------- 60 | | Sets the default amount of rows to display per page. 61 | | 62 | */ 63 | 64 | 'default_per_page' => 10, 65 | 66 | /* 67 | |-------------------------------------------------------------------------- 68 | | Model Namespace 69 | |-------------------------------------------------------------------------- 70 | | Sets the default namespace to be used when generating a new Datatables 71 | | component. 72 | | 73 | */ 74 | 75 | 'model_namespace' => 'App', 76 | 77 | /* 78 | |-------------------------------------------------------------------------- 79 | | Default Sortable 80 | |-------------------------------------------------------------------------- 81 | | Should a column of a datatable be sortable by default ? 82 | | 83 | */ 84 | 85 | 'default_sortable' => true, 86 | 87 | /* 88 | |-------------------------------------------------------------------------- 89 | | Default CSS classes 90 | |-------------------------------------------------------------------------- 91 | | 92 | | Sets the default classes that will be applied to each row and class 93 | | if the rowClasses() and cellClasses() functions are not overrided. 94 | | 95 | */ 96 | 97 | 'default_classes' => [ 98 | 'row' => [ 99 | 'even' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-gray-100', 100 | 'odd' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-gray-50', 101 | 'selected' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-yellow-100', 102 | ], 103 | 'cell' => 'whitespace-no-wrap text-sm text-gray-900 px-6 py-2', 104 | ], 105 | ]; 106 | -------------------------------------------------------------------------------- /migration_to_v2.md: -------------------------------------------------------------------------------- 1 | # Livewire Datatables Migrate Guide from 1st to 2nd version 2 | 3 | ### Impact Changes 4 | - Changed vendor's namespace from ```MedicOneSystems``` to ```Arm092``` 5 | - Changed default directory of data-tables from ```app/Http/Livewire``` to ```app/Livewire/Datatables``` 6 | - Added return types of most methods, if you have overwritten them, you may need to add the return type to your method 7 | - Defined types of most properties, if you have overwritten them, you may need to add the type to your property 8 | - In LivewireDatatable class some methods names changed because there were properties with the same name, so the method names were changed to avoid conflicts 9 | - As mentioned in previous point, `columns` method renamed to `getColumns` (most used) 10 | - View files need to be republished, if you have published ones 11 | - Also defined types of most properties of Column classes, if you have overwritten them, you may need to add the type to your property 12 | - Dropped support of `"illuminate/support"` under version 9.0 13 | - Minimal PHP version is 8.1 now 14 | - Minimal Laravel version is 9.0 now 15 | - Minimal Livewire version is 3.0 now 16 | 17 | If you have livewire 2 on your project, you need to upgrade it too. You can find the upgrade guide [here](https://livewire.laravel.com/docs/upgrading). 18 | Change `"livewire/livewire"` to version `"^3.0.0"`, `"arm092/livewire-datatables"` to version `"^2.0.0"` and run `composer update` command. 19 | After it, you can run `php artisan livewire:upgrade` command to upgrade your livewire components as mentioned in livewire migrate guide. 20 | 21 | #### If you have any problems with package, please open an issue on [github](https://github.com/arm092/livewire-datatables/issues) 22 | -------------------------------------------------------------------------------- /resources/images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arm092/livewire-datatables/58ee0aaf2af0cf5352d4beaf92e05436508ca1ed/resources/images/screenshot.png -------------------------------------------------------------------------------- /resources/views/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arm092/livewire-datatables/58ee0aaf2af0cf5352d4beaf92e05436508ca1ed/resources/views/.DS_Store -------------------------------------------------------------------------------- /resources/views/github.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-10 w-10 fill-current']) }} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120.78 117.79"> -------------------------------------------------------------------------------- /resources/views/icons/arrow-circle-left.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" 2 | stroke="currentColor"> 3 | 5 | -------------------------------------------------------------------------------- /resources/views/icons/arrow-left.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/arrow-right.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/check-circle.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/chevron-down.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/chevron-up.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/cog.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /resources/views/icons/copy.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/excel.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 384 512"> -------------------------------------------------------------------------------- /resources/views/icons/trash.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5']) }} fill="none" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/icons/x-circle.blade.php: -------------------------------------------------------------------------------- 1 | merge(['class' => 'h-5 w-5 stroke-current']) }} fill="none" viewBox="0 0 24 24" stroke="currentColor"> 2 | 3 | 4 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/boolean.blade.php: -------------------------------------------------------------------------------- 1 |
2 | @if($value) 3 | 4 | @else 5 | 6 | @endif 7 |
-------------------------------------------------------------------------------- /resources/views/livewire/datatables/checkbox.blade.php: -------------------------------------------------------------------------------- 1 |
2 | pinnedRecords)) checked @endif 8 | class="w-4 h-4 mt-1 text-blue-600 form-checkbox transition duration-150 ease-in-out" 9 | /> 10 |
11 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/complex-query-group.blade.php: -------------------------------------------------------------------------------- 1 |
2 | @foreach($rules as $index => $rule) 3 | @php $key = $parentIndex !== null ? $parentIndex . '.' . $index : $index; @endphp 4 |
5 | @if($rule['type'] === 'rule') 6 | @include('datatables::complex-query-rule', ['parentIndex' => $key, 'rule' => $rule]) 7 | @elseif($rule['type'] === 'group') 8 |
document.querySelector('[dragging]'), 11 | dragstart: (e, key) => { 12 | e.target.setAttribute('dragging', key) 13 | e.target.classList.add('bg-opacity-20', 'bg-white') 14 | }, 15 | dragend: (e) => { 16 | e.target.removeAttribute('dragging') 17 | e.target.classList.remove('bg-opacity-20', 'bg-white') 18 | }, 19 | dragenter(e) { 20 | if (e.target.closest('[drag-target]') !== this.source().closest('[drag-target]')) { 21 | console.log(this.$refs.placeholder) 22 | this.$refs.placeholder.appendChild(this.source()) 23 | } 24 | }, 25 | drop(e) { 26 | $wire.call('moveRule', this.source().getAttribute('dragging'), this.key) 27 | }, 28 | }" drag-target 29 | x-on:dragenter.prevent="dragenter" 30 | x-on:dragleave.prevent 31 | x-on:dragover.prevent 32 | x-on:drop="drop" 33 | class="p-4 space-y-4 bg-blue-500 bg-opacity-10 rounded-lg text-gray-{{ strlen($parentIndex) > 6 ? 1 : 9 }}00 border border-blue-400" 34 | > 35 | 36 | 37 | 38 | 39 |
40 |
41 | @if(count($rule['content']) > 1) 42 |
43 | 44 | 51 |
52 | @endif 53 |
54 |
55 |
56 | @include('datatables::complex-query-group', [ 57 | 'parentIndex' => $key, 58 | 'rules' => $rule['content'], 59 | 'logic' => $rule['logic'] 60 | ]) 61 |
62 |
63 |
64 | 65 | 66 |
67 | @unless($key === 0) 68 | 69 | @endunless 70 |
71 | 72 |
73 | @endif 74 |
75 | @endforeach 76 |
77 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/complex-query-rule.blade.php: -------------------------------------------------------------------------------- 1 |
2 | @php $key = collect(explode('.', $parentIndex))->join(".content.") . ".content" @endphp 3 |
10 |
11 |
12 | 14 |
15 | 22 |
23 |
24 | 25 | @if ($options = $this->getOperands($key)) 26 |
27 | 29 |
30 | 37 |
38 |
39 | @endif 40 | 41 | @if (!in_array($rule['content']['operand'], ['is empty', 'is not empty'])) 42 |
43 | @if ($column = $this->getRuleColumn($key)) 44 | 46 |
47 | @if (is_array($column['filterable'])) 48 | 63 | @elseif($column['type'] === 'boolean') 64 | 70 | @elseif($column['type'] === 'date') 71 | 73 | @elseif($column['type'] === 'time') 74 | 76 | @else 77 | 79 | @endif 80 |
81 | @endif 82 |
83 | @endif 84 |
85 |
86 | 90 | 94 |
95 |
96 |
97 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/complex-query.blade.php: -------------------------------------------------------------------------------- 1 |
12 |
13 | Query Builder 14 | @if($errors->any()) 15 |
You have missing values in your rules
16 | @endif
17 |
18 | 19 | @if(count($this->rules[0]['content'])) 20 |
{{ $this->rulesString }}@if($errors->any()) Invalid rules @endif
21 | @endif 22 | 23 |
@include('datatables::complex-query-group', ['rules' => $rules, 'parentIndex' => null])
24 | 25 | @if(count($this->rules[0]['content'])) 26 | @unless($errors->any()) 27 |
28 |
29 | {{-- --}} 30 |
31 |
32 | @isset($savedQueries) 33 |
40 | 41 | 46 |
47 | @endisset 48 | 52 |
53 |
54 | @endif 55 | 56 | @endif 57 | @if(count($savedQueries ?? [])) 58 |
59 |
Saved Queries
60 |
61 | @foreach($savedQueries as $saved) 62 |
63 | 64 | 68 |
69 | @endforeach 70 |
71 |
72 | @endif 73 |
74 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/datatable.blade.php: -------------------------------------------------------------------------------- 1 |
2 | @includeIf($beforeTableSlot) 3 |
4 |
5 |
6 | @if($this->searchableColumns()->count()) 7 |
8 |
9 |
10 | 11 | 12 | 13 |
14 | 15 |
16 | 19 |
20 |
21 |
22 | @endif 23 |
24 | 25 | @if($this->activeFilters) 26 | @lang('Filter active') 27 | @endif 28 | 29 |
30 | 31 | 32 | @if($this->activeFilters) 33 | 36 | @endif 37 | 38 | @if(count($this->massActionsOptions)) 39 |
40 | 41 | 57 | 61 |
62 | @endif 63 | 64 | @if($exportable) 65 |
68 | 70 |
71 | @endif 72 | 73 | @if($hideable === 'select') 74 | @include('datatables::hide-column-multiselect') 75 | @endif 76 | 77 | @foreach ($columnGroups as $name => $group) 78 | 82 | @endforeach 83 | @includeIf($buttonsSlot) 84 |
85 |
86 | 87 | @if($hideable === 'buttons') 88 |
89 | @foreach($this->columns as $index => $column) 90 | @if ($column['hideable']) 91 | 95 | @endif 96 | @endforeach 97 |
98 | @endif 99 | 100 |
101 |
102 |
103 | @unless($this->hideHeader) 104 |
105 | @foreach($this->columns as $index => $column) 106 | @if($hideable === 'inline') 107 | @include('datatables::header-inline-hide', ['column' => $column, 'sortIndex' => $sortIndex]) 108 | @elseif($column['type'] === 'checkbox') 109 | @unless($column['hidden']) 110 |
111 |
112 | {{ count($visibleSelected) }} 113 |
114 |
115 | @endunless 116 | @else 117 | @include('datatables::header-no-hide', ['column' => $column, 'sortIndex' => $sortIndex]) 118 | @endif 119 | @endforeach 120 |
121 | @endunless 122 |
123 | @foreach($this->columns as $index => $column) 124 | @if($column['hidden']) 125 | @if($hideable === 'inline') 126 |
127 | @endif 128 | @elseif($column['type'] === 'checkbox') 129 | @include('datatables::filters.checkbox') 130 | @elseif($column['type'] === 'label') 131 |
132 | {{ $column['label'] ?? '' }} 133 |
134 | @else 135 |
136 | @if($column['filterable']) 137 | @if( is_iterable($column['filterable']) ) 138 |
139 | @include('datatables::filters.select', ['index' => $index, 'name' => $column['label'], 'options' => $column['filterable']]) 140 |
141 | @else 142 |
143 | @include('datatables::filters.' . ($column['filterView'] ?? $column['type']), ['index' => $index, 'name' => $column['label']]) 144 |
145 | @endif 146 | @endif 147 |
148 | @endif 149 | @endforeach 150 |
151 | @foreach($this->results as $row) 152 |
153 | @foreach($this->columns as $column) 154 | @if($column['hidden']) 155 | @if($hideable === 'inline') 156 |
157 | @endif 158 | @elseif($column['type'] === 'checkbox') 159 | @include('datatables::checkbox', ['value' => $row->checkbox_attribute]) 160 | @elseif($column['type'] === 'label') 161 | @include('datatables::label') 162 | @else 163 | 164 |
165 | {!! $row->{$column['name']} !!} 166 |
167 | @endif 168 | @endforeach 169 |
170 | @endforeach 171 | 172 | @if ($this->hasSummaryRow()) 173 |
174 | @foreach($this->columns as $column) 175 | @unless($column['hidden']) 176 | @if ($column['summary']) 177 |
178 | {{ $this->summarize($column['name']) }} 179 |
180 | @else 181 |
182 | @endif 183 | @endunless 184 | @endforeach 185 |
186 | @endif 187 |
188 |
189 | @if($this->results->isEmpty()) 190 |

191 | {{ __("There's Nothing to show at the moment") }} 192 |

193 | @endif 194 |
195 | 196 | @unless($this->hidePagination) 197 |
198 |
199 | {{-- check if there is any data --}} 200 | @if(count($this->results)) 201 |
202 | 208 |
209 | 210 |
211 |
212 | {{ $this->results->links('datatables::tailwind-simple-pagination') }} 213 |
214 | 215 | 218 |
219 | 220 |
221 | {{__('Results')}} {{ $this->results->firstItem() }} - {{ $this->results->lastItem() }} {{__('of')}} 222 | {{ $this->results->total() }} 223 |
224 | @endif 225 |
226 |
227 | @endif 228 |
229 | 230 | @if($complex) 231 |
232 | 233 |
234 | @endif 235 | 236 | @includeIf($afterTableSlot) 237 | 238 | 239 |
240 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/delete.blade.php: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 | 6 |
8 |
12 |
13 |
14 | 15 |
22 | 31 |
32 |
33 |

34 | {{ __('Delete') }} {{ $value }} 35 |

36 |
37 |
38 | {{ __('Are you sure?')}} 39 |
40 |
41 | 42 | 45 | 46 | 47 | 50 | 51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/editable.blade.php: -------------------------------------------------------------------------------- 1 |
15 | 17 | 18 | 21 | 22 |
23 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/boolean.blade.php: -------------------------------------------------------------------------------- 1 |
2 | 13 | 14 |
15 | @isset($this->activeBooleanFilters[$index]) 16 | @if($this->activeBooleanFilters[$index] == 1) 17 | 22 | @elseif(strlen($this->activeBooleanFilters[$index]) > 0) 23 | 28 | @endif 29 | @endisset 30 |
31 |
32 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/checkbox.blade.php: -------------------------------------------------------------------------------- 1 |
4 |
{{ __('SELECT ALL') }}
5 |
6 | results->total() === count($visibleSelected)) checked @endif 11 | /> 12 |
13 |
14 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/date.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 5 |
6 | 9 |
10 |
11 |
12 | 14 |
15 | 18 |
19 |
20 |
21 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/datetime.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 7 |
8 | 11 |
12 |
13 |
14 | 18 |
19 | 22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/editable.blade.php: -------------------------------------------------------------------------------- 1 |
2 | 9 |
10 | @foreach($this->activeTextFilters[$index] ?? [] as $key => $value) 11 | 15 | @endforeach 16 |
17 |
18 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/number.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 10 |
11 | 14 |
15 |
16 | 17 |
18 | 25 |
26 | 29 |
30 |
31 |
32 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/select.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 23 |
24 | 25 |
26 | @foreach($this->activeSelectFilters[$index] ?? [] as $key => $value) 27 | 32 | @endforeach 33 |
34 |
35 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/string.blade.php: -------------------------------------------------------------------------------- 1 |
2 | 9 |
10 | @foreach($this->activeTextFilters[$index] ?? [] as $key => $value) 11 | 15 | @endforeach 16 |
17 |
18 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/filters/time.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 5 |
6 | 9 |
10 |
11 |
12 | 14 |
15 | 18 |
19 |
20 |
21 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/header-inline-hide.blade.php: -------------------------------------------------------------------------------- 1 | 16 | 45 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/header-no-hide.blade.php: -------------------------------------------------------------------------------- 1 | @unless($column['hidden']) 2 |
5 | @if($column['sortable']) 6 | 18 | @else 19 |
20 | {{ str_replace('_', ' ', $column['label']) }} 21 |
22 | @endif 23 |
24 | @endif 25 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/hide-column-multiselect.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 8 |
9 |
10 | @foreach($this->columns as $index => $column) 11 | @if ($column['hideable'] !== false) 12 |
13 | 23 | 33 |
34 | @endif 35 | @endforeach 36 |
37 |
38 |
39 |
40 | 41 | 55 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/highlight.blade.php: -------------------------------------------------------------------------------- 1 | {{ $slot }} -------------------------------------------------------------------------------- /resources/views/livewire/datatables/label.blade.php: -------------------------------------------------------------------------------- 1 | 2 |
3 | {!! $column['content'] ?? '' !!} 4 |
5 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/link.blade.php: -------------------------------------------------------------------------------- 1 | {{ $slot }} -------------------------------------------------------------------------------- /resources/views/livewire/datatables/style-width.blade.php: -------------------------------------------------------------------------------- 1 | style=" 2 | @isset($column['width']) width:{{ $column['width'] }}; @endisset 3 | @isset($column['minWidth']) min-width:{{ $column['minWidth'] }}; @endisset 4 | @isset($column['maxWidth']) max-width:{{ $column['maxWidth'] }}; @endisset 5 | " 6 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/tailwind-pagination.blade.php: -------------------------------------------------------------------------------- 1 | 51 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/tailwind-simple-pagination.blade.php: -------------------------------------------------------------------------------- 1 |
2 | 3 | @if ($paginator->onFirstPage()) 4 |
5 | 6 | {{ __('Previous')}} 7 |
8 | @else 9 | 13 | @endif 14 | 15 | 16 | 17 | @if ($paginator->hasMorePages()) 18 | 22 | @else 23 |
24 | {{ __('Next')}} 25 | 26 |
27 | @endif 28 |
29 | -------------------------------------------------------------------------------- /resources/views/livewire/datatables/tooltip.blade.php: -------------------------------------------------------------------------------- 1 | 2 | {{ Str::limit($slot, $length) }} 3 | 4 | -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arm092/livewire-datatables/58ee0aaf2af0cf5352d4beaf92e05436508ca1ed/src/.DS_Store -------------------------------------------------------------------------------- /src/Action.php: -------------------------------------------------------------------------------- 1 | $method, $args); 24 | } 25 | 26 | throw new BadMethodCallException(sprintf( 27 | 'Method %s::%s does not exist.', static::class, $method 28 | )); 29 | } 30 | 31 | public static function value($value): static 32 | { 33 | $action = new static; 34 | $action->value = $value; 35 | 36 | return $action; 37 | } 38 | 39 | public function label($label): static 40 | { 41 | $this->label = $label; 42 | 43 | return $this; 44 | } 45 | 46 | public function group($group): static 47 | { 48 | $this->group = $group; 49 | 50 | return $this; 51 | } 52 | 53 | public static function groupBy($group, $actions): ?Collection 54 | { 55 | if ($actions instanceof Closure) { 56 | return collect($actions())->each(function ($item) use ($group) { 57 | $item->group = $group; 58 | }); 59 | } 60 | 61 | return null; 62 | } 63 | 64 | public function export($fileName): static 65 | { 66 | $this->fileName = $fileName; 67 | $this->isExport(); 68 | 69 | return $this; 70 | } 71 | 72 | public function isExport($isExport = true): static 73 | { 74 | $this->isExport = $isExport; 75 | 76 | return $this; 77 | } 78 | 79 | public function styles($styles): static 80 | { 81 | $this->styles = $styles; 82 | 83 | return $this; 84 | } 85 | 86 | public function widths($widths): static 87 | { 88 | $this->widths = $widths; 89 | 90 | return $this; 91 | } 92 | 93 | public function callback($callable): static 94 | { 95 | $this->callable = $callable; 96 | 97 | return $this; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/BooleanColumn.php: -------------------------------------------------------------------------------- 1 | callbackFunction = static fn ($value) => view('datatables::boolean', ['value' => $value]); 17 | 18 | $this->exportCallback = static fn ($value) => $value ? 'Yes' : 'No'; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Column.php: -------------------------------------------------------------------------------- 1 | sortable = config('livewire-datatables.default_sortable', true); 68 | } 69 | 70 | public static function name($name): static 71 | { 72 | $column = new static; 73 | $column->name = $name; 74 | $column->aggregate = Str::contains($name, ':') ? Str::after($name, ':') : $column->aggregate(); 75 | $column->label = (string)Str::of($name)->after('.')->ucfirst()->replace('_', ' '); 76 | 77 | if (Str::contains(Str::lower($name), ' as ')) { 78 | $column->name = array_reverse(preg_split('/ as /i', $name))[0]; 79 | $column->label = array_reverse(preg_split('/ as /i', $name))[1]; 80 | $column->base = preg_split('/ as /i', $name)[0]; 81 | } 82 | 83 | return $column; 84 | } 85 | 86 | public static function index(LivewireDatatable $datatable, $attribute = 'id'): static 87 | { 88 | $column = new static; 89 | $column->name = $attribute; 90 | $column->label = '#'; 91 | $column->callbackFunction = static function () use ($datatable) { 92 | return $datatable->getPage() * $datatable->perPage - $datatable->perPage + $datatable->row++; 93 | }; 94 | 95 | return $column; 96 | } 97 | 98 | public static function raw($raw): static 99 | { 100 | $column = new static; 101 | $column->raw = $raw; 102 | $column->name = Str::after($raw, ' AS '); 103 | $column->select = DB::raw(Str::before($raw, ' AS ')); 104 | $column->label = (string)Str::of($raw)->afterLast(' AS ')->replace('`', ''); 105 | $column->sort = (string)Str::of($raw)->beforeLast(' AS '); 106 | 107 | return $column; 108 | } 109 | 110 | /** 111 | * Make a callback function. 112 | * 113 | * @param array|string $columns The (comma separated) columns that should be retrieved from the database. 114 | * Is being translated directly into the `.sql`. 115 | * @param Closure|string $callback A callback that defines how the retrieved columns are processed. 116 | * @param array $params Optional additional parameters that are passed to the given Closure. 117 | * @param string|null $callbackName string Optional string that defines the 'name' of the column. 118 | * Leave empty to let livewire autogenerate a distinct value. 119 | * @return Column 120 | */ 121 | public static function callback( 122 | array|string $columns, 123 | Closure|string $callback, 124 | array $params = [], 125 | ?string $callbackName = null 126 | ): static 127 | { 128 | $column = new static; 129 | 130 | $column->name = 'callback_' . ($callbackName ?? (string)crc32(json_encode(func_get_args()))); 131 | $column->callbackFunction = $callback; 132 | $column->additionalSelects = is_array($columns) ? $columns : array_map('trim', explode(',', $columns)); 133 | $column->params = $params; 134 | 135 | return $column; 136 | } 137 | 138 | public static function checkbox($attribute = 'id'): static 139 | { 140 | return static::name($attribute . ' as checkbox_attribute') 141 | ->setType('checkbox') 142 | ->excludeFromExport(); 143 | } 144 | 145 | public static function scope(string $scope, $alias): static 146 | { 147 | $column = new static; 148 | $column->scope = $scope; 149 | $column->name = $alias; 150 | $column->label = $alias; 151 | $column->sortBy("`$alias`"); 152 | 153 | return $column; 154 | } 155 | 156 | public function setScope(string $scope): static 157 | { 158 | $this->scope = $scope; 159 | 160 | return $this; 161 | } 162 | 163 | public static function delete($name = 'id'): static 164 | { 165 | return static::callback($name, static function ($value) { 166 | return view('datatables::delete', ['value' => $value]); 167 | }); 168 | } 169 | 170 | public function setCallback(Closure|string $callback, array $params = []): static 171 | { 172 | $this->callbackFunction = $callback; 173 | $this->params = $params; 174 | 175 | return $this; 176 | } 177 | 178 | public function label($label): static 179 | { 180 | $this->label = $label; 181 | 182 | return $this; 183 | } 184 | 185 | public function wrap(): static 186 | { 187 | $this->wrappable = true; 188 | 189 | return $this; 190 | } 191 | 192 | public function unwrap(): static 193 | { 194 | $this->wrappable = false; 195 | 196 | return $this; 197 | } 198 | 199 | public function enableSummary(): static 200 | { 201 | $this->summary = true; 202 | 203 | return $this; 204 | } 205 | 206 | public function disableSummary(): static 207 | { 208 | $this->summary = false; 209 | 210 | return $this; 211 | } 212 | 213 | public function setIndex($index): static 214 | { 215 | $this->index = $index; 216 | 217 | return $this; 218 | } 219 | 220 | public function tooltip($text, $label = null): static 221 | { 222 | $this->tooltip = [ 223 | 'text' => $text, 224 | 'label' => $label, 225 | ]; 226 | 227 | return $this; 228 | } 229 | 230 | public function sortBy($column): static 231 | { 232 | $this->sort = $column; 233 | 234 | return $this; 235 | } 236 | 237 | public function defaultSort($direction = true): static 238 | { 239 | $this->defaultSort = $direction; 240 | 241 | return $this; 242 | } 243 | 244 | public function hideable($hideable = true): static 245 | { 246 | $this->hideable = $hideable; 247 | 248 | return $this; 249 | } 250 | 251 | public function sortable(): static 252 | { 253 | $this->sortable = true; 254 | 255 | return $this; 256 | } 257 | 258 | public function unsortable(): static 259 | { 260 | $this->sortable = false; 261 | 262 | return $this; 263 | } 264 | 265 | public function searchable(): static 266 | { 267 | $this->searchable = true; 268 | 269 | return $this; 270 | } 271 | 272 | public function filterable(array|Collection $options = [], ?string $scopeFilter = null): static 273 | { 274 | $options = $options instanceof Collection ? $options->toArray() : $options; 275 | $this->filterable = $options ?: true; 276 | $this->scopeFilter = $scopeFilter; 277 | 278 | return $this; 279 | } 280 | 281 | public function filterOn($query): static 282 | { 283 | $this->filterOn = $query; 284 | 285 | return $this; 286 | } 287 | 288 | public function booleanFilterable(): static 289 | { 290 | $this->filterable = true; 291 | $this->filterView = 'boolean'; 292 | 293 | return $this; 294 | } 295 | 296 | public function exportCallback($exportCallback): static 297 | { 298 | $this->exportCallback = $exportCallback; 299 | 300 | return $this; 301 | } 302 | 303 | public function excludeFromExport(): static 304 | { 305 | $this->preventExport = true; 306 | 307 | return $this; 308 | } 309 | 310 | public function linkTo($model, $pad = null): static 311 | { 312 | $this->callbackFunction = static function ($value) use ($model, $pad) { 313 | return view('datatables::link', [ 314 | 'href' => url("/$model/$value"), 315 | 'slot' => $pad ? str_pad($value, $pad, '0', STR_PAD_LEFT) : $value, 316 | ]); 317 | }; 318 | 319 | $this->exportCallback = static function ($value) { 320 | return $value; 321 | }; 322 | 323 | return $this; 324 | } 325 | 326 | public function link($href, $slot = null): static 327 | { 328 | $this->callbackFunction = static function ($caption, $row) use ($href, $slot) { 329 | $substitutes = ['{{caption}}' => $caption]; 330 | 331 | foreach ($row as $attribute => $value) { 332 | $substitutes["{{{$attribute}}}"] = $value; 333 | } 334 | 335 | if (!$slot) { 336 | $slot = $caption; 337 | } 338 | 339 | return view('datatables::link', [ 340 | 'href' => strtr($href, $substitutes), 341 | 'slot' => strtr($slot, $substitutes), 342 | ]); 343 | }; 344 | 345 | $this->exportCallback = static function ($value) { 346 | return $value; 347 | }; 348 | 349 | return $this; 350 | } 351 | 352 | public function truncate($length = 16): static 353 | { 354 | $this->callbackFunction = static function ($value) use ($length) { 355 | return view('datatables::tooltip', ['slot' => $value, 'length' => $length]); 356 | }; 357 | 358 | $this->exportCallback = static function ($value) { 359 | return $value; 360 | }; 361 | 362 | return $this; 363 | } 364 | 365 | public function round($precision = 0): static 366 | { 367 | $this->callbackFunction = static function ($value) use ($precision) { 368 | return $value ? round($value, $precision) : null; 369 | }; 370 | 371 | return $this; 372 | } 373 | 374 | public function view($view, $data = []): static 375 | { 376 | $this->callbackFunction = static function ($value, $row) use ($view, $data) { 377 | return view($view, ['value' => $value, 'row' => $row, ...$data]); 378 | }; 379 | 380 | $this->exportCallback = static function ($value) { 381 | return $value; 382 | }; 383 | 384 | return $this; 385 | } 386 | 387 | public function filterView($view): static 388 | { 389 | $this->filterView = $view; 390 | 391 | return $this; 392 | } 393 | 394 | public function additionalSelects($selects): static 395 | { 396 | $this->additionalSelects = is_array($selects) ? $selects : array_map('trim', explode(',', $selects)); 397 | 398 | return $this; 399 | } 400 | 401 | public function editable($editable = true): static 402 | { 403 | $this->exportCallback = static function ($value) { 404 | return $value; 405 | }; 406 | 407 | return $editable ? $this->setType('editable') : $this; 408 | } 409 | 410 | public function isEditable(): bool 411 | { 412 | return $this->type === 'editable'; 413 | } 414 | 415 | public function hide(): static 416 | { 417 | $this->hidden = true; 418 | 419 | return $this; 420 | } 421 | 422 | public function alignRight(): static 423 | { 424 | $this->headerAlign = 'right'; 425 | $this->contentAlign = 'right'; 426 | 427 | return $this; 428 | } 429 | 430 | public function alignCenter(): static 431 | { 432 | $this->headerAlign = 'center'; 433 | $this->contentAlign = 'center'; 434 | 435 | return $this; 436 | } 437 | 438 | public function headerAlignRight(): static 439 | { 440 | $this->headerAlign = 'right'; 441 | 442 | return $this; 443 | } 444 | 445 | public function contentAlignRight(): static 446 | { 447 | $this->contentAlign = 'right'; 448 | 449 | return $this; 450 | } 451 | 452 | public function headerAlignCenter(): static 453 | { 454 | $this->headerAlign = 'center'; 455 | 456 | return $this; 457 | } 458 | 459 | public function contentAlignCenter(): static 460 | { 461 | $this->contentAlign = 'center'; 462 | 463 | return $this; 464 | } 465 | 466 | public function toggleHidden(): void 467 | { 468 | $this->hidden = !$this->hidden; 469 | } 470 | 471 | public function toArray(): array 472 | { 473 | return get_object_vars($this); 474 | } 475 | 476 | public function aggregate(): string 477 | { 478 | return $this->type === 'string' 479 | ? 'group_concat' 480 | : 'count'; 481 | } 482 | 483 | public function isBaseColumn(): bool 484 | { 485 | return !Str::startsWith($this->name, 'callback_') && !Str::contains($this->name, '.') && !$this->raw; 486 | } 487 | 488 | public function field(): string 489 | { 490 | return Str::afterLast($this->name, '.'); 491 | } 492 | 493 | /** 494 | * You can use group(null) to revoke a column from a group, if necessary. 495 | * 496 | * @see array LivewireDatatable->groupLabels to assign a human readable and translatable label for the group 497 | */ 498 | public function group($group): static 499 | { 500 | $this->group = $group; 501 | 502 | return $this; 503 | } 504 | 505 | public function relations(): ?Collection 506 | { 507 | return $this->isBaseColumn() ? null : collect(explode('.', Str::beforeLast($this->name, '.'))); 508 | } 509 | 510 | public function isType($type): bool 511 | { 512 | return $type === $this->type; 513 | } 514 | 515 | public function setType($type): static 516 | { 517 | $this->type = $type; 518 | 519 | return $this; 520 | } 521 | 522 | public function addParams($params): static 523 | { 524 | $this->params = $params; 525 | 526 | return $this; 527 | } 528 | 529 | public function width($width): static 530 | { 531 | if (preg_match('/^\\d*\\.?\\d+$/i', $width) === 1) { 532 | $width .= 'px'; 533 | } 534 | 535 | if (preg_match('/^(\\d*\\.?\\d+)\\s?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vmin|vmax|%+)$/i', $width) === 0) { 536 | return $this; 537 | } 538 | 539 | $this->width = $width; 540 | 541 | return $this; 542 | } 543 | 544 | public function minWidth($minWidth): static 545 | { 546 | if (preg_match('/^\\d*\\.?\\d+$/i', $minWidth) === 1) { 547 | $minWidth .= 'px'; 548 | } 549 | 550 | if (preg_match('/^(\\d*\\.?\\d+)\\s?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vmin|vmax|%+)$/i', $minWidth) === 0) { 551 | return $this; 552 | } 553 | 554 | $this->minWidth = $minWidth; 555 | 556 | return $this; 557 | } 558 | 559 | public function maxWidth($maxWidth): static 560 | { 561 | if (preg_match('/^\\d*\\.?\\d+$/i', $maxWidth) === 1) { 562 | $maxWidth .= 'px'; 563 | } 564 | 565 | if (preg_match('/^(\\d*\\.?\\d+)\\s?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vmin|vmax|%+)$/i', $maxWidth) === 0) { 566 | return $this; 567 | } 568 | 569 | $this->maxWidth = $maxWidth; 570 | 571 | return $this; 572 | } 573 | 574 | public function offsetExists(mixed $offset): bool 575 | { 576 | return property_exists($this, $offset) && !is_null($this->{$offset}); 577 | } 578 | 579 | public function offsetGet(mixed $offset): mixed 580 | { 581 | return $this->{$offset}; 582 | } 583 | 584 | public function offsetSet(mixed $offset, mixed $value): void 585 | { 586 | $this->{$offset} = $value; 587 | } 588 | 589 | public function offsetUnset(mixed $offset): void 590 | { 591 | $this->{$offset} = null; 592 | } 593 | } 594 | -------------------------------------------------------------------------------- /src/ColumnSet.php: -------------------------------------------------------------------------------- 1 | columns = $columns; 15 | } 16 | 17 | public static function build($input): static 18 | { 19 | return is_array($input) 20 | ? self::fromArray($input) 21 | : self::fromModelInstance($input); 22 | } 23 | 24 | public static function fromModelInstance($model): static 25 | { 26 | return new static( 27 | collect($model->getAttributes())->keys()->reject(function ($name) use ($model) { 28 | return in_array($name, $model->getHidden()); 29 | })->map(function ($attribute, $index) { 30 | return Column::name($attribute)->setIndex($index); 31 | }) 32 | ); 33 | } 34 | 35 | public static function fromArray($columns): static 36 | { 37 | return new static(collect(static::squeezeIndex($columns))); 38 | } 39 | 40 | /** 41 | * Takes an array of columns and squeezes the consecutive index inside each element. 42 | */ 43 | public static function squeezeIndex($columns) 44 | { 45 | foreach ($columns as $index => $column) { 46 | $column->setIndex($index); 47 | } 48 | 49 | return $columns; 50 | } 51 | 52 | public function include($include): static 53 | { 54 | if (! $include) { 55 | return $this; 56 | } 57 | 58 | $include = collect(is_array($include) ? $include : array_map('trim', explode(',', $include))); 59 | $this->columns = $include->map(function ($column) { 60 | return Str::contains($column, '|') 61 | ? Column::name(Str::before($column, '|'))->label(Str::after($column, '|')) 62 | : Column::name($column); 63 | }); 64 | 65 | return $this; 66 | } 67 | 68 | public function exclude($exclude): static 69 | { 70 | if (! $exclude) { 71 | return $this; 72 | } 73 | 74 | $exclude = is_array($exclude) ? $exclude : array_map('trim', explode(',', $exclude)); 75 | 76 | $this->columns = $this->columns->reject(function ($column) use ($exclude) { 77 | return in_array(Str::after($column->name, '.'), $exclude); 78 | }); 79 | 80 | return $this; 81 | } 82 | 83 | public function hide($hidden): static 84 | { 85 | if (! $hidden) { 86 | return $this; 87 | } 88 | $hidden = is_array($hidden) ? $hidden : array_map('trim', explode(',', $hidden)); 89 | $this->columns->each(function ($column) use ($hidden) { 90 | $column->hidden = in_array(Str::after($column->name, '.'), $hidden, true); 91 | }); 92 | 93 | return $this; 94 | } 95 | 96 | public function formatDates($dates): static 97 | { 98 | $dates = is_array($dates) ? $dates : array_map('trim', explode(',', $dates)); 99 | 100 | $this->columns = $this->columns->map(function ($column) use ($dates) { 101 | foreach ($dates as $date) { 102 | if ($column->name === Str::before($date, '|')) { 103 | $format = Str::of($date)->contains('|') ? Str::after($date, '|') : null; 104 | 105 | return DateColumn::name($column->name)->format($format); 106 | } 107 | } 108 | 109 | return $column; 110 | }); 111 | 112 | return $this; 113 | } 114 | 115 | public function formatTimes($times): static 116 | { 117 | $times = is_array($times) ? $times : array_map('trim', explode(',', $times)); 118 | 119 | $this->columns = $this->columns->map(function ($column) use ($times) { 120 | foreach ($times as $time) { 121 | if (Str::after($column->name, '.') === Str::before($time, '|')) { 122 | $format = Str::of($time)->contains('|') ? Str::after($time, '|') : null; 123 | 124 | return TimeColumn::name($column->name)->format($format); 125 | } 126 | } 127 | 128 | return $column; 129 | }); 130 | 131 | return $this; 132 | } 133 | 134 | public function search($searchable): static 135 | { 136 | if (! $searchable) { 137 | return $this; 138 | } 139 | 140 | $searchable = is_array($searchable) ? $searchable : array_map('trim', explode(',', $searchable)); 141 | $this->columns->each(function ($column) use ($searchable) { 142 | $column->searchable = in_array($column->name, $searchable, true); 143 | }); 144 | 145 | return $this; 146 | } 147 | 148 | public function sort($sort): static 149 | { 150 | if ($sort && $column = $this->columns->first(function ($column) use ($sort) { 151 | return Str::after($column->name, '.') === Str::before($sort, '|'); 152 | })) { 153 | /** @var Column $column */ 154 | $column->defaultSort(Str::of($sort)->contains('|') ? Str::after($sort, '|') : null); 155 | } 156 | 157 | return $this; 158 | } 159 | 160 | public function columns(): Collection 161 | { 162 | return collect($this->columns); 163 | } 164 | 165 | public function columnsArray(): array 166 | { 167 | return $this->columns()->toArray(); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/Commands/ComponentParser.php: -------------------------------------------------------------------------------- 1 | model = $model; 19 | 20 | $this->baseClassNamespace = $classNamespace; 21 | 22 | $classPath = static::generatePathFromNamespace($classNamespace); 23 | 24 | $this->baseClassPath = rtrim($classPath, DIRECTORY_SEPARATOR) . '/'; 25 | 26 | $directories = preg_split('/[.\/]+/', $rawCommand); 27 | 28 | $camelCase = Str::camel(array_pop($directories)); 29 | $kebabCase = Str::kebab($camelCase); 30 | 31 | $this->component = $kebabCase; 32 | $this->componentClass = Str::studly($this->component); 33 | 34 | $this->directories = array_map([Str::class, 'studly'], $directories); 35 | } 36 | 37 | public function component() 38 | { 39 | return $this->component; 40 | } 41 | 42 | public function classPath() 43 | { 44 | return $this->baseClassPath . collect() 45 | ->concat($this->directories) 46 | ->push($this->classFile()) 47 | ->implode('/'); 48 | } 49 | 50 | public function relativeClassPath() 51 | { 52 | return Str::replaceFirst(base_path() . DIRECTORY_SEPARATOR, '', $this->classPath()); 53 | } 54 | 55 | public function classFile() 56 | { 57 | return $this->componentClass . '.php'; 58 | } 59 | 60 | public function classNamespace() 61 | { 62 | return empty($this->directories) 63 | ? $this->baseClassNamespace 64 | : $this->baseClassNamespace . '\\' . collect() 65 | ->concat($this->directories) 66 | ->map([Str::class, 'studly']) 67 | ->implode('\\'); 68 | } 69 | 70 | public function className() 71 | { 72 | return $this->componentClass; 73 | } 74 | 75 | public function classContents() 76 | { 77 | if ($this->model) { 78 | $template = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'datatable-model.stub'); 79 | 80 | return preg_replace_array( 81 | ['/\[namespace\]/', '/\[use\]/', '/\[class\]/', '/\[model\]/'], 82 | [ 83 | $this->classNamespace(), 84 | config('livewire-datatables.model_namespace', 'App') . '\\' . Str::studly($this->model), 85 | $this->className(), 86 | Str::studly($this->model), 87 | ], 88 | $template 89 | ); 90 | } else { 91 | $template = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'datatable.stub'); 92 | 93 | return preg_replace_array( 94 | ['/\[namespace\]/', '/\[class\]/'], 95 | [$this->classNamespace(), $this->className()], 96 | $template 97 | ); 98 | } 99 | } 100 | 101 | public static function generatePathFromNamespace($namespace) 102 | { 103 | $name = Str::replaceFirst(app()->getNamespace(), '', $namespace); 104 | 105 | return app('path') . '/' . str_replace('\\', '/', $name); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Commands/DatatableMakeCommand.php: -------------------------------------------------------------------------------- 1 | parser = new ComponentParser( 17 | config('livewire.class_namespace', 'App\\Livewire') . '\\Datatables', 18 | $this->argument('name'), 19 | $this->option('model') 20 | ); 21 | 22 | if ($this->isReservedClassName($name = $this->parser->className())) { 23 | $this->line(" WHOOPS! 😳 \n"); 24 | $this->line("Class is reserved: $name"); 25 | 26 | return; 27 | } 28 | 29 | $class = $this->createClass(); 30 | 31 | // $this->refreshComponentAutodiscovery(); 32 | 33 | $this->line(" COMPONENT CREATED 🤙\n"); 34 | $class && $this->line("CLASS: {$this->parser->relativeClassPath()}"); 35 | } 36 | 37 | protected function createClass(): bool 38 | { 39 | $classPath = $this->parser->classPath(); 40 | 41 | if (File::exists($classPath)) { 42 | $this->line(" WHOOPS-IE-TOOTLES 😳 \n"); 43 | $this->line("Class already exists: {$this->parser->relativeClassPath()}"); 44 | 45 | return false; 46 | } 47 | 48 | $this->ensureDirectoryExists($classPath); 49 | 50 | File::put($classPath, $this->parser->classContents()); 51 | 52 | return $classPath; 53 | } 54 | 55 | // public function refreshComponentAutodiscovery() 56 | // { 57 | // app(LivewireComponentsFinder::class)->build(); 58 | // } 59 | 60 | public function isReservedClassName($name): bool 61 | { 62 | return in_array($name, ['Parent', 'Component', 'Interface']); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Commands/datatable-model.stub: -------------------------------------------------------------------------------- 1 | format(); 16 | } 17 | 18 | public function format($format = null): static 19 | { 20 | $this->callbackFunction = static function ($value) use ($format) { 21 | return $value ? Carbon::parse($value)->format($format ?? config('livewire-datatables.default_date_format')) : null; 22 | }; 23 | 24 | return $this; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/DatetimeColumn.php: -------------------------------------------------------------------------------- 1 | format(); 16 | } 17 | 18 | public function format($format = null): static 19 | { 20 | $this->callbackFunction = static function ($value) use ($format) { 21 | return $value ? Carbon::parse($value)->format($format ?? config('livewire-datatables.default_datetime_format')) : null; 22 | }; 23 | 24 | return $this; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Exports/DatatableExport.php: -------------------------------------------------------------------------------- 1 | collection = $collection; 26 | } 27 | 28 | public function collection() 29 | { 30 | return $this->collection; 31 | } 32 | 33 | public function headings(): array 34 | { 35 | return array_keys((array) $this->collection->first()); 36 | } 37 | 38 | public function setFileName($fileName) 39 | { 40 | $this->fileName = $fileName; 41 | 42 | return $this; 43 | } 44 | 45 | public function getFileName(): string 46 | { 47 | return $this->fileName; 48 | } 49 | 50 | public function setColumnWidths($columnWidths) 51 | { 52 | $this->columnWidths = $columnWidths; 53 | 54 | return $this; 55 | } 56 | 57 | public function getColumnWidths(): array 58 | { 59 | return $this->columnWidths; 60 | } 61 | 62 | public function columnWidths(): array 63 | { 64 | return $this->getColumnWidths(); 65 | } 66 | 67 | public function setStyles($styles) 68 | { 69 | $this->styles = $styles; 70 | 71 | return $this; 72 | } 73 | 74 | public function getStyles(): array 75 | { 76 | return $this->styles; 77 | } 78 | 79 | public function styles(Worksheet $sheet) 80 | { 81 | return $this->getStyles(); 82 | } 83 | 84 | public function download() 85 | { 86 | return Excel::download($this, $this->getFileName()); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Http/Controllers/FileExportController.php: -------------------------------------------------------------------------------- 1 | get('datatables/' . $filename), 200, [ 14 | 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 15 | 'Content-Disposition' => 'inline; filename="' . $filename . '"', 16 | 'X-Vapor-Base64-Encode' => 'True', 17 | ]); 18 | 19 | Storage::disk(config('livewire-datatables.file_export.disk') ?: config('filesystems.default')) 20 | ->delete('datatables/' . $filename); 21 | 22 | return $response; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/JsonColumn.php: -------------------------------------------------------------------------------- 1 | callbackFunction = static function ($value) { 14 | return $value ? join(', ', json_decode($value, false, 512, JSON_THROW_ON_ERROR)) : null; 15 | }; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/LabelColumn.php: -------------------------------------------------------------------------------- 1 | label('foo')->content('bar'), 9 | */ 10 | class LabelColumn extends Column 11 | { 12 | public string $type = 'label'; 13 | 14 | public string $labelContent = ''; 15 | 16 | /** 17 | * Which fixed string should always be displayed in every row of this column? 18 | */ 19 | public function content($content): static 20 | { 21 | $this->labelContent = $content; 22 | 23 | return $this; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Livewire/ComplexQuery.php: -------------------------------------------------------------------------------- 1 | 'group', 23 | 'logic' => 'and', 24 | 'content' => [], 25 | ], 26 | ]; 27 | 28 | public function mount($columns, $persistKey, $savedQueries = null): void 29 | { 30 | $this->columns = $columns; 31 | $this->persistKey = $persistKey; 32 | $this->savedQueries = $savedQueries; 33 | } 34 | 35 | #[On('updateSavedQueries')] 36 | public function updateSavedQueries($savedQueries = null): void 37 | { 38 | $this->mount($this->columns, $this->persistKey, $savedQueries ?? $this->savedQueries); 39 | } 40 | 41 | public function updatedRules($value, $key): void 42 | { 43 | $this->clearOperandAndValueWhenColumnChanged($key); 44 | 45 | $this->runQuery(); 46 | } 47 | 48 | public function clearOperandAndValueWhenColumnChanged($key): void 49 | { 50 | if (Str::endsWith($key, 'column')) { 51 | data_set($this->rules, str_replace('column', 'operand', $key), null); 52 | data_set($this->rules, str_replace('column', 'value', $key), null); 53 | } 54 | } 55 | 56 | public function getRulesStringProperty($rules = null, $logic = 'and'): string 57 | { 58 | return collect($rules ?? $this->rules)->map(function ($rule) { 59 | return $rule['type'] === 'rule' 60 | ? implode(' ', [$this->columns[$rule['content']['column']]['label'] ?? '', $rule['content']['operand'] ?? '', $rule['content']['value'] ?? '']) 61 | : '(' . $this->getRulesStringProperty($rule['content'], $rule['logic']) . ')'; 62 | })->join(strtoupper(" $logic ")); 63 | } 64 | 65 | public function runQuery(): void 66 | { 67 | $this->validateRules(); 68 | 69 | $this->dispatch('complexQuery', rules: count($this->rules[0]['content']) ? $this->rules : null); 70 | } 71 | 72 | public function saveQuery($name): void 73 | { 74 | $this->dispatch('saveQuery', name: $name, rules: $this->rules); 75 | } 76 | 77 | public function loadRules($rules): void 78 | { 79 | $this->rules = $rules; 80 | $this->runQuery(); 81 | } 82 | 83 | public function deleteRules($id): void 84 | { 85 | $this->dispatch('deleteQuery', id: $id); 86 | } 87 | 88 | #[On('reset-query')] 89 | public function resetQuery(): void 90 | { 91 | $this->reset('rules'); 92 | $this->dispatch('refreshLivewireDatatable'); 93 | $this->runQuery(); 94 | } 95 | 96 | public function validateRules($rules = null, $key = ''): void 97 | { 98 | $rules = $rules ?? $this->rules[0]['content']; 99 | 100 | collect($rules)->each(function ($rule, $i) { 101 | if ($rule['type'] === 'rule') { 102 | $v = Validator::make($rule['content'], ['column' => 'required']); 103 | 104 | $v->sometimes('operand', 'required', function ($rule) { 105 | return !($rule['value'] === 'true' || $rule['value'] === 'false'); 106 | }); 107 | 108 | $v->sometimes('value', 'required', function ($rule) { 109 | return !collect([ 110 | 'is empty', 111 | 'is not empty', 112 | ])->contains($rule['operand']); 113 | }); 114 | 115 | $v->validate(); 116 | } else { 117 | $this->validateRules($rule['content']); 118 | } 119 | }); 120 | } 121 | 122 | public function addRule($index): void 123 | { 124 | $temp = Arr::get($this->rules, $index); 125 | 126 | $temp[] = [ 127 | 'type' => 'rule', 128 | 'content' => [ 129 | 'column' => null, 130 | 'operand' => null, 131 | 'value' => null, 132 | ], 133 | ]; 134 | 135 | Arr::set($this->rules, $index, $temp); 136 | 137 | $this->validateRules(); 138 | } 139 | 140 | public function duplicateRule($index): void 141 | { 142 | $current = Arr::get($this->rules, Str::beforeLast($index, '.content')); 143 | $parentGroup = Arr::get($this->rules, Str::beforeLast(Str::beforeLast($index, '.content'), '.')); 144 | 145 | $parentGroup[] = $current; 146 | 147 | Arr::set($this->rules, Str::beforeLast(Str::beforeLast($index, '.content'), '.'), $parentGroup); 148 | 149 | $this->validateRules(); 150 | } 151 | 152 | public function moveRule($from, $to): void 153 | { 154 | $mover = Arr::get($this->rules, Str::beforeLast($from, '.')); 155 | $newParent = Arr::get($this->rules, $to); 156 | 157 | if (is_array($newParent) && is_array($mover)) { 158 | $newParent[] = $mover; 159 | Arr::set($this->rules, $to, $newParent); 160 | Arr::pull($this->rules, Str::beforeLast($from, '.')); 161 | } 162 | 163 | $this->runQuery(); 164 | } 165 | 166 | public function addGroup($index): void 167 | { 168 | $temp = Arr::get($this->rules, $index); 169 | 170 | $temp[] = [ 171 | 'type' => 'group', 172 | 'logic' => 'and', 173 | 'content' => [], 174 | ]; 175 | 176 | Arr::set($this->rules, $index, $temp); 177 | } 178 | 179 | public function removeRule($index): void 180 | { 181 | Arr::pull($this->rules, Str::beforeLast($index, '.')); 182 | 183 | $this->runQuery(); 184 | } 185 | 186 | public function setRuleColumn($index, $value): void 187 | { 188 | $this->rules[$index]['column'] = $this->columns[$value]; 189 | } 190 | 191 | public function setRuleOperand($index, $value): void 192 | { 193 | $this->rules[$index]['operand'] = $value; 194 | } 195 | 196 | public function setRuleValue($index, $value): void 197 | { 198 | $this->rules[$index]['value'] = $value; 199 | } 200 | 201 | public function getRuleColumn($key) 202 | { 203 | return $this->columns[Arr::get($this->rules, $key . '.column')] ?? null; 204 | } 205 | 206 | public function getOperands($key) 207 | { 208 | $operands = [ 209 | 'string' => ['equals', 'does not equal', 'contains', 'does not contain', 'is empty', 'is not empty', 'begins with', 'ends with'], 210 | 'json' => ['equals', 'does not equal', 'contains', 'does not contain', 'is empty', 'is not empty', 'begins with', 'ends with'], 211 | 'editable' => ['equals', 'does not equal', 'contains', 'does not contain', 'is empty', 'is not empty', 'begins with', 'ends with'], 212 | 'number' => ['=', '<>', '<', '<=', '>', '>='], 213 | 'date' => ['=', '<>', '<', '<=', '>', '>='], 214 | 'time' => ['=', '<>', '<', '<=', '>', '>='], 215 | 'boolean' => [], 216 | 'scope' => ['includes'], 217 | ]; 218 | 219 | if (!$this->getRuleColumn($key)) { 220 | return []; 221 | } 222 | 223 | return optional($this->getRuleColumn($key))['scopeFilter'] 224 | ? $operands['scope'] 225 | : $operands[$this->getRuleColumn($key)['type']]; 226 | } 227 | 228 | public function render(): View 229 | { 230 | return view('datatables::complex-query'); 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /src/LivewireDatatablesServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__ . '/../resources/views/livewire/datatables', 'datatables'); 28 | $this->loadViewsFrom(__DIR__ . '/../resources/views/icons', 'icons'); 29 | 30 | Blade::component('icons::arrow-left', 'icons.arrow-left'); 31 | Blade::component('icons::arrow-right', 'icons.arrow-right'); 32 | Blade::component('icons::arrow-circle-left', 'icons.arrow-circle-left'); 33 | Blade::component('icons::chevron-up', 'icons.chevron-up'); 34 | Blade::component('icons::chevron-down', 'icons.chevron-down'); 35 | Blade::component('icons::cog', 'icons.cog'); 36 | Blade::component('icons::trash', 'icons.trash'); 37 | Blade::component('icons::copy', 'icons.copy'); 38 | Blade::component('icons::excel', 'icons.excel'); 39 | Blade::component('icons::x-circle', 'icons.x-circle'); 40 | Blade::component('icons::check-circle', 'icons.check-circle'); 41 | 42 | if ($this->app->runningInConsole()) { 43 | $this->publishes([ 44 | __DIR__ . '/../config/livewire-datatables.php' => config_path('livewire-datatables.php'), 45 | ], 'config'); 46 | 47 | $this->publishes([ 48 | __DIR__ . '/../resources/views/livewire/datatables' => resource_path('views/livewire/datatables'), 49 | __DIR__ . '/../resources/views/icons' => resource_path('views/livewire/datatables/icons'), 50 | ], 'views'); 51 | 52 | $this->commands([MakeDatatableCommand::class, DatatableMakeCommand::class]); 53 | } 54 | 55 | Route::get('/datatables/{filename}', [FileExportController::class, 'handle']) 56 | ->middleware('web'); 57 | 58 | $this->loadBuilderMacros(); 59 | $this->loadEloquentBuilderMacros(); 60 | $this->loadRelationMacros(); 61 | } 62 | 63 | public function loadBuilderMacros(): void 64 | { 65 | Builder::macro('leftJoinIfNotJoined', function (...$params) { 66 | $isJoined = collect($this->joins)->pluck('table')->contains($params[0]); 67 | 68 | return $isJoined ? $this : call_user_func_array([$this, 'leftJoin'], $params); 69 | }); 70 | 71 | Builder::macro('groupIfNotGrouped', function (...$params) { 72 | $isGrouped = collect($this->groups)->contains($params[0]); 73 | 74 | return $isGrouped ? $this : call_user_func_array([$this, 'groupBy'], $params); 75 | }); 76 | } 77 | 78 | public function loadEloquentBuilderMacros(): void 79 | { 80 | EloquentBuilder::macro('customWithAggregate', function ($relations, $aggregate, $column, $alias = null) { 81 | if (empty($relations)) { 82 | return $this; 83 | } 84 | 85 | $relations = is_array($relations) ? $relations : [$relations]; 86 | 87 | foreach ($this->parseWithRelations($relations) as $name => $constraints) { 88 | $segments = explode(' ', $name); 89 | 90 | if (count($segments) == 3 && Str::lower($segments[1]) == 'as') { 91 | [$name, $alias] = [$segments[0], $segments[2]]; 92 | } 93 | 94 | $relation = $this->getRelationWithoutConstraints($name); 95 | 96 | $table = $relation->getRelated()->newQuery()->getQuery()->from === $this->getQuery()->from 97 | ? $relation->getRelationCountHashWithoutIncrementing() 98 | : ($this->query->getConnection()->getTablePrefix() ?? '') . $relation->getRelated()->getTable(); 99 | 100 | $query = $relation->getRelationExistenceAggregatesQuery( 101 | $relation->getRelated()->newQuery(), 102 | $this, 103 | $aggregate, 104 | $table . '.' . ($column ?? 'id') 105 | ); 106 | 107 | $query->callScope($constraints); 108 | 109 | $query = $query->mergeConstraintsFrom($relation->getQuery())->toBase(); 110 | 111 | if (count($query->columns) > 1) { 112 | $query->columns = [$query->columns[0]]; 113 | } 114 | $columnAlias = new Expression('`' . ($alias ?? collect([$relations, $column])->filter()->flatten()->join('.')) . '`'); 115 | $this->selectSub($query, $columnAlias); 116 | } 117 | 118 | return $this; 119 | }); 120 | 121 | EloquentBuilder::macro('hasAggregate', function ($relation, $column, $aggregate, $operator = '>=', $count = 1) { 122 | if (is_string($relation)) { 123 | $relation = $this->getRelationWithoutConstraints($relation); 124 | } 125 | 126 | $table = $relation->getRelated()->newQuery()->getQuery()->from === $this->getQuery()->from 127 | ? $relation->getRelationCountHashWithoutIncrementing() 128 | : ($this->query->getConnection()->getTablePrefix() ?? '') . $relation->getRelated()->getTable(); 129 | 130 | $hasQuery = $relation->getRelationExistenceAggregatesQuery( 131 | $relation->getRelated()->newQueryWithoutRelationships(), 132 | $this, 133 | $aggregate, 134 | $table . '.' . $column 135 | ); 136 | 137 | $hasQuery->mergeConstraintsFrom($relation->getQuery()); 138 | 139 | return $this->addWhereCountQuery($hasQuery->toBase(), $operator, $count, 'and'); 140 | }); 141 | } 142 | 143 | public function loadRelationMacros(): void 144 | { 145 | Relation::macro('getRelationExistenceAggregatesQuery', function (EloquentBuilder $query, EloquentBuilder $parentQuery, $aggregate, $column) { 146 | $distinct_aggregate = new Expression($aggregate . "(distinct {$column} separator ', ')"); 147 | 148 | if ($query->getConnection()->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite') { 149 | $distinct_aggregate = new Expression($aggregate . "(REPLACE(DISTINCT({$column}), '', '') , ', ')"); 150 | } 151 | 152 | $expression = $aggregate === 'group_concat' 153 | 154 | ? $distinct_aggregate 155 | : new Expression('COALESCE(' . $aggregate . "({$column}),0)"); 156 | 157 | return $this->getRelationExistenceQuery( 158 | $query, 159 | $parentQuery, 160 | $expression 161 | )->setBindings([], 'select'); 162 | }); 163 | 164 | Relation::macro('getRelationCountHashWithoutIncrementing', function () { 165 | return 'laravel_reserved_' . static::$selfJoinCount; 166 | }); 167 | } 168 | 169 | public function register(): void 170 | { 171 | $this->mergeConfigFrom(__DIR__ . '/../config/livewire-datatables.php', 'livewire-datatables'); 172 | } 173 | 174 | protected function loadViewsFrom($path, $namespace): void 175 | { 176 | $this->callAfterResolving('view', function ($view) use ($path, $namespace) { 177 | if ( 178 | isset($this->app->config['view']['paths']) && 179 | is_array($this->app->config['view']['paths']) 180 | ) { 181 | foreach ($this->app->config['view']['paths'] as $viewPath) { 182 | if (is_dir($appPath = $viewPath . '/livewire/' . $namespace)) { 183 | $view->addNamespace($namespace, $appPath); 184 | } 185 | } 186 | } 187 | 188 | $view->addNamespace($namespace, $path); 189 | }); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/NumberColumn.php: -------------------------------------------------------------------------------- 1 | roundPrecision = $precision; 15 | 16 | $this->callbackFunction = function ($value) { 17 | return round($value, $this->roundPrecision); 18 | }; 19 | 20 | return $this; 21 | } 22 | 23 | public function format(int $decimals = 0, ?string $decimalSeparator = '.', ?string $thousandsSeparator = ','): static 24 | { 25 | $this->callbackFunction = static fn($value) => number_format($value, $decimals, $decimalSeparator, $thousandsSeparator); 26 | 27 | return $this; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/TimeColumn.php: -------------------------------------------------------------------------------- 1 | format(); 16 | } 17 | 18 | public function format($format = null): static 19 | { 20 | $this->callbackFunction = static function ($value) use ($format) { 21 | return $value ? Carbon::parse($value)->format($format ?? config('livewire-datatables.default_time_format')) : null; 22 | }; 23 | 24 | return $this; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Traits/CanExport.php: -------------------------------------------------------------------------------- 1 | label(__('Pin selected Records')) 30 | ->callback(function ($mode, $items) { 31 | $this->pinnedRecords = array_merge($this->pinnedRecords, $items); 32 | $this->selected = $this->pinnedRecords; 33 | 34 | session()->put($this->sessionKey(), $this->pinnedRecords); 35 | }), 36 | 37 | Action::value('unpin') 38 | ->label(__('Unpin selected Records')) 39 | ->callback(function ($mode, $items) { 40 | $this->pinnedRecords = array_diff($this->pinnedRecords, $items); 41 | $this->selected = $this->pinnedRecords; 42 | 43 | session()->put($this->sessionKey(), $this->pinnedRecords); 44 | }), 45 | ]); 46 | } 47 | 48 | public function resetTable() 49 | { 50 | parent::resetTable(); 51 | $this->pinnedRecords = []; 52 | } 53 | 54 | protected function initialisePinnedRecords() 55 | { 56 | if (session()->has($this->sessionKey())) { 57 | $this->pinnedRecords = session()->get($this->sessionKey()); 58 | } 59 | 60 | $this->selected = $this->pinnedRecords; 61 | } 62 | 63 | /** 64 | * This function should be called after every filter method to ensure pinned records appear 65 | * in every possible filter combination. 66 | * Ensures to have at least _one other_ where applied to the current query build 67 | * to apply this orWhere() on top of that. 68 | */ 69 | protected function applyPinnedRecords(): self 70 | { 71 | if (isset($this->pinnedRecords) && $this->pinnedRecords && $this->query->getQuery()->wheres) { 72 | $this->query->orWhereIn('id', $this->pinnedRecords); 73 | } 74 | 75 | return $this; 76 | } 77 | 78 | private function sessionKey(): string 79 | { 80 | return $this->sessionStorageKey() . $this->sessionKeyPostfix; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Traits/WithCallbacks.php: -------------------------------------------------------------------------------- 1 | where(Str::after($key, '.'), $rowId) 14 | ->update([$column => $value]); 15 | 16 | $this->dispatch('fieldEdited', rowId: $rowId, column: $column); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Traits/WithPresetDateFilters.php: -------------------------------------------------------------------------------- 1 | dates['start'] = now()->subMonth()->startOfMonth()->format('Y-m-d'); 10 | $this->dates['end'] = now()->subMonth()->endOfMonth()->format('Y-m-d'); 11 | } 12 | 13 | public function lastQuarter(): void 14 | { 15 | $this->dates['start'] = now()->subQuarter()->startOfQuarter()->format('Y-m-d'); 16 | $this->dates['end'] = now()->subQuarter()->endOfQuarter()->format('Y-m-d'); 17 | } 18 | 19 | public function lastYear(): void 20 | { 21 | $this->dates['start'] = now()->subYear()->startOfYear()->format('Y-m-d'); 22 | $this->dates['end'] = now()->subYear()->endOfYear()->format('Y-m-d'); 23 | } 24 | 25 | public function monthToToday(): void 26 | { 27 | $this->dates['start'] = now()->subMonth()->addDay()->format('Y-m-d'); 28 | $this->dates['end'] = now()->format('Y-m-d'); 29 | } 30 | 31 | public function quarterToToday(): void 32 | { 33 | $this->dates['start'] = now()->subQuarter()->addDay()->format('Y-m-d'); 34 | $this->dates['end'] = now()->format('Y-m-d'); 35 | } 36 | 37 | public function yearToToday(): void 38 | { 39 | $this->dates['start'] = now()->subYear()->addDay()->format('Y-m-d'); 40 | $this->dates['end'] = now()->format('Y-m-d'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Traits/WithPresetTimeFilters.php: -------------------------------------------------------------------------------- 1 | times['start'] = '09:00:00'; 10 | $this->times['end'] = '17:00:00'; 11 | } 12 | 13 | public function sevenToSevenDay(): void 14 | { 15 | $this->times['start'] = '07:00:00'; 16 | $this->times['end'] = '19:00:00'; 17 | } 18 | 19 | public function sevenToSevenNight(): void 20 | { 21 | $this->times['start'] = '19:00:00'; 22 | $this->times['end'] = '07:00:00'; 23 | } 24 | 25 | public function graveyardShift(): void 26 | { 27 | $this->times['start'] = '22:00:00'; 28 | $this->times['end'] = '06:00:00'; 29 | } 30 | } 31 | --------------------------------------------------------------------------------