fix: multi-filtro corrigido
This commit is contained in:
parent
833715876b
commit
2aa410a96f
|
|
@ -624,31 +624,51 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
|
|||
|
||||
// Filtrar dados baseado nos filtros de coluna
|
||||
const filteredData = React.useMemo(() => {
|
||||
if (!data || data.length === 0) return data;
|
||||
console.log('🔍 Debug filteredData - data:', data?.length, 'columnFilters:', columnFilters);
|
||||
|
||||
return data.filter((row) => {
|
||||
if (!data || data.length === 0) {
|
||||
console.log('⚠️ No data available');
|
||||
return data;
|
||||
}
|
||||
|
||||
const filtered = data.filter((row) => {
|
||||
return Object.entries(columnFilters).every(([field, filterValues]) => {
|
||||
if (!filterValues || filterValues.length === 0) return true;
|
||||
|
||||
const cellValue = (row as any)[field];
|
||||
const stringValue = cellValue === null || cellValue === undefined ? "" : String(cellValue);
|
||||
|
||||
return filterValues.includes(stringValue);
|
||||
const matches = filterValues.includes(stringValue);
|
||||
if (!matches) {
|
||||
console.log(`❌ Filter mismatch - Field: ${field}, CellValue: ${cellValue}, StringValue: ${stringValue}, FilterValues:`, filterValues);
|
||||
}
|
||||
return matches;
|
||||
});
|
||||
}).map((row, index) => ({
|
||||
...row,
|
||||
id: `filtered-${row.id || row.recnum || index}` // Garantir ID único e estável
|
||||
}));
|
||||
|
||||
console.log('✅ Filtered result:', filtered.length, 'out of', data.length);
|
||||
return filtered;
|
||||
}, [data, columnFilters]);
|
||||
|
||||
// Ordenar dados baseado na ordenação de coluna
|
||||
const sortedAndFilteredData = React.useMemo(() => {
|
||||
if (!filteredData || filteredData.length === 0) return filteredData;
|
||||
console.log('🔍 Debug sortedAndFilteredData - filteredData:', filteredData?.length, 'columnSorts:', columnSorts);
|
||||
|
||||
if (!filteredData || filteredData.length === 0) {
|
||||
console.log('⚠️ No filtered data available');
|
||||
return filteredData;
|
||||
}
|
||||
|
||||
const sortField = Object.keys(columnSorts).find(field => columnSorts[field] !== null);
|
||||
if (!sortField || !columnSorts[sortField]) return filteredData;
|
||||
if (!sortField || !columnSorts[sortField]) {
|
||||
console.log('✅ No sorting applied, returning filtered data as is');
|
||||
return filteredData;
|
||||
}
|
||||
|
||||
return [...filteredData].sort((a, b) => {
|
||||
const sorted = [...filteredData].sort((a, b) => {
|
||||
const aValue = (a as any)[sortField];
|
||||
const bValue = (b as any)[sortField];
|
||||
|
||||
|
|
@ -662,8 +682,29 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
|
|||
return bString.localeCompare(aString);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('✅ Sorted result:', sorted.length);
|
||||
return sorted;
|
||||
}, [filteredData, columnSorts]);
|
||||
|
||||
// Debug useEffect para investigar estado inicial
|
||||
React.useEffect(() => {
|
||||
console.log('🔍 Debug Estado Inicial:');
|
||||
console.log('📊 data:', data?.length);
|
||||
console.log('🔍 columnFilters:', columnFilters);
|
||||
console.log('📈 columnSorts:', columnSorts);
|
||||
console.log('🌐 globalFilter:', globalFilter);
|
||||
console.log('🔗 filtrosExternos:', filtrosExternos);
|
||||
}, [data, columnFilters, columnSorts, globalFilter, filtrosExternos]);
|
||||
|
||||
// Debug useEffect para DataGrid
|
||||
React.useEffect(() => {
|
||||
console.log('🔍 Debug DataGrid - sortedAndFilteredData:', sortedAndFilteredData?.length, 'loading:', loading);
|
||||
if (sortedAndFilteredData && sortedAndFilteredData.length > 0) {
|
||||
console.log('📊 First row sample:', sortedAndFilteredData[0]);
|
||||
}
|
||||
}, [sortedAndFilteredData, loading]);
|
||||
|
||||
// Calcular valor total dos dados filtrados
|
||||
const valorTotal = React.useMemo(() => {
|
||||
return sortedAndFilteredData.reduce((sum, item) => sum + (Number(item.valor) || 0), 0);
|
||||
|
|
@ -804,7 +845,27 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
|
|||
</Button>
|
||||
)}
|
||||
{data.length > 0 && (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
onClick={() => {
|
||||
console.log('🧪 Teste - Forçando dados para aparecer');
|
||||
console.log('📊 data atual:', data.length);
|
||||
console.log('🔍 columnFilters atual:', columnFilters);
|
||||
console.log('📈 sortedAndFilteredData atual:', sortedAndFilteredData.length);
|
||||
|
||||
// Limpar todos os filtros
|
||||
setColumnFilters({});
|
||||
setColumnSorts({});
|
||||
setGlobalFilter("");
|
||||
|
||||
console.log('✅ Filtros limpos');
|
||||
}}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="bg-yellow-100 border-yellow-300 text-yellow-800 hover:bg-yellow-200"
|
||||
>
|
||||
🧪 Teste Debug
|
||||
</Button>
|
||||
<Button
|
||||
onClick={clearAllFilters}
|
||||
variant="outline"
|
||||
|
|
@ -818,7 +879,7 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
|
|||
{getFilterCount()}
|
||||
</span>
|
||||
)}
|
||||
</Button>
|
||||
</Button>
|
||||
<Button
|
||||
onClick={exportToExcel}
|
||||
variant="outline"
|
||||
|
|
@ -875,7 +936,11 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
|
|||
},
|
||||
},
|
||||
}}
|
||||
getRowId={(row: any) => row.id || `row-${row.recnum || Math.random()}`}
|
||||
getRowId={(row: any) => {
|
||||
const id = row.id || row.recnum || `row-${Math.random()}`;
|
||||
console.log('🔍 getRowId called for row:', { id, recnum: row.recnum, nome_fornecedor: row.nome_fornecedor });
|
||||
return id;
|
||||
}}
|
||||
sx={{
|
||||
"& .MuiDataGrid-columnHeaders": {
|
||||
position: "sticky",
|
||||
|
|
|
|||
Loading…
Reference in New Issue