如何使用 Golang Echo 框架在我的分页下创建搜索框

问题描述 投票:0回答:1
// Get page number from path `product/:page`
        pageNumber := c.Param("page")
        page, err := strconv.Atoi(pageNumber)
        if err != nil || page < 1 {
            page = 1
        }

        // set pageSize to 10 for now
        pageSize := 10

        offset := (page - 1) * pageSize

        // get the rows paginated by offset
        rows, err := database.WrapQuery(dbconnections.DBPool, ctx, "GetFromProductPaginatedByOffset", pageSize, offset)
        if err != nil {
            loggerWithTrace.Error().Err(err).Caller().Msg("database.WrapQuery error")
        }
        defer rows.Close()

        results := make([]models.Product, 0)

        for rows.Next() {
            var data models.Product
            err := rows.Scan(&data.Prefix, &data.Suffix, &data.Usage)
            if err != nil {
                loggerWithTrace.Error().Err(err).Caller().Msg("database.WrapQuery error")
            }
            results = append(results, data)
        }

        // Get the total item of product 
        var totalItems int
        err = database.WrapQueryRow(dbconnections.DBPool, ctx, "GetTotalSizeFromProduct").Scan(&totalItems)
        if err != nil {
            loggerWithTrace.Error().Err(err).Caller().Msg("database.WrapQueryRow error")
        }

        totalPages := totalItems / pageSize
        nextPage := page + 1
        prevPage := page - 1

        templateDataMap["DataMap"] = DataMap
        templateDataMap["Product"] = results
        templateDataMap["Page"] = page
        templateDataMap["PageSize"] = pageSize
        templateDataMap["TotalItems"] = totalItems
        templateDataMap["PageItemCount"] = pageSize
        templateDataMap["TotalPages"] = totalPages
        templateDataMap["NextPage"] = nextPage
        templateDataMap["PrevPage"] = prevPage

        return c.Render(http.StatusOK, "product", templateDataMap)

**这是我在处理程序中的分页代码, **//更多代码在这里连接登录和 cookies

**这是我的sql **Q_GET_PAGINATION_FROM_PRODUCT =

SELECT * FROM PRODUCT where (prefix like '%%%s%%' or "usage" like '%%%s%%') LIMIT $1 OFFSET $2
Q_GET_FROM_PRODUCT_TOTAL_SIZE =
SELECT count(*) FROM PRODUCT

**这是我的路线 **PATH_PAGINATE_PRODUCT = PATH + "/product/:page"

e.GET(PATH_PAGINATE_PRODUCT, handlers.Product)

`

我尝试创建搜索框/搜索查询,但它似乎没有用

html go echo searchbar
1个回答
0
投票

在 SQL 查询的 LIKE 运算符中“prefix”和“usage”,您必须传递一个参数,如下例所示

//... some logic
// change your request like this
prefix = "%"+data.Prefix+"%"
usage = "%"+data.Usage+"%"

然后你的SQL查询必须改成这个

SELECT * FROM PRODUCT where (prefix like '%s' or "usage" like '%s') LIMIT $1 OFFSET $2
© www.soinside.com 2019 - 2024. All rights reserved.