how to fix search results with datatable

  datatable, javascript, laravel, php

good morning, i want to ask. how to fix search results with datatable?
as in the picture, when I enter the keyword "corp", data that doesn’t have the keyword "corp" appears.
enter image description here

index.blade.php

@extends('layouts.be')

@push('css')
    <link rel="stylesheet" type="text/css" href="{{ asset('css/backend/select2.min.css')}}">
    <style>
        .btn-sm {
        padding: .25rem .5rem !important;
            font-size: 12px !important;
            margin: 3px 0px !important;
            height: 28px !important;
        }
        .avatar {
            width: 50px; border-radius: 50%; height: 50px; object-fit: cover;
        }
    </style>
@endpush

@section('topbar')
    @component('component.topbar', [
            'top' => 'true',
            'topmenu' => [
                0 => ['Learners Report','learner-panel'],
                1 => ['Courses Report','course-panel'],
            ]
        ])
    @endcomponent
@endsection

@section('content')
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active show" id="learner-panel">
            <div class="row page-titles">
                <div class="col-md-12 align-self-center">
                    <h5 class="subtitle type-paragraph5">Learning Report</h5>
                    <h4 class="title">Learners Report</h4>
                    <div class="jinja jinja-hr-1 mb-3">
                        <div><hr class="titlehr"></div>
                        {{-- <div>
                            <a href="{{ route('lms.learningreport.download.learners', ['pfx' => $pfx]) }}" 
                                class="btn btn-secondary-outline" target="_blank">Download Report</a>
                        </div> --}}
                        <div>
                            <a href="{{ route('lms.learningreport.download.summary', ['pfx' => $pfx]) }}" 
                                class="btn btn-secondary-outline" target="_blank">Download Summary</a>
                        </div>
                        {{-- <h5 class="subtitle type-paragraph5">Learning report by learners in your company</h5> --}}
                    </div>
                    
                </div>
            </div>

            <div class="row">
                <div class="col-12 m-t-20 mb-3">
                    <div class="panel">
                        <div class="nav-pagelength">
                            <label>
                                <span class="text-muted">Show</span>
                                <select id="learnerDataLength" class="pagelength-control">
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select>
                                <span class="text-muted">entries</span>
                            </label>
                        </div>
                        <div class="type-paragraph2 nav-search">
                            <input id="searchLearner" class="form-control type-paragraph2" type="text" placeholder="Search ...">
                        </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <table id="learnerReport" class="display datatables" style="width:100%">
                        <thead>
                            <tr>
                                <th class="font-demi"></th>
                                <th class="font-demi">Learner name</th>
                                <th class="font-demi">Department</th>
                                <th class="font-demi">Job title</th>
                                <th class="font-demi">Course Taken</th>
                                <th class="font-demi"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>

        </div>

        <div role="tabpanel" class="tab-pane" id="course-panel">
            <div class="row page-titles">
                <div class="col-md-12 align-self-center">
                    <h5 class="subtitle type-paragraph5">Learning Report</h5>
                    <h4 class="title">Courses Report</h4>
                    <div class="jinja jinja-hr mb-3">
                        <div><hr class="titlehr"></div>
                        {{-- <h5 class="subtitle type-paragraph5">Learning report by courses of your learner</h5> --}}
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-12 m-t-20 mb-2">
                    <div class="panel">
                        <div class="nav-pagelength">
                            <label>
                                <span class="text-muted">Show</span>
                                <select id="courseDataLength" class="pagelength-control">
                                    <option value="10">10</option>
                                    <option value="25">25</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select>
                                <span class="text-muted">entries</span>
                            </label>
                        </div>
                        <div class="type-paragraph2 nav-search">
                            <input id="searchCourse" class="form-control type-paragraph2" type="text" placeholder="Search ...">
                        </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <table id="coursereport" class="display datatables" style="width:100%">
                        <thead>
                            <tr>
                                <th width="30%" class="font-demi">Course name</th>
                                <th class="font-demi">Author</th>
                                <th class="font-demi">Total module</th>
                                <th class="font-demi"></th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>
@endsection

@push('js')
  @include('learning.learningreport.scripts.index_script')
@endpush

And index_script.blade.php

<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.7/js/dataTables.select.min.js"></script>
<script>
    $(document).ready(function() {
        $('li[class="nav-item"] a').click(function(e) {
            e.preventDefault();
            $('a').removeClass('active');
            $(this).addClass('active');
        });
    });
</script>

<script>
    var learnerreport;

    $(document).ready(function() {
        learnerreport = $('#learnerReport').DataTable({
            dom: 'rtpi',
            pageLength:  {{ $limit ?? '10' }},
            language: {
                paginate: {
                    previous: '<i class="fa fa-angle-left"></i>', // or '←' 
                    next: '<i class="fa fa-angle-right"></i>', // or '→'
                },
            
            },
            processing: true,
            drawCallback : function( settings ) {
                $('#lengthInput option[value={{ $limit ?? '10' }}]').attr('selected','selected');
            },
            serverSide: true,
            stateSave: false,
            ajax: {
                url: "{{ route('lms.learningreport.showlearner', ['pfx'=>$pfx]) }}",
                dataType: "json",
                type: "POST",
                data: function ( d ) {
                    d._token = "{{ csrf_token() }}";
                },
            },
            columns: [
                { 
                    data : "avatar",
                    orderable : false,
                    className : "text-center",
                },
                { data: "learner_name"},
                { data: "dept_name"},
                { data: "job_title"},
                { data : "course_num"},
                { 
                    data : "action",
                    orderable : false,
                    className : "text-center",
                }
            ],
        });

        // Data length
        $('#learnerDataLength').on('change', function () {
            learnerreport.page.len( $(this).val() ).draw();
        });


        // Search
        $('#searchLearner').on('keyup', function () {
            learnerreport.search( this.value ).draw();
        });
     });
</script>

<script>
    var coursereport;

    $(document).ready(function() {

        coursereport = $('#coursereport').DataTable({
            dom: 'rtpi',
            pageLength:  {{ $limit ?? '10' }},
            language: {
                paginate: {
                    previous: '<i class="fa fa-angle-left"></i>', // or '←' 
                    next: '<i class="fa fa-angle-right"></i>', // or '→'
                },
            
            },
            processing: true,
            drawCallback : function( settings ) {
                $('#lengthInput option[value={{ $limit ?? '10' }}]').attr('selected','selected');
            },
            serverSide: true,
            stateSave: false,
            ajax: {
                url: "{{ route('lms.learningreport.showcourse', ['pfx'=>$pfx]) }}",
                dataType: "json",
                type: "POST",
                data: function ( d ) {
                    d._token = "{{csrf_token()}}";
                },
            },
            columns: [
                { data: "title"},
                { data: "provider"},
                { data: "total_module"},
                { data : "action",
                    orderable : false,
                    className : "text-center",
                }
            ],
        });

        // Data length
        $('#courseDataLength').on('change', function () {
            coursereport.page.len( $(this).val() ).draw();
        });


        // Search
        $('#searchCourse').on('keyup', function () {
            coursereport.search( this.value ).draw();
        });
    });
</script>

please help, how can I display data that is almost the same as the search keyword? thank you

Source: Laravel

Leave a Reply