简体   繁体   English

Javascript打印功能

[英]Javascript print function

I need help trying to print certain fields from the table using javascript. 我需要帮助,尝试使用javascript从表中打印某些字段。 Mainly each ticket number that is generated with the name and phone number of the person and another function to just print the address of the person that purchased the tickets. 主要是用该人的姓名和电话号码生成的每个票号,以及另一个仅打印购买票的人地址的功能。 I have been racking my brain for a couple of days and think I just need some fresh eyes to help me out. 我已经绞尽脑汁了几天,认为我只需要一些新鲜的眼睛即可帮助我。 Here s the code I have been working with, I will admit that I am somewhat of a newb! 这是我一直在使用的代码,我承认我有点新手! i just a little help. 我只是一点帮助。 Thanks. 谢谢。

    <div class="span16">
    <div style="float:right;">
        <a href="/admin/orders/multiprint/" class="btn btn-large btn-multiprint" id="multiprint1">Printer 3 Checked</a>
        <!--<a href="/admin/orders/multiprint/" class="btn btn-large btn-multiprint" id="multiprint2">Printer 2 Checked</a>-->
    </div>
<h1>Orders</h1>
    <hr/>
        <?php echo $this->element('pager'); ?>
            <table class="table table-striped table-bordered table-condensed">
                <thead>
                    <tr>
                        <th>Order</th>
                        <th>Raffle</th>
                        <!--<th>Transaction</th>-->
                        <th style="white-space:nowrap;">Tickets</th>
                        <th>Total</th>
                        <th>Date</th>
                        <th>Status</th>
                        <th>Name</th>
                        <th style="white-space:nowrap;width:200px;">Address</th>
                        <th style="white-space:nowrap;width:100px;">Print</th>
                        <th style="white-space:wrap;width:100px;">Address Label</th>
                    </tr>
                </thead>
                <tbody>
            <?php
                $i = 0;
                $t = 0;
                    foreach ($orders as $order):

                        $rowclass = 'orders';
                        if ($i++ % 2 == 0) {
                            $rowclass .= ' orders-even';
                        } else {
                            $rowclass .= ' orders-odd';
                        }

                        $id = $order['Order']['id'];
                        //$transaction_number = $order['Order']['transaction_number'];
                        $quantity = $order['Order']['quantity'];
                        $total = '$'.number_format($order['Order']['total'], 2, '.', ',');
                        $date = '<div class="date">'.date('m/d/y', strtotime($order['Order']['modified'])).'&nbsp;</div>';
                        $date .= '<div class="time">'.date('h:i a', strtotime($order['Order']['modified'])).'&nbsp;</div>';
                        $status = '<div class="status status-'.strtolower($order['Order']['status']).'">'.$order['Order']['status'].'&nbsp;</div>';
                        $name = '<div class="name">'.$order['Order']['first_name'].' '.$order['Order']['last_name'].'&nbsp;</div>';
                        $name .= '<div class="email">'.$order['Order']['email'].'&nbsp;</div>';
                        $phone = '<div class="phone">'.$order['Order']['phone'].'&nbsp;</div>';
                        if (!empty($phone)) {
                            $name .= '<span class="phone">'.$phone.'</span>';
                        }

                        $address  = '<div class="address">'.$order['Order']['address'].'</div>';
                        if (!empty($order['Order']['address2'])) {
                            $address .= '<div class="address">'.$order['Order']['address2'].'</div>';
                        }
                        $csz = $order['Order']['city'];
                        if (!empty($order['Order']['state'])) {
                            $csz .= ' '.$order['Order']['state'];
                        }
                        if (!empty($order['Order']['zip'])) {
                            $csz .= ', '.$order['Order']['zip'];
                        }
                        $address .= '<div class="csz">'.$csz.'</div>';
                        if (empty($order['Order']['address2'])) {
                            $address .= '<div class=""></div>';
                        }

                        $raffle = $order['Raffle']['name'];

                        if (!empty($order['Order']['printed'])) {
                            $printed  = '<a href="/orders/view/'.$order['Order']['id'].'/printer1" class="btn btn-printed">';
                            $printed .= 'Tickets';
                            $printed .= '</a>';
                        } else {
                            $printed  = '<a href="/orders/view/'.$order['Order']['id'].'/printer1" class="btn">';
                            $printed .= 'Tickets';
                            $printed .= '</a>';
                        }
                        /* settings for address labels */

                        if (!empty($order['Order']['printed2'])) {
                            $printed2  = '<a href="/orders/address/'.$order['Order']['id'].'" class="btn btn-printed">';
                            $printed2 .= 'Address';
                            $printed2 .= '</a>';

                        } else {
                            $printed2  = '<a href="/orders/address/'.$order['Order']['id'].'/" class="btn">';
                            $printed2 .= 'Address';
                            $printed2 .= '</a>';
                        }

                        ?>
                    <tr class="<?php echo $rowclass; ?>" rel="tickets-<?php echo $order['Order']['id']; ?>">
                        <?php echo '<td align="center"><input type="checkbox" name="data[Order][][id]" id="OrderId'.$order['Order']['id'].'" value="'.$order['Order']['id'].'"></td>'; ?>
                        <?php echo '<td>'.$raffle.'</td>'; ?>
                        <?php //echo '<td>'.$transaction_number.'</td>'; ?>
                        <?php echo '<td nowrap>'; ?>
                        <?php foreach ($order['Ticket'] as $ticket): ?>
                        <?php echo $ticket['reference_number']; ?>
                        <script>
                                function printTickets()
                                {
                                    window.print()
                                }
                            </script>
                                <input type="button" value="Print" onclick="printMe()"><br />
                        <?php endforeach; ?>
                        <?php echo '</td>'; ?>
                        <?php echo '<td>'.$total.'</td>'; ?>
                        <?php echo '<td>'.$date.'</td>'; ?>
                        <?php echo '<td>'.$status.'</td>'; ?>
                        <?php echo '<td>'.$name.'</td>'; ?>
                        <?php echo '<td>'.$address.'</td>'; ?>
                        <?php echo '<td>'.$printed.'</td>'; ?>
                        <?php echo '<td>'.$printed2.'   

                     </td>';?>
                    </tr>
                    <?php endforeach; ?>

                </tbody>
            </table>
        <?php echo $this->element('pager'); ?>
    <?php $this->start('sidebox.orders');?>
    <div class="well">
        <?php echo $this->Html->link(__('New Order', true), array('action' => 'add'), array('class' => 'btn primary')); ?>
        <?php echo $this->Html->link(__('List Orders', true), array('controller' => 'orders', 'action' => 'index'), array('class' => 'btn')); ?>
        <?php echo $this->Html->link(__('New Order', true), array('controller' => 'orders', 'action' => 'add'), array('class' => 'btn')); ?>
        <?php echo $this->Html->link(__('List Tickets', true), array('controller' => 'tickets', 'action' => 'index'), array('class' => 'btn')); ?>
        <?php echo $this->Html->link(__('New Ticket', true), array('controller' => 'tickets', 'action' => 'add'), array('class' => 'btn')); ?>
    </div>
    <?php $this->end(); ?>
    <?php $this->append('sidebox', $this->fetch('sidebox.orders')); ?>
</div>
<script>
$('.tickets').hide();
$(function(){

    $('.tickets-link').click(function(e){
        e.preventDefault();
        $('.'+$(this).attr('rel')).slideToggle();
    });

    $('.btn-printed, .btn-multiprint').addClass('disabled');

    $(':checkbox').change(function(){
        if ($(':checked').length) {
            $('.btn-multiprint').removeClass('disabled');
        } else {
            $('.btn-multiprint').addClass('disabled');
        }
    });

    $('.btn-multiprint').click(function(e){
        e.preventDefault();

        var ids = [];
        $(':checked').each(function(){
            ids.push($(this).attr('value'));
        });

        window.location = $(this).attr('href')+ids.join(',')+'/'+$(this).attr('id').replace('multiprint','printer');

    });
});
</script>

I may be missing something and maybe you have thought of this, but i feel I had to do something similar years back... why not use a print stylesheet to hide that which you don't want printed and show what you do? 我可能会丢失一些东西,也许您已经想到了这一点,但是我觉得几年前我不得不做些类似的事情……为什么不使用打印样式表来隐藏您不想打印的内容并显示您的工作呢? You could try dynamically loading a different print stylesheet if different clicks have different actions. 如果不同的点击具有不同的操作,则可以尝试动态加载不同的打印样式表。 How to load up CSS files using Javascript? 如何使用Javascript加载CSS文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM