Membuat fitur cetak laporan berdasarkan filter data.

Sebelumnya saya sudah bisa menampilkan laporan berikut kolom untuk filter datanya. Saat ini, saya ingin membuat fitur cetak laporan berdasarkan filter data tersebut namun terkendala button Print tidak terpanggil, serta agak bingung untuk membuat view dan modelnya. Fyi: saya menggunakan CI 3 dan dibantu javascript. Berikut saya lampirkan kode tcetak laporan yang sudah saya coba.

1.  View pada halaman laporan sebelum mencetak

<div class="page-wrapper">

    <div class="page-breadcrumb">

        <div class="row align-items-center">

            <div class="col-6">

                <nav aria-label="breadcrumb">

                    <ol class="breadcrumb mb-0 d-flex align-items-center">

                        <li class="breadcrumb-item"><a href="index.html" class="link"><i class="mdi mdi-home-outline fs-4"></i></a></li>

                        <li class="breadcrumb-item active" aria-current="page">Data Scrap</li>

                    </ol>

                </nav>

                <h1 class="mb-0 fw-bold">Scrap Product Monitoring</h1>

                <div class="d-flex mt-3">

                    <div class="badge bg-primary me-3 fs-5" id="current-date">Minggu 1 - Tanggal

                        <?= $shift_date[0]['tanggal'] ?></div>

                    <div class="badge bg-primary px-5 fs-5" id="current-channel">IR</div>

                </div>

            </div>

        </div>

    </div>

    <div class="container-fluid">

        <div class="row">

            <div class="col-4">

                <h5>Filter IPD</h5>

                <button class="btn btn-md btn-info text-light button-ipd px-5 active-ipd" data-ipd="1">IR</button>

                <button class="btn btn-md btn-outline-info button-ipd px-5" data-ipd="2">OR</button>

                <button class="btn btn-md btn-outline-info button-ipd px-5" data-ipd="3">BC</button>

            </div>

            <div class="col-4">

                <h5>Filter Tanggal</h5>

                <select class="js-example-basic-single form-control" name="date" id="select-date">

                    <?php foreach ($shift_date as $key => $val) : ?>

                        <option value="<?= $val['tanggal'] ?>">

                            Minggu <?= $val['minggu_ke'] ?> - Tanggal <?= $val['tanggal'] ?></option>

                    <?php endforeach; ?>

                </select>

            </div>

            <div class="col-4">

                <h5></h5>

                <button class="btn btn-md btn-info text-light px-5" href="<?php echo base_url('ScrapProductMonitoring/print') ?>">Print</button>

            </div>

            <div class="col-12">

                <div class="card">

                    <div class="table-responsive">

                        <table class="table table-bordered table-responsive-lg">

                            <thead>

                                <tr>

                                    <td>CH</td>

                                    <td>Output</td>

                                    <td>Shift 1</td>

                                    <td>Shift 2</td>

                                    <td>Shift 3</td>

                                    <td id="current-td-total">Total IR</td>

                                    <td id="current-td-percent">%IR</td>

                                </tr>

                            </thead>

                            <tbody id="table-body">

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>

        </div>

    </div>

2. Controller : ScrapProductMonitoring

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class ScrapProductMonitoring extends CI_Controller

{

    public function __construct()

    {

        parent::__construct();

        $this->load->model('Model_Scrap', 'Scrap');

        $this->load->model('Model_Channel', 'Channel');

        $this->load->model('Model_Shift', 'Shift');

        $this->load->model('Model_Product', 'Product');

    }

    public function index()

    {

        $data['shift_date'] = $this->Shift->get_shift_date();

        $data['shift_date_json'] = json_encode($data['shift_date']);

        $this->load->view('components/header');

        $this->load->view('dashboard/scrap-product-monitoring/index', $data);

        $this->load->view('components/footer');

        $this->load->view('dashboard/scrap-product-monitoring/js');

    }

    public function json_scrap_monitoring_product()

    {

        $ipd = $this->input->get('ipd');

        $date = $this->input->get('date');

        $data = $this->Product->scrap_monitoring_product($date, $ipd);

        echo json_encode($data);

    }

    public function print()

    {

        $data['shift_date'] = $this->Shift->get_shift_date();

        $data['shift_date_json'] = json_encode($data['shift_date']);

        $this->load->view('components/header');

        $this->load->view('dashboard/laporan-scrap-product-monitoring-details/index', $data);

        $this->load->view('components/footer');

        $this->load->view('dashboard/laporan-scrap-product-monitoring-details/js');

    }

}

3. Model

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Model_Scrap extends CI_Model

{

    public function get_all_scrap()

    {

        return $this->db->get('scrap')->result_array();

    }

    public function insert_scrap_qty($data)

    {

        $checkScrap = $this->db->get_where('scrap_qty', ['ipd_id' => $data['ipd_id'], 'channel_id' => $data['channel_id'], 'scrap_id' => $data['scrap_id'], 'date_week_id' => $data['date_week_id']])->row_array();

        if ($checkScrap) {

            $this->db->where('id', $checkScrap['id']);

            $this->db->update('scrap_qty', $data);

        } else {

            $this->db->insert('scrap_qty', $data);

        }

        if ($this->db->affected_rows() > 0) {

            return ['status' => 200, 'action' => true, 'msg' => 'Scrap Quantity berhasil tersimpan!'];

        } else {

            return ['status' => 500, 'action' => false, 'msg' => 'Scrap Quantity gagal tersimpan!'];

        }

    }

    public function get_scrap_qty($date)

    {

        $this->db->select("scrap_qty.*, scrap.*, ipd.*, date_week.*");

        $this->db->from('scrap_qty');

        $this->db->join('scrap', 'scrap.id = scrap_qty.scrap_id');

        $this->db->join('ipd', 'ipd.id = scrap_qty.ipd_id');

        $this->db->join('date_week', 'date_week.id = scrap_qty.date_week_id');

        // $this->db->where('date_week.tanggal BETWEEN "'.$from.'" AND "'.$until. '"', NULL, FALSE);

        $this->db->where('date_week.tanggal', $date);

        $this->db->order_by('channel_id');

        $this->db->order_by('ipd_id');

        $this->db->order_by('date_week.tanggal');

        $this->db->order_by('scrap.id');

        return $this->db->get()->result_array();

    }

    public function get_scrap_qty_channel($date, $channel, $ipd)

    {

        $this->db->select("scrap_qty.*, scrap_qty.id as sqId, scrap.*, ipd.*, date_week.*");

        $this->db->from('scrap_qty');

        $this->db->join('scrap', 'scrap.id = scrap_qty.scrap_id');

        $this->db->join('ipd', 'ipd.id = scrap_qty.ipd_id');

        $this->db->join('date_week', 'date_week.id = scrap_qty.date_week_id');

        $this->db->where('date_week.tanggal', $date);

        $this->db->where('scrap_qty.channel_id', $channel);

        $this->db->where('scrap_qty.ipd_id', $ipd);

        $this->db->order_by('channel_id');

        $this->db->order_by('ipd_id');

        $this->db->order_by('date_week.tanggal');

        $this->db->order_by('scrap.id');

        return $this->db->get()->result_array();

    }

    public function update_status($date, $channel, $ipd)

    {

        $this->db->where(['date_week_id' => $date, 'channel_id' => $channel, 'ipd_id' => $ipd]);

        $this->db->update('scrap_qty', ['status' => '1']);

        if ($this->db->affected_rows() > 0) {

            return true;

        } else {

            return false;

        }

    }

    public function edit_scrap_quantity($id, $data)

    {

        $this->db->where('id', $id);

        $this->db->update('scrap_qty', $data);

        if ($this->db->affected_rows() > 0) {

            return true;

        } else {

            return false;

        }

    }

    public function delete_scrap_quantity($id)

    {

        $this->db->where('id', $id);

        $this->db->delete('scrap_qty');

        if ($this->db->affected_rows() > 0) {

            return true;

        } else {

            return false;

        }

    }

}

4. View laporan-scrap-monitoring-details/index

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width-device-width, initial-scale-1.0">

</head>

<body>

    <table class="table table-bordered">

        <thead>

            <tr>

                <td>CH</td>

                <td>Output</td>

                <td>Shift 1</td>

                <td>Shift 2</td>

                <td>Shift 3</td>

                <td>Total</td>

                <td>%</td>

            </tr>

        </thead>

        <tbody id="table-body">

        </tbody>

    </table>

    <script type="text/javascript">

        window.print();

    </script>

</body>

</html>

5. View laporan-scrap-monitoring-details/js

<script>

    $(document).ready(() => {

        const currentChannel = $("#current-channel");

        const currentDate = $("#current-date");

        const currentTdPercent = $("#current-td-percent");

        const currentTdTotal = $("#current-td-total");

        const selectDate = $("#select-date");

        const dateFirstData = <?= $shift_date_json ?>;

        const loadData = (ipd = 1, date = dateFirstData[0].tanggal) => {

            $.ajax({

                type: 'ajax',

                url: `<?= base_url('ScrapProductMonitoring/json_scrap_monitoring_product?') ?>ipd=${ipd}&date=${date}`,

                async: true,

                dataType: 'json',

                success: (data) => {

                    let html = "";

                    data.forEach((content, index) => {

                        const parse_qty_shift_1 = parseInt(content.qty_shift_1);

                        const parse_qty_shift_2 = parseInt(content.qty_shift_2);

                        const parse_qty_shift_3 = parseInt(content.qty_shift_3);

                        const total = parse_qty_shift_1 + parse_qty_shift_2 +

                            parse_qty_shift_3;

                        html += `

                        <tr class="table-row">

                            <td>${content.channel_id}</td>

                            <td>${content.output}</td>

                            <td>${content.qty_shift_1}</td>

                            <td>${content.qty_shift_2}</td>

                            <td>${content.qty_shift_3}</td>

                            <td>${total}</td>

                            <td>${total/content.output}</td>

                        </tr>

                    `;

                    })

                    const tableBody = $("#table-body");

                    const tableRow = $(".table-row");

                    tableRow.remove();

                    tableBody.append(html)

                },

                error: (error) => {

                    console.log(error);

                }

            })

        }

        loadData();

    })

</script>

avatar affifahnasrillah
@affifahnasrillah

1 Kontribusi 0 Poin

Diperbarui 2 tahun yang lalu

1 Jawaban:

<div>maaf saya kurang ngerti baca codingnya, tapi pengalaman saya- kalo saya sendiri bikin laporan itu setiap didalam tag &lt;tr&gt;*disini&lt;/tr&gt; dikasih input type hidden yang "id"nya itu contohnya kayak ini (misal di baris 1 itu id_number datanya di database adalah "2")</div><pre>&lt;tr&gt; &lt;td&gt;&lt;input type="hidden" id="checkbox&lt;?php echo $id_data; ?&gt;" value="&lt;?php echo $id_data; ?&gt;" /&gt;&lt;/td&gt; //jadinya input id="checkbox2" value="2" &lt;td&gt;*data yang id nya sama, misal nama, usia dll&lt;/td&gt; // yang idnya 2 ... &lt;tr&gt;</pre><div><br>trus buat javascriptnya (saya pakai jquery):</div><pre>$('#print_yang_dipilih').click( function() { //kalo tekan tombol print var cb = []; //bikin array "cb" $('[id^=cb]:checked').each( function(){ //untuk semua yang di check tadi cb.push(this.value); // masukkan value dari checkbox yang checkednya true ke array

// nah &quot;cb&quot; ini yang dikirim ke page dalam bentuk string dengan pemisah &quot;,&quot; yang buat ngeprint lalu di page itu di explode biar jadi &quot;array&quot; lagi nanti foreach semua arraynya, masukin ke tr&amp;gt;td&amp;gt;

}); });</pre><div><br>singkatnya:</div><pre>1. waktu klik print 2. value checkboxnya yang :checked dijadikan array 3. kirim data arraynya ke halaman yang buat ngeprint (biasanya auto dalam bentuk string) 4. explode datanya jadi array lagi 5. foreach arraynya trus load 1 per satu dari database 6. disini udh bisa buat ngeprint karena datanya udah sesuai sama yang dipilih tadi</pre><div><br>kalo aku sih kayak gitu caranya, semoga bisa membuka inspirasi</div>

avatar muhammadridho30
@muhammadridho30

9 Kontribusi 2 Poin

Dipost 2 tahun yang lalu

Login untuk ikut Jawaban