前端使用search搜索器、with(关联)和 field

使用情况:

在很多页面获取到的数据相近,
区别只是多获取几个字段、多几个搜索条件、多关联几张表的时候。
后端就可以小小的偷个懒,
接口只写一个,前端使用search搜索器、with(关联)和 field来精准的获取需要的数据。
这样减少了接口的数量也减少了使用同一个接口导致获取了冗余的数据。

need_count: 0, // 0不需要total(总数)1需要
with_trashed: 0, // 0不需要获取删掉的数据1需要

search: { // 相当于添加搜索条件
    id: this.id,
    enable: 1
},
field: ['id', 'sn', 'sort', 'name'], // 加上额外需要获取的一些字段(需要注意的是:有一些字段在关联的表中,因此需要使用with关联上对应的表才能正确响应你添加的field中的那个字段)

with: ['category', 'sku'] // 关联上category和sku两个表,就可以field带上这两个表中的字段

接口文档会给出支持的search(搜索器)、支持的with(关联)、支持的field

示例:

支持的search(搜索器)
name 搜索表字段name(商品名称)
category_id 搜索表字段category_id(商品分类id)
foods_class 搜索字段foods_class(菜品类型 1 单品 2 套餐)

支持的with(关联)
category 关联出platform_goods_category表(platform_goods_category表的id字段与platform_goods表的category_id字段关联)
sku关联出platform_goods_sku表(platform_goods_sku表的goods_id字段与platform_goods表的id字段关联)
attribute关联出platform_goods_attribute表(platform_goods_attribute表的goods_id字段与platform_goods表的id字段关联)

this.$apis.getGoodsList({
    search: {
      brand_id: this.brandId,
      foods_class: this.fromId
    },
    field: ['foods_class', 'id', 'name', 'sn', 'sort', 'enable'],
    with: ['category', 'sku']
}).then(res => {})

简洁 (0.0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于 Next.js 和 TypeScript 的简单搜索过滤器的示例代码: 首先,我们需要安装必要的依赖: ```bash npm install next react react-dom typescript @types/react @types/node ``` 然后在根目录下创建一个 `pages` 文件夹,在其创建一个名为 `index.tsx` 的文件,作为我们的搜索页面: ```tsx import React, { useState } from 'react'; interface SearchResult { title: string; description: string; } const searchResults: SearchResult[] = [ { title: 'Next.js', description: 'The React Framework for Production', }, { title: 'TypeScript', description: 'Typed JavaScript at Any Scale.', }, { title: 'React', description: 'A JavaScript library for building user interfaces.', }, ]; const IndexPage: React.FC = () => { const [query, setQuery] = useState(''); const [results, setResults] = useState<SearchResult[]>([]); const handleSearch = (event: React.FormEvent<HTMLFormElement>) => { event.preventDefault(); const filteredResults = searchResults.filter( (result) => result.title.toLowerCase().includes(query.toLowerCase()) || result.description.toLowerCase().includes(query.toLowerCase()) ); setResults(filteredResults); }; return ( <div> <form onSubmit={handleSearch}> <input type="text" placeholder="Search" value={query} onChange={(event) => setQuery(event.target.value)} /> <button type="submit">Search</button> </form> {results.length > 0 ? ( <ul> {results.map((result, index) => ( <li key={index}> <h2>{result.title}</h2> <p>{result.description}</p> </li> ))} </ul> ) : ( <p>No results found.</p> )} </div> ); }; export default IndexPage; ``` 在上面的示例代码,我们定义了一个 `SearchResult` 接口,它包含 `title` 和 `description` 两个属性。然后我们创建了一个包含几个示例搜索结果的数组 `searchResults`。 在组件的状态,我们使用 `useState` 钩子定义了两个状态变量:`query` 和 `results`。`query` 存储用户的搜索查询,`results` 存储过滤后的搜索结果。 在 `handleSearch` 函数,我们使用数组的 `filter` 方法来过滤搜索结果,并将过滤后的结果存储在 `results` 状态变量。 最后,在组件的渲染,我们使用一个表单和一个输入框来获取用户的搜索查询。当用户提交查询时,我们调用 `handleSearch` 函数来更新搜索结果列表的内容。如果没有搜索结果,则显示一个简单的文本消息。 希望这个示例代码能够帮助您开始编写自己的基于 Next.js 和 TypeScript 的搜索过滤器!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值