# DataTable Migration Guide ## Summary of Changes The DataTable component has been updated to follow **shadcn-vue** architecture patterns using **TanStack Table v8**. This provides better flexibility, more features, and follows industry-standard patterns. ## What's New ### ✅ Components Created/Updated 1. **`DataTableNew2.vue`** - New main component with shadcn-vue architecture 2. **`DataTableColumnHeader.vue`** - Already good, uses lucide-vue-next icons 3. **`DataTablePagination.vue`** - Already follows shadcn-vue patterns 4. **`DataTableViewOptions.vue`** - Already follows shadcn-vue patterns 5. **`DataTableToolbar.vue`** - Already exists with advanced features 6. **`columns-example.js`** - Column definition examples 7. **`README.md`** - Comprehensive documentation 8. **`DataTableExample.vue`** - Working example page ### ✅ Utilities Added - **`valueUpdater()`** in `lib/utils.js` - Helper for TanStack Table state management ## Key Improvements ### 1. **FlexRender Integration** Now properly uses TanStack Table's FlexRender for column headers and cells: ```vue ``` ### 2. **Better Column Definitions** Supports both simple and advanced formats: **Simple:** ```javascript { key: 'name', label: 'Name', sortable: true } ``` **Advanced:** ```javascript { accessorKey: 'name', header: ({ column }) => h(DataTableColumnHeader, { column, title: 'Name' }), cell: ({ row }) => h('div', {}, row.getValue('name')), } ``` ### 3. **Enhanced Features** - ✅ Row selection with checkboxes - ✅ Column visibility toggle - ✅ Advanced filtering - ✅ Better loading/empty states - ✅ Custom cell slots - ✅ Flexible toolbar ### 4. **Better State Management** Uses `valueUpdater()` helper for proper Vue reactivity with TanStack Table: ```javascript onSortingChange: (updater) => valueUpdater(updater, sorting) ``` ## Migration Steps ### Step 1: Update Imports **Before:** ```vue import DataTable from '@/Components/DataTable/DataTable.vue'; ``` **After:** ```vue import DataTable from '@/Components/DataTable/DataTableNew2.vue'; ``` ### Step 2: Update Props **Before:** ```vue ``` **After:** ```vue ``` Main prop changes: - `rows` → `data` - Add `route-name` for server-side pagination ### Step 3: Column Definitions Your existing simple column format still works: ```javascript const columns = [ { key: 'id', label: 'ID', sortable: true }, { key: 'name', label: 'Name', sortable: true }, ]; ``` But you can now use advanced format for more control: ```javascript import { h } from 'vue'; import DataTableColumnHeader from '@/Components/DataTable/DataTableColumnHeader.vue'; const columns = [ { accessorKey: 'name', header: ({ column }) => h(DataTableColumnHeader, { column, title: 'Name' }), cell: ({ row }) => h('div', { class: 'font-medium' }, row.getValue('name')), }, ]; ``` ### Step 4: Custom Cell Rendering **Before:** Required editing component **After:** Use slots! ```vue ``` ## Backward Compatibility The **old DataTable components are still available**: - `DataTable.vue` - Your current enhanced version - `DataTableServer.vue` - Your server-side version - `DataTableOld.vue` - Original version You can migrate pages gradually. Both old and new can coexist. ## Example Migration ### Before (Client/Index.vue) ```vue ``` ### After (Using DataTableNew2) ```vue ``` ## Testing Your Migration 1. **Check the example page:** ``` Visit: /examples/datatable ``` (You'll need to add a route for this) 2. **Test features:** - ✅ Sorting (click column headers) - ✅ Filtering (use search input) - ✅ Pagination (navigate pages) - ✅ Row selection (if enabled) - ✅ Column visibility (View button) 3. **Check browser console:** - No errors - Events firing correctly ## Common Issues ### Issue: "FlexRender is not defined" **Solution:** Make sure you imported it: ```javascript import { FlexRender } from '@tanstack/vue-table'; ``` ### Issue: Column not sorting **Solution:** Make sure `sortable: true` is set: ```javascript { key: 'name', label: 'Name', sortable: true } ``` ### Issue: Server-side not working **Solution:** Provide both `meta` and `route-name`: ```vue ``` ### Issue: Custom cells not rendering **Solution:** Use the correct slot name format: ```vue ``` ## Need Help? 1. Check `README.md` for detailed documentation 2. Look at `columns-example.js` for column patterns 3. Review `DataTableExample.vue` for working examples 4. Check TanStack Table docs: https://tanstack.com/table/v8 ## Rollback Plan If you encounter issues, you can always use the old components: ```vue import DataTable from '@/Components/DataTable/DataTable.vue'; // or import DataTableServer from '@/Components/DataTable/DataTableServer.vue'; ``` Nothing breaks your existing code!