因此,我对我的表进行了更新,以便用户可以通过为所选内容添加下拉菜单来搜索按日期(年份)筛选的数据。我试图使用ajax来过滤日期,但不知怎么的,我无法让它正常工作。我能知道我的代码出了什么问题吗?还是有其他方法按日期过滤我的桌子?
表视图刀片文件
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Pekerjaan</h3>
<br>
<div class="card-tools">
<a href="/tambahpekerjaan" type="button" class="btn btn-outline-success btn-block"><i class="fa fa-plus"></i>  Tambah Pekerjaan</a>
</div>
<div class="col-md-2">
<select id="filter-tahun" class="form-control filter">
<option value="">Pilih Tahun</option>
@foreach ($pekerjaan as $pekerjaans)
<option value="{{$pekerjaans->id}}">{{$pekerjaans->tanggal}}</option>
@endforeach
</select>
</div>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Paket Pekerjaan</th>
<th>Tanggal</th>
<th>Nama Perusahaan</th>
<th>Lokasi Pekerjaan</th>
<th>PPK</th>
<th>HPS</th>
<th>Gambar</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
@php $no = 1; @endphp
@foreach ($pekerjaan as $pekerjaans)
<tr>
<td>{{$no++}}</td>
<td>{{$pekerjaans->pekerjaan}}</td>
<td>{{$pekerjaans->tanggal}}</td>
<td>{{$pekerjaans->penyedia->nama}}</td>
<td>{{$pekerjaans->lokasi}}</td>
<td>{{$pekerjaans->user->name}}</td>
<td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
<td>
<img src="{{asset('gambarpekerjaan/'.$pekerjaans->gambar)}}" style="width: 100px"alt="">
</td>
<td>
@if (Auth::user()->status=='super')
<a href="/editpekerjaan/{{$pekerjaans->id}}" type="button" class="btn btn-outline-primary">Edit</a>
<a href="#" type="button" class="btn btn-outline-danger delete" data-id="{{$pekerjaans->id}}" data-nama="{{$pekerjaans->pekerjaan}}">Hapus</a>
@else
<a href="/editpekerjaan/{{$pekerjaans->id}}" type="button" class="btn btn-outline-primary btn-block">Edit</a>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>这里是脚本
<script>
$(function () {
// let pekerjaan = $("#filter-tahun").val();
/*const table =*/ $('#tabelpekerjaan').DataTable({
"paging": true,
"lengthChange": true,
"searching": true,
"ordering": true,
"info": true,
"autoWidth": true,
"responsive": true,
// "ajax":{
// url: "{{url('')}}/datapekerjaan",
// type:"POST",
// data:function(d){
// d.pekerjaan = pekerjaan;
// return d
// }
// }
});
//Initialize Select2 Elements
$('.select2bs4').select2({
theme: 'bootstrap4'
})
});
@if (session()->has('message'))
toastr.success("{{session()->get('message')}}")
@endif
$('.delete').click(function(){
var idpekerjaan = $(this).attr('data-id');
var namapekerjaan = $(this).attr('data-nama');
Swal.fire({
title: 'Apakah Anda yakin?',
text: "Paket Pekerjaan "+namapekerjaan+" akan dihapus!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Ya, yakin!'
}).then((result) => {
if (result.isConfirmed) {
window.location = "/deletepekerjaan/"+idpekerjaan+""
Swal.fire(
'Dihapus!',
'Data berhasil dihapus.',
'success'
)
}
});
});
// $(".filter").on('change',function(){
// pekerjaan = $("#filter-tahun").val()
// table.ajax.reload(null,false)
// })
</script>我评论了ajax的部分,因为它根本不工作,我的桌子变得一团糟.
我能知道如何解决这个问题吗?提前感谢
发布于 2022-07-14 06:13:40
查一下这个
$(document).ready(function () { // Setup )-向每个页脚单元格$(‘#示例头tr') .clone(真) .addClass('filters') .appendTo(’#示例头‘)添加一个文本输入;
var table =$(‘#样例’).DataTable({ orderCellsTop: true,fixedHeader: true,initComplete: function () { var api = this.api();//对于每一列api .columns() .eq(0) .each (colIdx) { //设置标头单元格包含输入元素var cell = $('.filters th').eq( $(api.column(colIdx).header()).index( );var title = $(cell).text();$(cell).html('');//在此输入中的每个键按下$('.filters‘、$(’.filters.off‘) .off('keyup change') .on('change’)函数(e) { //获取搜索值$(this).attr('title',$(this).val());var regexr = '({search})';//$(this).parents('th').find('select').val();//在列中搜索该值api .column(colIdx) .search( this.value != '‘?)regexr.replace('{search}',‘(’+ this.value +‘))’):'',this.value != '',this.value == '‘) .draw();} .on('keyup',函数(e) { e.stopPropagation();$(this).trigger('change');$(this) .focus() .setSelectionRange(cursorPosition,cursorPosition;});};
请参阅此https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html
https://stackoverflow.com/questions/72975874
复制相似问题