สอบถามปัญหาการใช้ DataTable แบบ Server-side processing ต้องการให้ข้อมูลบางคอลัมน์อยู่ในช่องเดียวกัน

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามปัญหาการใช้ DataTable แบบ Server-side processing ต้องการให้ข้อมูลบางคอลัมน์อยู่ในช่องเดียวกัน

สอบถามปัญหาการใช้ DataTable แบบ Server-side processing ต้องการให้ข้อมูลบางคอลัมน์อยู่ในช่องเดียวกัน
ตาราง po


ตาราง po_detail


คือลอง join ตารางแล้ว มันขึ้น No data found in the server

ตอนนี้


ข้อมูลมาจากตาราง po เลขที่96 ซึ่งมีไฟล์เกี่ยวกับpoนี้ 2ไฟล์ด้วยกัน ซึ่งจัดเก็บในตาราง po_detail ที่อยากให้เป็นคือ ตามรูปค่ะ


โค้ด
<script type="text/javascript" language="javascript" >
			$(document).ready(function() {
				var dataTable = $('#employee-grid').DataTable( {
					"processing": true,
					"serverSide": true,
					"Paginate": true, 
					"LengthChange": false,
					"Filter": false,
					"Info": false,
                    "ajax":{
						url :"employee-grid-data.php", // json datasource
						type: "post",  // method  , by default get
						error: function(){  // error handling
							$(".employee-grid-error").html("");
							$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
							$("#employee-grid_processing").css("display","none");
							
						}
					},
					"fnDrawCallback": function () {
					  //alert('test');
						$(".example1").fancybox({
						'maxWidth'	: 800,
						'maxHeight'	: 600,
						'fitToView'	: false,
						'width'		: '100%',
						'height'		: '100%',
						'autoSize'	: false,
						'closeClick'	: false,
						'openEffect'	: 'none',
						'closeEffect'	: 'none',
						'type'			: 'iframe'
								});
								$(".example2").attr("rel","gallery").fancybox({
								
							/*'width'			: '50%',
							'height'		: '50%',
							'autoScale'		: false,
							'transitionIn'		: 'none',
							'transitionOut'		: 'none',
							'type'			: 'iframe',*/
	////////print//////////
	'afterShow' : function(){
    var win=null;
    var content = $('.fancybox-inner');
    $('.fancybox-wrap')
    // append print button
    .append('<div id="fancy_print"></div>')
    // use .on() in its delegated form to bind a click to the print button event for future elements
    .on("click", "#fancy_print", function(){
      win = window.open("width=200,height=200");
      self.focus();
      win.document.open();
      win.document.write('<' 'html' '><' 'head' '><' 'style' '>');
      win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
      win.document.write('<' '/' 'style' '><' '/' 'head' '><' 'body' '>');
      win.document.write(content.html());
      win.document.write('<' '/' 'body' '><' '/' 'html' '>');
      win.document.close();
      win.print();
      win.close();
    }); // on
  } //  afterShow
						});
					
					}
				} );
			} );
		</script>

<div id="templatemo_content">
			<table id="employee-grid"  cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
					<thead>
						<tr>
			<th width="5%">ประเภท</th>
			<th width="5%">Com</th>
			<th width="10%">วันที่ PO</th>
			<th width="10%">PO. No.</th>
			<th width="10%">QT. No.</th>
			<th width="25%">บริษัท</th>
			<th>ไฟล์ </th>
			<th width="10%">ผู้รับผิดชอบ</th>
			<th width="5%">แก้ไข </th>
						</tr>
					</thead>
			</table>
		</div>





อีกหน้า
<?php 
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "4321";
$dbname = "salecentral";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */


// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$columns = array( 
// datatable column index  => database column name
	0 =>'ptype',
	1 =>'date',
	2 =>'name_partner',
	3 =>'po_no', 
	4 => 'qt_no',
	5 => 'com',
	6 => 'file',
	7 => 'Ename'
);

// getting total number records without any search
$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT po.id As idPO,date, name_partner, po_no, qt_no, com,Ename,Enname,file,ptype ";
$sql.=" FROM po,partner,employee,po_detail where po.id_partner=partner.id_partner AND po.id_author=employee.Eid AND po.id=po_detail.id_po AND 1=1";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
	$sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";    
	$sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR file LIKE '%".$requestData['search']['value']."%' ";
	$sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
	
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
	$idPO= $row["idPO"];
	$nestedData=array();
	if($row['ptype']=='po') { $nestedData[] = "PO";} 
	if($row['ptype']=='ใบเคลม') { $nestedData[] = "ใบเคลม";} 
	
	if($row['com']=='CC') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/central.png' height='40px'/>";} 
			if($row['com']=='CL') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/clwheel.png' height='40px'/>";}
			if($row['com']=='DS') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/dreamshop.png' height='40px'/>";}

	list($Year,$month,$day) = explode("-",$row['date']);
    $nestedData[] = (int)$day."/".(int)$month."/".($Year 543);
	
	$nestedData[] = $row["po_no"];
	if($row['qt_no']!= "ประวัติลูกค้า" && $row['qt_no']!= "ข้อมูลSale"){
	if (strpos($row['qt_no'],'Rev'))
	{ $nestedData[] = "<a class='example1' href='viewEdit$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
	else{ $nestedData[] = "<a class='example1' href='view$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
	} else { $nestedData[] = $row['qt_no'];}
	$nestedData[] = $row["name_partner"];
	
	list($name,$type) = explode(".",$row['file']);
     if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$row[file]'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
	 else {	 $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$row[file]'><img src='images/po/$row[file]' width='60px'/></a>";}
	$nestedData[] = $row['Ename']." (".$row['Enname'].")"; 
	$nestedData[] = "<a class='example1' href='checkEdit.php?idx=$idPO&po=po' target='_self'><img src='../images/edit.png' width='30' title='แก้ไข' border='0' ></a>";
	$data[] = $nestedData;
}



$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format

?>



Saranya2532 125.24.80.xxx 15-11-2016 19:20:38

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ลองดูคำสั่งนี้เป็นแนวทาง

SELECT a . * , b . * , GROUP_CONCAT( b.file ) AS all_file
FROM po a
LEFT JOIN po_detail b ON a.id = b.id_po
WHERE b.id_po IS NOT NULL 
GROUP BY a.id


จะได้ฟิลด์ all_file เก็บชื่อไฟล์ เป็น string ต่อกันเช่น aaa.jpg,bbb.jpb
เวลานำไปใช้ก็เอาค่า string ที่ไดเป็นแตกเป็น array ด้วยคำสั่ง explode
แล้วแยกแสดงข้อมูลในตาราง


ninenik 1.46.194.xxx 15-11-2016
 ความคิดเห็นที่ 2
$array = explode(',', $row["all_file"]);
	foreach(array_unique($array ) as $val){
	list($name,$type) = explode(".",$val);
     if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$val'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
	 else {	 $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$val'><img src='images/po/$val' width='60px'/></a>";}
	
	}

คือ อยากให้ไฟล์อยู่ตรงช่องค่ะ โดยขึ้นบรรทัดใหม่เรียงลงมา แต่อันนี้เหมือนมันขยับออกไปด้านข้างค่ะ


saranya2532 101.108.128.xxx 17-11-2016 13:45
 ความคิดเห็นที่ 3
  อันนี้อยู่ที่การจัดการแสดงผล คงต้องลองเอง ต้องค่อยๆ ไล่ดู แล้วปรับจนกว่าจะได้


ninenik 1.46.229.xxx 17-11-2016
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ