删除一篇文章并从下一页拉入第一篇 [英] Delete an article and pull in the first one from next page

查看:25
本文介绍了删除一篇文章并从下一页拉入第一篇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用分页系统构建一个博客(例如每页 5 篇文章).分页本身一切都很好,但在每篇文章旁边我都包含了一个删除"按钮,允许管理员动态删除(jquery)相关文章及其内容.我现在想要的是删除文章时追加"(不刷新)下一页的第一篇文章,删除另一篇文章时添加第二篇,然后是第三篇,依此类推.如果我继续删除文章而不刷新,我希望这样可以避免出现空白页面.

I'm trying to build a blog with a pagination system (ex. 5 articles per page). Everything is fine with the pagination itself but next to each article I included a "delete" button allowing the admin to remove dynamically (jquery)the related article and its content. What I want now is to "append" (without refreshing) the first article of the next page when I delete an article, the second when I delete another article, then the third and so on. I want this to avoid to have a blank page if I keep on deleting articles without refreshing.

我实际上是在使用 php 和一个 mysql 数据库来存储数据对于分页,我正在使用类似的东西链接.

I'm actually using php and a mysql database to store data for paginating I'm using something similar to this link.

推荐答案

我原先回答了重复的,但认为可能会被删除,所以复制到这里:

I originally answered on the duplicate, but think that might get deleted, so copying here:

jQuery 让它变得简单又好用.这假设您有一个名为 ajax/delete-article.php 的 php 文件,它以要删除的文章 id 作为参数,以页面上最后一篇文章的文章 id 作为参数,并返回下一篇文章.它还假设您的所有文章都具有article"类和 idarticle-230",其中 230 是 ID 号.我相信您可以根据自己的 HTML 结构调整它.

jQuery makes it nice and easy. This assumes you've got a php file called ajax/delete-article.php which takes an article id to be deleted as a parameter, and the article id of the last article on the page as a parameter, and returns the HTML of the next article. It also assumes all your articles have the "article" class, and the id "article-230" where 230 is the ID number. I'm sure you can adapt it to your own HTML structure.

$('.article-delete').click(function (event) {
  $(this).closest('.article').remove(); // possibly use a more fancy-looking hide(), and remove on complete?

  var lastArticleId = $('.article').last().attr('id').replace('article-','');
  var thisArticleId = $(this).attr('id').replace('article-','');
  $.get('ajax/next-article.php?last-article=' + lastArticleId + '&article-to-delete=' + thisArticleId ,function (data) {
    $('#article-list').append(data);
  });
  event.preventDefault();
});

请注意,这只是一个粗略的例子.将需要一些改进以提高健壮性.它也没有经过测试,所以它可能会到处抛出 JS 错误.

Note that this is just a rough example. Will require some improvements for robustness. Its also untested, so it might throw up JS errors everywhere.

这篇关于删除一篇文章并从下一页拉入第一篇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆