前端使用js模糊搜索

该博客介绍了如何使用layui前端UI框架实现数据表格的搜索和刷新功能。通过监听搜索框的输入事件,实现了按指定列进行搜索,并在回车或点击搜索按钮时调整返回数据的数量。同时,提供了刷新按钮来重新加载表格数据。代码中展示了layui的table、layer和form模块的使用,以及如何处理和展示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

成品

1

2

3

4手机端

 

前端UI使用layui的数据表格,代码如下

    function load(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;
        var cacheData = []; //列表缓存数据
        var tempData = []; //搜索出来的数据
        var secols = ['channelName', 'description', 'idName', 'urls']; //只搜索指定列,默认搜索台号列
		//var secols = ['channelName', 'urls']; //手机端使用,只搜索指定列
        table.render({
            elem: '#demo'
            , id: 'demoid'
            , url: '/api/getchsOfStatus?status=-1&allchs=' + $("#allshow").prop("checked")
            , method: 'get'
            , limit: 30
            , page: true
            , cols: [[
                { type: 'numbers' }
                , { field: 'channelId', title: '台号', width: 54, sort: true }
                , { field: 'channelName', title: '频道名', align: 'left', width: 200, sort: true }
                , { field: 'duration', title: '持续', sort: true, align: 'left', width: 100, templet: '<div>{{ SecFormat(d.duration)}}</div>' }
                , { field: 'bitRate', title: '码率', align: 'left', width: 60, sort: true }
                , { field: 'transCodeEnable', title: '⇅', align: 'left', sort: true, width: 26, templet: '<div>{{ CodecFormat(d)}}</div>' }
                , { field: 'platform', title: '', align: 'left', width: 26, templet: '<div>{{ PlatFormat(d.platform)}}</div>' }
                , { field: 'streamCodec', title: '格式', align: 'left', width: 180 }
                , { field: 'description', title: '备注', align: 'left', width: 160 }
                , { field: 'idName', title: '频道ID', align: 'left', width: 80 }
                , { field: 'urls', title: 'URls', align: 'left', templet: '<div>{{ UrlsFormat(d)}}</div>' }
                , { fixed: 'right', title: '操作', width: 100, toolbar: '#barDemo' }
            ]]
            , done: function (d) {
                $('#channeltotalNum').text("频道数:" + d.count);
                cacheTable = this;
                if (this.url.length > 5) {
                    cacheData = table.cache['demoid'];
                    var j = 0;
                    for (var i = 0; i < cacheData.length; i++) {
                        if (cacheData[i].status == 0) {
                            j++;
                        }
                    }
                    $('#offNum').text(j);
                }
            }
        });
        //搜索框输入,默认返回20条,回车键返回100条,点击搜索按钮返回全部
        $("#s_title").on('keyup', function (event) {
            var demoReload = $('#s_title').val();
            //keyCode=13是回车键
            if (event.keyCode === 13) {
                Search(demoReload, 100);
            } else {
                Search(demoReload, 20);
            }
        });
        //点击搜索
        $("#mysearch").click(function () {
            var st = $('#s_title').val();
            if (st.length > 0) {
                Search(st, 1000);
            }
        });
        //刷新
        $("#myrefresh").click(function () {
            Tbreload(false);
        });
        //搜索
        function Search(value, num) {
            tempData = [];//清空临时数据
            if (value) {
                if (cacheData.length > 0) {
                     if (/^\d+$/.test(value)) {
                    for (index = 0; index < cacheData.length; index++) {
                        if (String(cacheData[index]["channelId"]).indexOf(value) == 0) {
                            tempData.push(cacheData[index]);
                            if (tempData.length >= num) {
                                break;
                            }
                        }
                    }
                }
                else {
                    value = "^.*?" + value.trim().toLowerCase() + ".*?$";
                    let re = new RegExp(value.replace(" ", ".*?"));
                    let str = '';
                    for (index = 0; index < cacheData.length; index++) {
                        for (j = 0; j < secols.length; j++) {
                            str = String(cacheData[index][secols[j]]).toLowerCase();
                            if (re.test(str)) {
                                tempData.push(cacheData[index]);
                                break;
                            }
                        }
                        if (tempData.length >= num) {
                            break;
                        }
                    }
                }
            }
            } else {
                tempData = cacheData;
            }
            Tbreload(true);
        }
        //表格重新载入数据
        function Tbreload(isCache) {
            if (isCache) {
                table.reload('demoid', {
                    page: { curr: 1 }
                    , url: ''
                    , data: tempData
                });
            } else {
                table.reload('demoid', { url: '/api/getchsOfStatus?status=-1&allchs=' + $("#allshow").prop("checked") });
            }
        }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值