raouia12's picture
ajoute a cette page a ce code excatement suadn on slectionne un field avoir option davoir la liste du ce field avec nombre et si ya des doublons
2bd4dcb verified
Raw History Blame Contribute Delete
9.07 kB
// Sample data for demonstration
const filtersData = {
"Post1": {
"filter1": ["Field1", "Field2"],
"filter2": ["Field3"],
"filter3": []
},
"Post2": {
"filter1": ["Field4"],
"filter2": [],
"filter3": ["Field5", "Field6"]
}
};
const allFields = ["Poste", "Field1", "Field2", "Field3", "Field4", "Field5", "Field6", "Field7", "Field8"];
const sampleData = {
"Post1": {
"Field1": ["Value1", "Value2", "Value1", "Value3"],
"Field2": ["A", "B", "C", "A"],
"Field3": ["X", "Y", "Z"]
},
"Post2": {
"Field4": ["One", "Two", "Three"],
"Field5": ["1", "2", "3", "2"],
"Field6": ["Alpha", "Beta", "Alpha"]
}
};
const selectedPosts = ["Post1", "Post2"];
// Initialize the page
$(document).ready(function() {
renderPosts();
renderFilters();
});
function renderPosts() {
const postsContainer = $('#postsContainer');
postsContainer.empty();
allFields.filter(field => field === 'Poste').forEach(poste => {
const isChecked = selectedPosts.includes(poste);
postsContainer.append(`
<label class="flex items-center gap-2">
<input type="checkbox" name="postes" id="${poste}" value="${poste}" ${isChecked ? 'checked' : ''} onchange="updateSelectedPosts()">
${poste}
</label>
`);
});
}
function renderFilters() {
const filtersContainer = $('#filtersContainer');
filtersContainer.empty();
selectedPosts.forEach((post, idx) => {
const filters = filtersData[post] || {filter1: [], filter2: [], filter3: []};
filtersContainer.append(`
<div class="mt-8">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Filters for ${post}</h3>
<div class="flex space-x-3 mb-4">
<button type="button" class="inline-flex items-center px-3 py-2 bg-offline-red text-white rounded-lg hover:bg-red-600 transition-colors" onclick="deselectAllFilters(${idx})">
<i data-feather="trash-2" class="mr-2 w-4 h-4"></i>
Deselect All
</button>
<button type="button" class="inline-flex items-center px-3 py-2 bg-atos-green text-white rounded-lg hover:bg-green-700 transition-colors" onclick="addFilter(${idx})">
<i data-feather="plus" class="mr-2 w-4 h-4"></i>
Add Filter
</button>
<button type="submit" name="submit" value="${post}" class="inline-flex items-center px-3 py-2 bg-atos-blue text-white rounded-lg hover:bg-dark-blue transition-colors">
<i data-feather="check" class="mr-2 w-4 h-4"></i>
Save Filters
</button>
</div>
<div class="filtersContainer" data-index="${idx}">
<div class="grid grid-cols-1 gap-6 md:grid-cols-3">
${[1, 2, 3].map(c => {
const filterName = `${post}_filter${c}_fields`;
const currentFields = filters[`filter${c}`] || [];
const otherFields = c === 1 ?
(filters.filter2 || []).concat(filters.filter3 || []) :
(c === 2 ?
(filters.filter1 || []).concat(filters.filter3 || []) :
(filters.filter1 || []).concat(filters.filter2 || []));
const isAdded = currentFields.length > 0;
return `
<div class="filter-card bg-white p-4 rounded-lg border border-gray-200 ${isAdded ? '' : 'cacher'} filter${c}">
<h2 class="text-base font-medium text-gray-700 mb-3">Filter ${c}</h2>
<div class="filter-column space-y-2">
${allFields.filter(field => field !== 'Poste').map(field => {
const checked = currentFields.includes(field);
const disabled = otherFields.includes(field);
return `
<div class="checkbox-container">
<label>
<input type="checkbox" name="${filterName}" id="${field}_${idx}_${c}"
value="${field}" ${checked ? 'checked' : ''} ${disabled ? 'disabled' : ''}
onchange="showFieldStats('${post}', '${field}')">
<span>${field}</span>
</label>
<div id="stats_${post}_${field}" class="field-stats hidden text-xs mt-1 pl-6"></div>
</div>
`;
}).join('')}
</div>
</div>
`;
}).join('')}
</div>
</div>
</div>
`);
});
feather.replace();
}
function deselectAllPosts() {
$('input[name="postes"]:checked').prop('checked', false);
updateSelectedPosts();
}
function updateSelectedPosts() {
const selected = $('input[name="postes"]:checked').map(function() {
return $(this).val();
}).get();
$('#selectedPostsHidden').val(JSON.stringify(selected));
// In a real app, you would update the filters section here
}
function deselectAllFilters(postIndex) {
$(`.filtersContainer[data-index="${postIndex}"] input[type="checkbox"]:checked`).prop('checked', false);
}
function addFilter(postIndex) {
const emptyFilter = $(`.filtersContainer[data-index="${postIndex}"] .cacher`).first();
if (emptyFilter.length) {
emptyFilter.removeClass('cacher');
}
}
function showFieldStats(post, field) {
const statsElement = $(`#stats_${post}_${field}`);
const isChecked = $(`input[id^="${field}_"]:checked`).length > 0;
if (!isChecked) {
statsElement.addClass('hidden');
return;
}
const values = sampleData[post]?.[field] || [];
const totalCount = values.length;
// Count occurrences of each value
const valueCounts = {};
values.forEach(value => {
valueCounts[value] = (valueCounts[value] || 0) + 1;
});
// Sort all values by count (descending)
const sortedValues = Object.entries(valueCounts)
.sort((a, b) => b[1] - a[1]);
let statsHTML = `
<div class="text-gray-600">
<div class="font-semibold mb-1">${field} - ${post}</div>
<div class="mb-2">Total entries: ${totalCount}</div>
<div class="max-h-60 overflow-y-auto border rounded p-2 bg-white">
<table class="w-full text-sm">
<thead>
<tr class="border-b">
<th class="text-left py-1">Value</th>
<th class="text-right py-1">Count</th>
<th class="text-right py-1">%</th>
</tr>
</thead>
<tbody>
${sortedValues.map(([value, count]) => {
const percentage = ((count / totalCount) * 100).toFixed(1);
const isDuplicate = count > 1;
return `
<tr class="${isDuplicate ? 'bg-red-50' : ''}">
<td class="py-1">${value}</td>
<td class="text-right py-1">${count}</td>
<td class="text-right py-1">${percentage}%</td>
</tr>
`;
}).join('')}
</tbody>
</table>
</div>
${sortedValues.some(([_, count]) => count > 1) ?
`<div class="mt-2 text-red-600 font-medium">
<i data-feather="alert-triangle" class="inline mr-1 w-4 h-4"></i>
${sortedValues.filter(([_, count]) => count > 1).length} duplicate values found
</div>` :
`<div class="mt-2 text-green-600 font-medium">
<i data-feather="check-circle" class="inline mr-1 w-4 h-4"></i>
No duplicates found
</div>`
}
</div>
`;
statsElement.html(statsHTML).removeClass('hidden');
feather.replace();
}