express+mysql 增删改查
路由:user.js
链接数据库
var mysql = require('mysql');
var connection = mysql.createPool({
host: 'localhost',
port: 3306,
user: 'root',
password: 'root',
database: 'xynews'
});
查询所有数据:
/* 查询所有*/
router.get('/getlist', function(req, res, next) {
connection.query('SELECT * FROM `baidunews2` where 1', function(err, rows) {
console.log(rows);
res.json(rows);
console.log("aaaa");
})
});
admin.js部分:
function getNews() {
console.log("aaaa");
var $lists = $(".all");
$.ajax({
url: '/admin/getlist',
type: 'get',
datatype: 'json',
success: function(data) {
console.log(data);
for (var i = 0; i <= data.length - 1; i++) {
var $list = $("<div class='list'></div>").appendTo($(".all"));
var $list_1 = $('<span class="newid">' + data[i].news_id + '</span>').appendTo($list);
var $list_2 = $('<span class="newtext">' + data[i].news_text + '</span>').appendTo($list);
// var $list_3 = $('<span class="del" onclick=delNews(' + data[i].news_id + ')>' + '删除</span>').appendTo($list);
var $list_3 = $('<span class="del" data-id=' + data[i].news_id + '>' + '删除</span>').appendTo($list);
var $list_4 = $('<span class="edit" data-id=' + data[i].news_id + '>' + '修改</span>').appendTo($list);
}
}
})
}
修改一条数据:
router.post('/update', function(req, res) {
var news_id = req.body.newsid;
new_text = req.body.newstext;
connection.query('UPDATE `baidunews2` SET `news_text` = ? where `news_id` = ? ', [new_text, news_id], function(err, result) {
// console.log("slfkaslf" + result);
res.json(result);
});
});
注意,需要返回res.json(result),admin.js的请求才能success;另外,点击事件要用on监听;
$(document).on('click', '.edit', function(e) {
$(".editblock").show();
edit($(this).parent().index());
});
//模态框取值
function edit(index) {
var id = $(".list").eq(index).find('.newid').html();
var text = $(".list").eq(index).find('.newtext').html();
console.log($(".list").eq(index).find(".newtext").html());
$(".editid").html(id);
$(".texts").val(text);
$(".okbtn").data("id", id);
console.log("data:" + $(".okbtn").data("id"));
}
$(".okbtn").click(function() {
updatanews($(this).data("id"));
});
删除一条数据:
router.post('/dellist', function(req, res) {
var news_id = req.body.newsid;
connection.query('DELETE FROM `baidunews2` WHERE `baidunews2`.`news_id`= ?', [news_id], function(err, result) {
// console.log("slfkaslf" + result);
res.json(result);
});
});
$(document).on('click', '.del', function(e) {
delNews($(this).data("id"))
});
function delNews(id) {
console.log(id)
$.ajax({
url: '/admin/dellist',
type: 'post',
data: { newsid: id },
success: function(data) {
$(".all").empty();
getNews();
}
})
}
增加一条数据:
router.post('/add', function(req, res) {
var newtype = req.body.newtype;
newstexts = req.body.newstext;
connection.query('INSERT INTO `baidunews2`(`news_id`, `news_text`, `newstype`) VALUES (\' \',?,?);', [newstexts, newtype], function(err, result) {
// console.log("slfkaslf" + result);
res.json(result);
});
});
function addnews() {
console.log($(".addtype").val()+","+$(".addtexts").val())
$.ajax({
url: '/admin/add',
type: 'post',
data: {
newtype: $(".addtype").val(),
newstext: $(".addtexts").val()
},
success: function(data) {
$(".all").empty();
getNews();
}
})
}
$(".okbtn2").click(function() {
addnews();
});