Как фильтровать список без перезагрузки страницы?
Вложений: 3
Логика:
- пользователь вводит критерии фильтра в поле фильтра; - результат: список фильтруется без перезагрузки страницы; Примечание Критерием для фильтра является текст. Фильтрация осуществляется по полю: title В данном примере текущее содержание поля title, текст типа "Articles-…’ ", является условным. В будущем в поле title может находится словосочетание из нескольких слов ( как текстовый заголовок, так и ФИО и т.д. по аналогии). home.controller.js const fs = require('fs'); exports.getHomePage = (req, res) => { let query = "SELECT * FROM `articles` ORDER BY id ASC"; // query database to get all the players // execute query db.query(query, (err, result) => { if (err) { res.redirect('/'); } res.render('index.ejs', { title: "Welcome to DB", articles: result }); }); }; index.routes.js const express = require("express"); const router = express.Router(); const homeController = require('../controllers/home.controller'); router.get('/', homeController.getHomePage); module.exports = router; app.js const express = require('express'); const fileUpload = require('express-fileupload'); const bodyParser = require('body-parser'); const mysql = require('mysql'); const path = require('path'); const app = express(); const articleRoutes = require('./routes/article.routes'); const homeRoutes = require('./routes/index.routes'); const port = 2000; // create connection to database // the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name. const db = mysql.createConnection ({ host: 'localhost', user: 'root', password: '', database: 'socka' }); // connect to database db.connect((err) => { if (err) { throw err; } console.log('Connected to database'); }); global.db = db; // configure middleware app.set('port', process.env.port || port); // set express to use this port app.set('views', __dirname + '/views'); // set express to look in this folder to render our view app.set('view engine', 'ejs'); // configure template engine app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // parse form data client app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder app.use(fileUpload()); // configure fileupload // routes for the app app.use('/', homeRoutes); // app.use('/player', playerRoutes); app.use('/article', articleRoutes); app.get('*', function(req, res, next){ res.status(404); res.render('404.ejs', { title: "Page Not Found", }); }); // set the app to listen on the port app.listen(port, () => { console.log(`Server running on port: ${port}`); }); index.ejs https://jsfiddle.net/User65659/tz01nk7b/4/ SQL Код:
CREATE DATABASE socka; |
Часовой пояс GMT +3, время: 04:32. |