iou3344 2021-10-25 17:54 采纳率: 85.2%
浏览 129
已结题

Datatables前端页面的一个难题(Server-side模式下)


<!DOCTYPE html>
<html lang="en-US">
<head>
<title>ajax_demo</title>
<meta charset="utf-8">
</head>
<body>
<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
</table>
<style>
    #dvType {
        text-align: center;
        font-weight: bold;
        line-height: 50px;
    }
    .r{color:#f00}
</style>
<div id="dvType">单选或多选:<input type="radio" name="type" value="0" />重置 <input type="radio" name="type" value="1" />单选搜索 <input type="radio" name="type" value="2" />多选搜索 <input type="button" id="btnMul" style="display:none" value="开始搜索" /></div>
<link href="https://cdn.datatables.net/1.11.1/css/jquery.dataTables.min.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.1/js/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function () {
        var eg = $('#example'), searchBox, type = '0', keywords,
            searchCols = [0, 1, 2]//要要处理的列下标,从0开始,0表示第一列,1第二列,依次类推
var $dt = eg.DataTable({
            "ajax": "data.txt",
            initComplete: function (settings, json) {//加载完毕事件
                eg.after($('#dvType'));//插入搜索类型
                searchBox = eg.prev().find('input');
            },
            rowCallback: function (row) {//关键字高亮
                row = $(row);
                row.find('span.r').removeClass('r');
                if (type == '2' || type == '0') {
                    row.find(':checkbox' + (type == '0' ? ',span.single' : '')).parent().html(function () { return this.innerText })
                }
                else {
                    if (!row.find('span.single').length) {//这行没有格式化过,需要重新格式化
                        row.find('td').each(function () {
                            var cellIndex = this.cellIndex;
                            if (searchCols.findIndex(function (v) { return v == cellIndex }) == -1) return true;//不包含处理的列退出
                            this.innerHTML= this.innerText.split(' ').map(function (i) {
                                if (i) return '<span class="single">' + i + '</span>'
                                return ''
                            }).join(' ');
                        });
                    }
                }
                //高亮操作
                if (keywords) {
                    var re = new RegExp('(' + keywords.split(' ').join('|') + ')', 'ig');
                    row.find('td').each(function () {
                        var cellIndex = this.cellIndex;
                        if (searchCols.findIndex(function (v) { return v == cellIndex }) == -1) return true;//不包含处理的列退出
                        if (type == '1') $('span', this).each(function () {
                            if (re.test(this.innerText)) this.classList.add('r')
                        });
                        else this.innerHTML = this.innerText.replace(re, '<span class=r>$1</span>')
                    });
                }
            }
        });
        eg.on('click', 'span.single', function () {
            searchBox.val(this.innerHTML);//执行搜索
            btnMul.trigger('click', true);
        }).on('click', ':checkbox', function () {
            var s = searchBox.val();
            if (this.checked) s += (s ? ' ' : '') + this.value;
            else s = s.replace(' ' + this.value, '').replace(this.value + ' ', '');
            searchBox.val($.trim(s));
        });
        var btnMul = $('#btnMul');
        btnMul.click(function () {////多选只能通过按钮来触发,要不无法知道用户是否已经选择完所有关键字
            var s = searchBox.val();
            keywords = s;
            searchBox.trigger('input');
        });
        $('#dvType :radio').click(function () {
            type = this.value;
            if (type == '0') {
                searchBox.val(keywords = '');
                $dt.search('').draw();
                return;
            }
            $('#btnMul')[type == '2' ? 'show' : 'hide']();
            eg.find('tbody td').each(function () {
                var cellIndex = this.cellIndex;
                if (searchCols.findIndex(function (v) { return v == cellIndex }) == -1) return true;//不包含处理的列退出
                this.innerHTML = this.innerText.split(' ').map(function (i) {
                    if (i) return type == 1 ? '<span class="single">' + i + '</span>' : '<input type="checkbox" value="'+i.replace(/"/g,'&quot;')+'"/>' + i
                    return ''
                }).join(' ');
            });
        });
    });
</script>

以上代码在ajax方式读取本地json数据模式下( https://datatables.net/examples/ajax/simple.html )运行正常,但是换成Datatables的Server-side方式( https://datatables.net/examples/server_side/simple.html ) 其他都没改的情况下,唯独改了如图所示那一块后,单选和多选搜索都失效了:多选可以构造出多选框,但是勾选后点击搜索无效。单选时,点击单词选不中也搜不了,非常的郁闷~

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-10-25 22:54
    关注
                initComplete: function (settings, json) {//加载完毕事件
                    eg.after($('#dvType'));//插入搜索类型
                    searchBox = eg.prev().find('input');
                }
    
    

    改下面这样,由于加了processing:true,会显示加载层,导致dom结构变了,获取不到searchBox对象。

    
                initComplete: function (settings, json) {//加载完毕事件
                    console.log('initComplete')
                    eg.after($('#dvType'));//插入搜索类型
                    searchBox = eg.parent().find('input');
                }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月3日
  • 已采纳回答 10月26日
  • 修改了问题 10月26日
  • 创建了问题 10月25日

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”