vue3+前端分页+模糊查询

import { formatDateTime } from '@fl/utils/fn.ts'

import { cloneDeep, sortBy } from 'lodash-es'

import { reactive } from 'vue'

import { useDicts } from './use-dicts'

const { useDict } = useDicts()

export function useTable() {

    const gridOptions = reactive<any>({

        border: true,

        columnConfig: {

            resizable: true,

        },

        columns: [

            // { slots: { checkbox: 'checkbox_cell', header: 'checkbox_header' }, type: 'checkbox', width: 60 },

            { field: 'enterpriseId', title: '企业号', width: 100 },

            { field: 'enterpriseName', title: '企业名称', width: 200 },

            { field: 'status', slots: { default: 'status' }, title: '企业状态', width: 90 },

            { field: 'enterpriseShort', title: '企业简称', width: 150 },

            { field: 'enterpriseLevel', slots: { default: 'enterpriseLevel' }, title: '企业级别', width: 100 },

            { field: 'unifiedSocialCreditCode', title: '信用代码', width: 200 },

            { field: 'legalRepresentative', title: '企业法人', width: 90 },

            { field: 'cooperationType', slots: { default: 'cooperationType' }, title: '合作类型', width: 90 },

            { field: 'enterpriseAccount', title: '企业管理员', width: 90 },

            { field: 'createTime', title: '添加时间', width: 150 },

            { field: 'createBy', title: '添加人', width: 100 },

            { slots: { default: 'operate' }, title: '操作', width: 300 },

        ],

        data: [],

        editConfig: {

            mode: 'row',

            showStatus: true,

            trigger: 'manual',

        },

        height: 530,

        keepSource: true,

        loading: false,

        pagerConfig: {

            currentPage: 1,

            enabled: true,

            pageSize: 10,

            pageSizes: [10, 20, 50, 100, 200, 500],

            total: 0,

        },

        showOverflow: true,

        stripe: true,

    })

    const btns = [

        {

            label: '查看',

            text: true,

            value: 'view',

        },

        {

            label: '编辑',

            text: true,

            value: 'edit',

        },

        {

            label: '删除',

            text: true,

            value: 'delete',

        },

        {

            label: '组织机构',

            text: true,

            value: 'organization',

        },

    ]

    // 删除空属性

    const paramsIsNull = (obj: any) => {

        const _newPar = {}

        for (const key in obj) {

            if (

                (obj[key] === 0 || obj[key])

                && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== ''

            )

                _newPar[key] = obj[key]

        }

        // 返回新对象

        return _newPar

    }

    // TODO:多条件查询方法 仅字符串对比 模糊匹配

    const useQuery = (data: any, params: any) => {

        const notNullParams = paramsIsNull(params)

        return data.filter((item) => {

            return Object.keys(notNullParams).every((key: string) => {

                if (key === 'createTime') {

                    const target = item[key] ? item[key].replace('T', ' ') : ''

                    const [start, end] = notNullParams[key]

                    console.log('start--', formatDateTime(start))

                    console.log('end--', formatDateTime(end))

                    const t = new Date(target).getTime()

                    const s = new Date(start).getTime()

                    const e = new Date(end).getTime()

                    return (

                        s <= t && e >= t

                    )

                }

                else {

                    // eslint-disable-next-line unicorn/prefer-includes

                    const flag = item[key] ? item[key].indexOf(notNullParams[key]) !== -1 : false

                    return (

                        item[key] !== undefined

                        && flag

                    )

                }

            })

        })

    }

    // 前端生成企业号 enterpriseId 默认不可改:取最大企业号加1

    const createQiyeh = (data: any) => {

        const all = data.map((v: any) => {

            if (v.enterpriseId) {

                const target = Number(v.enterpriseId)

                if (typeof target === 'number' && !Number.isNaN(target))

                    return target

                return 0

            }

            else { return 0 }

        })

        const target = sortBy(all).reverse()

        return target[0] + 1

    }

    // 前端分页

    const usePage = (data: any, gridOptions: any) => {

        const { currentPage, pageSize } = gridOptions.pagerConfig

        const val = cloneDeep(data)

        const list = val.splice((currentPage - 1) * pageSize, pageSize)

        return {

            list,

            total: data.length,

        }

    }

    // 处理业务类别

    const showCatType = (catTypeId: any) => {

        if (!catTypeId)

            return ''

        const reg = /[\u4E00-\u9FA5]/

        if (reg.test(catTypeId))

            return catTypeId

        const arr = catTypeId.split(',')

        const str = arr.reduce((accumulate, cur) => {

            accumulate += `${useDict('CatType', cur)},`

            return accumulate

        }, '')

        return str.substring(0, str.length - 1)

    }

    return { btns, createQiyeh, gridOptions, showCatType, usePage, useQuery }

}

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中模糊查询数据库的数据,通常需要结合后端技术来实现。以下是一种常见的实现方式: 1. 创建一个包含输入框和展示查询结果的组件。 2. 在输入框中监听用户输入的关键词,并将关键词传递给后端进行模糊查询。 3. 后端接收到关键词后,使用数据库查询语言(如SQL)进行模糊查询,并返回匹配的结果。 4. 前端接收到后端返回的结果后,将结果展示在页面上。 具体的实现步骤可能因后端技术的不同而有所差异,以下是一种基于Vue 3和Node.js(使用Express框架)的实现示例: 1. 在Vue组件中,定义一个输入框和一个用于展示结果的列表: ```html <template> <div> <input v-model="keyword" @input="search" placeholder="输入关键词"> <ul> <li v-for="item in searchResults" :key="item.id">{{ item.name }}</li> </ul> </div> </template> <script> export default { data() { return { keyword: '', searchResults: [], }; }, methods: { async search() { const response = await fetch(`/api/search?keyword=${this.keyword}`); this.searchResults = await response.json(); }, }, }; </script> ``` 2. 在后端使用Express框架创建一个路由处理模糊查询的请求: ```javascript const express = require('express'); const app = express(); app.get('/api/search', (req, res) => { const keyword = req.query.keyword; // 使用SQL进行模糊查询,这里以MySQL为例 const query = `SELECT * FROM your_table WHERE name LIKE '%${keyword}%'`; // 执行查询并返回结果 // ... res.json(searchResults); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` 这只是一个简单的示例,实际的实现可能还需要考虑数据分页、防止SQL注入等安全性问题。同时,你还需要根据你所使用的数据库和后端技术进行相应的调整。 希望以上信息能帮助到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值