|
 |
|
การดึงข้อมูลมาแสดงตามเงื่อนไขของ checkbox โดยใช้หน้าเดิม แสดง |
|
 |
|
|
 |
 |
|
แนะนำ
Ajax DataTable
กำหนดชื่อ Radio ให้เหมือนกันและกำหนดค่าต่างกัน
Code (PHP)
<input type="radio" name="doc_type" value="1">ดูรายงานยืมอุปกรณ์
<input type="radio" name="doc_type" value="2">ดูรายงานคืนอุปกรณ
Code (JavaScript)
$("#dataTable").dataTable({
"ajax": {
"url": URL,
"type": 'POST',
"data": function(d) {
return {
doc_type: $("input[name='doc_type']:checked").val()
};
}
}
});
ที่เหลือก็ไปทำ Backend ให้ response กลับมาที่ Frontend->View
Code (PHP)
$data = Query Result ตามเงื่อนไข
foreach ($data as $row) {
$sub_array = array();
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxxx
$sub_array = xxx
$result[] = $sub_array;
}
echo json_encode(array('data' => $result), JSON_UNESCAPED_UNICODE);
|
 |
 |
 |
 |
Date :
2024-04-03 15:34:34 |
By :
Guest |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
datatable เหมาะกับข้อมูลไม่เกิน 2mb (spec 16mb) มากกว่านั้น ก็จะอื่ดๆ ขึ้นอยู่กับ เครื่อง client
และเครื่อง server รับโหลด ได้มากน้อยขนาดไหน
แนะนำ ไม่ควรทำเป็น ajax ส่งลิงค์ ไปคิวรี่ใหม่ดีกว่า โปรแกรมเดิมก็มีอยู่แล้ว
|
 |
 |
 |
 |
Date :
2024-04-03 15:55:31 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าดึงข้อมูลใหม่โดยอยู่หน้าเดิม URL เดิมก็ใช้ AJAX แหละเหมาะสุด
แต่มันก็แตกออกไปได้อีกหลายวิธี อย่างเช่นใช้ DataTable.js (+AJAX), AJAX กับ ข้อมูลที่ต้องเอามาประมวลผล-แสดงผลอีกที เช่น JSON, AJAX กับ HTML แบบเดิมๆแล้วใช้การ replace content เอาเลย
ถ้าแบบสุดท้ายก็ไม่ต้องแก้เยอะ คล้ายๆ คห.2 เพียงแต่ต้องดักตอน click check box แล้วตอนส่ง URL pagination ก็ต้องสร้าง URL ใหม่ตาม check box เพื่อให้โหลดหน้าอื่นได้ถูกต้อง.
|
 |
 |
 |
 |
Date :
2024-04-04 09:51:58 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : Guest เมื่อวันที่ 2024-04-04 09:42:42
รายละเอียดของการตอบ ::
และ feature ของ datatable มีไว้ประดับเท่านั้นเหรอ ทั้งการค้นหา ทั้งการจัดเรียง มันทำเฉพาะข้อมูลที่อยู่บน client เท่านั้น
ถ้าโหลดเฉพาะหน้าที่แสดง ก็ใช้แบบเดิมนั้่นแหล่ะ
เวลาค้นหาก็เป็นการค้นหาทั้ง table ใช้ feature ของ database server เป็นตัวจัดการ
มีตัวอย่าง datatable การแสดง webboard ให้ใช้ แบ่งเป็น โหลดครั้งละไม่เกิน 3000 เรคคอร์ด
มีทั้ง การ reload โดยไม่ต้องเปลี่ยนหน้า
Code (JavaScript)
dt = $('#tb').DataTable({ //sDom:'tipr',
oLanguage: {sSearch: 'Search :   '},
order:[[3, 'desc']], bAutoWidth: false, pageLength:25, lengthChange: false, searching: true,
ajax: { url: `/Forum/GetTopics/1/`, dataType: "json", contentType: "application/json; charset=utf-8"
, dataSrc: function (js){
if(js.msg==='complete'){
console.log(js);
let p = Math.ceil(js.data.c / 3000);
count_topic=js.data.c;
if( c_page<2){
$('#sw-inc').addClass('d-none').removeClass('d-inline-block'); }else{$('#sw-inc').addClass('d-inline-block').removeClass('d-none'); }
if ( p > c_page){
c_page=p;
let ls = (p - 1) * 3000, st=ls+1, en=js.c;
$('#st').last().text(`${ls -2999} - ${ls}`) ;
$('#st').append(`<option value="${p}" >${st} - ${en}</option>`);
}
return js.data.rows;
}
}
},
columnDefs: [
{targets:0, width: 200, searchable: false, orderable: true, className: 'text-center'}
, {targets:2, searchable: true, orderable: true, className: 'text-left',render: (data)=>{
return `<span>${data}</span>${pen}`;
}}
, {targets:[3,4], width: 120, orderable: true, searchable: false, className: 'text-center'}
, {targets:[1,5,6], width: 70, orderable: true, searchable: false, className: 'text-center'}
],
createdRow: function (row, data) { $(row).attr('x-id',data.id).addClass('tr-topic'); },
columns: [
{mData: function(ro){return ro.name;}},
{mData: function(ro){return ro.id;}},
{mData: function(ro){return ro.topic;}},
{mData: function(ro){return ro.write_date.substr(2,17);}},
{mData: function(ro){return ro.answerdate>''? ro.answerdate.substr(2,17):'-';}},
{mData: function(ro){return ro.count_answer;}},
{mData: function(ro){return ro.count_view;}}
],
drawCallback: function() { page_css();
if( !$('#tb_filter').hasClass('start_date')){
$('#tb_filter').addClass('start_date').append( dt_inc);
}
$('#st').change(function(){
dt.ajax.url(`/Forum/GetTopics/${ForumID}/${$('#st').val()}/`);
dt.ajax.reload();
});
}
});
|
ประวัติการแก้ไข 2024-04-04 10:26:09
 |
 |
 |
 |
Date :
2024-04-04 10:17:37 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|