vue實現打印、批量打印


vue+element實現單條打印、批量打印(圖片)

winodw.print()方法

print() 方法用於打印當前窗口的內容。調用 print() 方法會產生一個打印預覽彈框,讓用戶可以設置打印請求。最簡單的打印就是直接調用window.print(),當然用 document.execCommand('print') 也可以達到同樣的效果。默認打印頁面中body里的所有內容。

<template>
  <div class="dashboard-container">
    <el-button @click='allPrint'>批量打印</el-button>
    <el-table :data="tableData" style="width: 100%" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="id" width="180">
      </el-table-column>
      <el-table-column label="圖片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.src" class="table_img">
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="print(scope.row)" type="text" size="small">打印</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--startprint-->
    <div id="printcontent" ref='printcontent'>
      <img :src="item.src" class="print_img" v-for="item in multipleSelection" :key='item.id' />
    </div>
    <!--endprint-->
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [{
            id: 1,
            src:'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
          },
          {
            id: 2,
            src:'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
          },
          {
            id: 3,
            src:'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg'
          }
        ],
        multipleSelection: [],   //存放將要打印的數據
      }
    },
    methods: {
      print(row={}) {
        if(row.src){
          this.multipleSelection.push(row)
        }
        this.$nextTick(()=>{
          var bdhtml=window.document.body.innerHTML;       // 獲取body的內容
          var jubuData = document.getElementById("printcontent").innerHTML; //獲取要打印的區域內容
          window.document.body.innerHTML= jubuData;        
          window.print();                                  // 調用瀏覽器的打印功能
          window.document.body.innerHTML=bdhtml;           // body替換為原來的內容
          window.location.reload();                        //刷新頁面,如果不刷新頁面再次點擊不生效或打印使用新窗口實現打印
        })
      },
      allPrint(){
        this.print()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    }
  }
</script>

<style lang="scss">
  .dashboard-container {
    .table_img {
      width: 50px;
      height: 50px;
    }
    #printcontent{
      display: none;
    }
  }
  //使用媒體查詢    設置預覽時的樣式
  @media print{
    @page {
      margin: 0;
      size: portrait;   //設置打印布局portrait為縱向;landscape為橫向
    }
    #printcontent{
      width: 100%;
    }
    .print_img{
      display: block;
      width: 100%;
      height: 100%;
      margin: auto auto;
    }
  }

</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM