Qt+Cutelyst学习笔记(二十一)win10+Qt5.15.2 添加一个简单的删除功能

前言:

本篇继续在前一篇的基础上实现

之前一直都记录如何添加数据,本次实现一个简单的删除功能

一、修改控制器源码

1.编辑src/books.h并输入以下方法:

class Books : public Controller
{
public:
    ...
    /**
     * Fetch the specified book object based on the book ID and store
     * it in the stash
     */
    C_ATTR(object, :Chained("base") :PathPart("id") :CaptureArgs(1))
    void object(Context *c, const QString &id);

    /**
     * Fetch the specified book object based on the book ID and store
     * it in the stash
     */
    C_ATTR(delete_obj, :Chained("object") :PathPart("delete") :Args(0))
    void delete_obj(Context *c);
};

本次添加了两个函数 ,

第一个,根据提交的书本id,查询出书本相关的信息,并存储起来

第二个,根据已查询出的书本信息,执行删除操作

 2.编辑src/books.cpp,以实现方法

void Books::object(Context *c, const QString &id)
{
    // Find the object on the database
    QSqlQuery query = CPreparedSqlQueryThreadForDB("SELECT * FROM book WHERE id = :id", "MyDB");
    query.bindValue(":id", id);
    if (query.exec()) {
        c->setStash("object", Sql::queryToHashObject(query));
    } else {
        // You would probably want to do something like this in a real app:
        // c->detach("/error_404");
    }
    qDebug() << "*** INSIDE OBJECT METHOD for obj id=" << id << " ***";
}

void Books::delete_obj(Context *c)
{
    QVariantHash book = c->stash("object").toHash();
 
    // Delete the object on the database
    QSqlQuery query = CPreparedSqlQueryThreadForDB("DELETE FROM book WHERE id = :id", "MyDB");
    query.bindValue(":id", book.value("id"));
    if (query.exec()) {
        // Set a status message to be displayed at the top of the view
        c->setStash("status_msg", "Book deleted.");
    }
 
    // Forward to the list action/method in this controller
    c->forward("list");
}

在删除操作完后,再返回到图书列表界面

二、修改动作模板

在root/src/books/list.html中,添加删除链接,修改如下

{% comment %}This is a Grantlee comment.{% endcomment %}
 
{% comment %}Some basic HTML with a loop to display books{% endcomment %}
<table>
<tr><th>Title</th><th>Rating</th><th>Author(s)</th><th>Links</th></tr>
{% comment %}Display each book in a table row{% endcomment %}
{% for book in books %}
  <tr>
    <td>{{ book.title }}</td>
    <td>{{ book.rating }}</td>
    <td></td>
    <td>
      {% comment %} Add a link to delete a book {% endcomment %}
      <a href="/books/id/{{ book.id }}/delete">Delete</a>
    </td>
  </tr>
{% endfor %}
</table>

代码是在表的右侧添加一个新列,并带有一个“Delete”按钮(为了简单起见,将使用链接而不是完整的HTML按钮;但是,在实践中,任何修改数据的操作都应该通过发送POST请求的表单来处理)。

注意:在实践中,永远不要使用GET请求来删除记录——对于修改数据的操作,始终使用POST。我们在这里这样做只是为了说明和简单的目的

三、编译并测试

直接编译,生成动态库,并运行服务,输出如下

 可以看到删除的链已添加

在浏览器中访问http://localhost:3000/books/list

运行如下所示

点击一个删除,会减少一行,显示如下 

 本测试源码下载

后记:

本篇已经实现了一个简单的删除功能

在删除成功后,添加了一个转发,若不添加这个转发,会停留在删除上,而删除并没有指定动作模板,会出错的,如下所示

下一篇,笔者探究简单删除功能存在的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁白杨树下꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值