filtrage coté serveur
This commit is contained in:
parent
d550a98418
commit
5fac3f9b37
@ -505,28 +505,8 @@ autopostRouter.get('/', async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filtrage combiné avec recherche
|
|
||||||
var currentFilter = null; // null => pas de filtre actif
|
|
||||||
|
|
||||||
function applyFilterAndSearch() {
|
|
||||||
var q = ($("#searchInput").val() || "").toLowerCase();
|
|
||||||
$("table tbody tr").each(function() {
|
|
||||||
var $tr = $(this);
|
|
||||||
var name = $tr.find("td:first").text().toLowerCase();
|
|
||||||
var status = parseInt($tr.data("status"), 10);
|
|
||||||
|
|
||||||
var matchFilter = (currentFilter === null) || (status === currentFilter);
|
|
||||||
var matchSearch = !q || name.indexOf(q) !== -1;
|
|
||||||
|
|
||||||
$tr.toggle(matchFilter && matchSearch);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
|
|
||||||
// Recherche AJAX
|
|
||||||
let searchTimer = null;
|
let searchTimer = null;
|
||||||
|
|
||||||
$("#searchInput").on("keyup", function() {
|
$("#searchInput").on("keyup", function() {
|
||||||
clearTimeout(searchTimer); // annule le timer précédent
|
clearTimeout(searchTimer); // annule le timer précédent
|
||||||
|
|
||||||
@ -540,10 +520,8 @@ autopostRouter.get('/', async (req, res) => {
|
|||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
updateTable(data);
|
updateTable(data);
|
||||||
if (typeof currentFilter === 'undefined') {
|
$(".filter-card").removeClass("ring-4 ring-white/40");
|
||||||
currentFilter = null;
|
toggleShowAllButton();
|
||||||
}
|
|
||||||
applyFilterAndSearch();
|
|
||||||
},
|
},
|
||||||
error: function(jqXHR, textStatus, errorThrown) {
|
error: function(jqXHR, textStatus, errorThrown) {
|
||||||
// On ignore l'erreur si la requête a été annulée volontairement
|
// On ignore l'erreur si la requête a été annulée volontairement
|
||||||
@ -558,29 +536,56 @@ autopostRouter.get('/', async (req, res) => {
|
|||||||
|
|
||||||
|
|
||||||
function toggleShowAllButton() {
|
function toggleShowAllButton() {
|
||||||
if (currentFilter === null) {
|
if ($(".filter-card.ring-4").length === 0) {
|
||||||
$('#showAll').addClass('hidden');
|
$('#showAll').addClass('hidden');
|
||||||
} else {
|
} else {
|
||||||
$('#showAll').removeClass('hidden');
|
$('#showAll').removeClass('hidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Filtrage par clic sur une card
|
// Filtrage par clic sur une card
|
||||||
$(document).on("click", ".filter-card", function () {
|
$(document).on("click", ".filter-card", function () {
|
||||||
currentFilter = parseInt($(this).data("status"), 10);
|
const status = parseInt($(this).data("status"), 10);
|
||||||
|
|
||||||
$(".filter-card").removeClass("ring-4 ring-white/40");
|
$(".filter-card").removeClass("ring-4 ring-white/40");
|
||||||
$(this).addClass("ring-4 ring-white/40");
|
$(this).addClass("ring-4 ring-white/40");
|
||||||
applyFilterAndSearch();
|
|
||||||
|
$.ajax({
|
||||||
|
url: '/autopost/filter',
|
||||||
|
type: 'GET',
|
||||||
|
data: { status },
|
||||||
|
dataType: 'json',
|
||||||
|
success: function (data) {
|
||||||
|
updateTable(data);
|
||||||
toggleShowAllButton();
|
toggleShowAllButton();
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
alert("Erreur lors du filtrage.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Bouton tout afficher
|
// Bouton tout afficher
|
||||||
$(document).on("click", "#showAll", function () {
|
$(document).on("click", "#showAll", function () {
|
||||||
currentFilter = null;
|
|
||||||
$(".filter-card").removeClass("ring-4 ring-white/40");
|
$(".filter-card").removeClass("ring-4 ring-white/40");
|
||||||
applyFilterAndSearch();
|
|
||||||
|
$.ajax({
|
||||||
|
url: '/autopost/search',
|
||||||
|
type: 'GET',
|
||||||
|
data: { q: $("#searchInput").val() || "" },
|
||||||
|
dataType: 'json',
|
||||||
|
success: function (data) {
|
||||||
|
updateTable(data);
|
||||||
toggleShowAllButton();
|
toggleShowAllButton();
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
alert("Erreur lors du chargement des données.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Edition
|
// Edition
|
||||||
$(document).on("click", ".edit-link", function(e) {
|
$(document).on("click", ".edit-link", function(e) {
|
||||||
@ -985,6 +990,24 @@ autopostRouter.post('/delete/:id', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --------------------------- Fultrage -----------------------------
|
||||||
|
autopostRouter.get('/filter', async (req, res) => {
|
||||||
|
const status = req.query.status;
|
||||||
|
if (status === undefined) {
|
||||||
|
return res.status(400).json({ error: "Status non fourni." });
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const [rows] = await db.query(
|
||||||
|
`SELECT nom, status, id FROM \`${config.DB_TABLE}\` WHERE status = ? ORDER BY id DESC LIMIT 500`,
|
||||||
|
[status]
|
||||||
|
);
|
||||||
|
res.json(rows);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err.message);
|
||||||
|
res.status(500).json({ error: "Erreur lors de la requête." });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Redirection accès direct GET /edit/:id
|
// Redirection accès direct GET /edit/:id
|
||||||
autopostRouter.get('/edit/:id', (req, res) => {
|
autopostRouter.get('/edit/:id', (req, res) => {
|
||||||
res.redirect("/autopost/");
|
res.redirect("/autopost/");
|
||||||
|
|||||||
10
readme.md
10
readme.md
@ -157,6 +157,16 @@ location /autopost/ {
|
|||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header X-Forwarded-Host $http_host;
|
proxy_set_header X-Forwarded-Host $http_host;
|
||||||
}
|
}
|
||||||
|
location /js/ {
|
||||||
|
proxy_pass http://127.0.0.1:XXXXX;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header X-Forwarded-Host $http_host;
|
||||||
|
}
|
||||||
|
location /jquery/ {
|
||||||
|
proxy_pass http://127.0.0.1:XXXXX;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header X-Forwarded-Host $http_host;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
> Pensez à bien remplacer XXXXX par le port que vous avez mis dans votre config.js
|
> Pensez à bien remplacer XXXXX par le port que vous avez mis dans votre config.js
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user