รบกวนสอบถามด้วยค่ะ คือdata table export excel print pdf ไม่ขึ้นรูปภาพค่ะ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา รบกวนสอบถามด้วยค่ะ คือdata table export excel print pdf ไม่ขึ้นรูปภาพค่ะ

รบกวนสอบถามด้วยค่ะ คือdata table export excel print pdf ไม่ขึ้นรูปภาพค่ะ
Narita Nan Kaewprasert 05-05-2019 20:16:53

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

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


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


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

 ความคิดเห็นที่ 1
น่าจะต้องแปลงรูปที่อยู่ในรูปแบบ url หรือไฟล์เป็น DataURI และก็ตอน export เพิ่ม option  ให้รองรับ HTML แนวทางประมาณนั้น

1
2
3
4
exportOptions:{
    columns:[0,1,2,3,4,5],
    stripHtml: false
},


ninenik 06-05-2019
 ความคิดเห็นที่ 2
ดูโค้ดน้เป็นแนวทาง

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
</head>
 
<body>
 
<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th width="200">Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
<?php for($i=1;$i<=20;$i++){?>       
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td><img id="img_<?=$i?>" src="ninenik_page_logo.png" style="width:60px;height:auto;"/>
                </td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
  <?php } ?>
        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
 
<script type="text/javascript">
$(function(){
 
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'pdfHtml5',
                pageSize:'A4',
                orientation:'landscape',
                action: function ( e, dt, node, config ) {
                    dt.page.len(-1).draw(); // เมื่อกดปุ่มส่งออก pdf ให้สลับไปแสดงข้อมูลทั้งหมด
                    $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, node, config);        
                    dt.on( 'draw.dt', function () {
                        console.log('drwa complate');
                    });
                },             
                exportOptions:{
                    columns:[0,1,2,3], // คอลัมส์ที่จะส่งออก
                    modifier : {
                        page:'all' // หน้าที่จะส่งออก all / current
                    },
                    stripHtml: false
                },
                customize: function ( doc ) {  
                    doc.content[1].table.widths = ['25%','25%','25%','auto'];  
                    doc.content[1].table.headerRows = 1;
                    doc.content[1].table.dontBreakRows = true;
                    doc.content[1].table.keepWithHeaderRows = 1;
 
                    var rowCount = doc.content[1].table.body.length;
                    for(i = 1; i < rowCount; i++){
                        doc.content[1].table.body[i][0] = {
                            alignment:'center',
                            text:'test'
                        };
                        doc.content[1].table.body[i][1] = {
                            alignment:'center',
                            text:'test'
                        };
                        doc.content[1].table.body[i][2] = {
                            alignment:'center',
                            text:'test'
                        };
                        doc.content[1].table.body[i][3] = {
                            alignment:'center',
                            text:''
                        };                     
                        var imgData = "";
                        if($("#img_"+i)[0]!=undefined){
                            imgData = getDataURL($("#img_"+i)[0]);         
                            if(imgData!=""){
                                doc.content[1].table.body[i][3] = {
                                    alignment: 'center',
                                    image: imgData,
                                    width: 60,
                                    height: 58                         
                                }                      
                            }
                        }
                    }
                     
                }
            }
        ],
        "drawCallback": function( settings ) {
            var api = new $.fn.dataTable.Api( settings );
             // ถ้าแสดงแบบทั้งหมดแล้ว ให้เปลี่ยนกลับมาแสดงแบบปกติ หน้าละ 10 รายการ หลังจากส่งออกแล้ว
            if(api.page.len()==-1){
                setTimeout(function(){
                    api.page.len(10).draw();
                },1000);
            }
        }
    } );
 
});
// ฟังก์ชั่นสำหรับสร้าง DataURL จากรูปภาพ
function getDataURL(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.naturalWidth;
    canvas.height = img.naturalHeight;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataURL = canvas.toDataURL("image/png");
    return dataURL;
 // return dataURL.replace(/^data:image/(png|jpg);base64,/, "");
}
</script>
</body>
</html>


ตัวอย่าง DEMO

ninenik 06-05-2019






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