jQuery多个表在页面中分开打印

时间:2017-02-02 13:28:12

标签: javascript jquery html ajax

我想在打印时将多个表行划分为不同的页面。 第一个表行分开完成然后单独开始下一个表行,每个页面可以包含21个

非常感谢任何帮助



<script>

jQuery(document).ready(function()
{
	for (i=0; i<document.getElementsByTagName("table").length; i++)
	{
		document.getElementsByTagName("table")[i].style.pageBreakBefore="always";
		
	}
	
	var div_pageBreaker = '<div style="page-break-before:always;"></div>';
	var per_page = 15;
	$('table').each(function(index, element)
	{
		console.log($(element));
		//how many pages of rows have we got?
		var pages = Math.ceil($('tbody tr').length / per_page);

		//if we only have one page no more
		if (pages == 1) {
			return;
		}
		//get the table we're splutting
		var table_to_split = $(element);

		var current_page   = 1;
		//loop through each of our pages
		for (current_page = 1; current_page <= pages; current_page++) 
		{
			//make a new copy of the table
			var cloned_table = table_to_split.clone();
			//remove rows on later pages
			$('tbody tr', table_to_split).each(function(loop, row_element) {
				//if we've reached our max
				if (loop >= per_page) {
					//get rid of the row
					$(row_element).remove();
				}
			});

			//loop through the other copy
			$('tbody tr', cloned_table).each(function(loop, row_element) {
				//if we are before our current page
				if (loop < per_page) {
					//remove that one
					$(row_element).remove();
				}
			});

			//insert the other table afdter the copy
			if (current_page < pages) {
				$(div_pageBreaker).appendTo('#appendTable');
				$(cloned_table).appendTo('#appendTable');
			}

			//make a break
			table_to_split = cloned_table;
		}
	});

});
</script>
&#13;
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>

<table>
	<thead>
		<tr>
			<td><h4>Table 1</h4></td>
		</tr>
	</thead>
	<tbody>
		<tr><td>1</td></tr>
		<tr><td>2</td></tr>
		<tr><td>3</td></tr>
		<tr><td>4</td></tr>
		<tr><td>5</td></tr>
		<tr><td>6</td></tr>
		<tr><td>7</td></tr>
		<tr><td>8</td></tr>
		<tr><td>9</td></tr>
		<tr><td>10</td></tr>
		<tr><td>11</td></tr>
		<tr><td>12</td></tr>
		<tr><td>13</td></tr>
		<tr><td>14</td></tr>
		<tr><td>15</td></tr>
		<tr><td>16</td></tr>
	
	</tbody>
</table>

<table>
	<thead>
		<tr>
			<td><h4>Table 2</h4></td>
		</tr>
	</thead>
	<tbody>
		<tr><td>1</td></tr>
		<tr><td>2</td></tr>
		<tr><td>3</td></tr>
		<tr><td>4</td></tr>
		<tr><td>5</td></tr>
		<tr><td>6</td></tr>
		<tr><td>7</td></tr>
		<tr><td>8</td></tr>
		<tr><td>9</td></tr>
		<tr><td>10</td></tr>
		<tr><td>11</td></tr>
		<tr><td>12</td></tr>
		<tr><td>13</td></tr>
		<tr><td>14</td></tr>
		<tr><td>15</td></tr>

	</tbody>
</table>
 <div id="appendTable"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这是最后更新的代码并删除了空白页: -

&#13;
&#13;
    jQuery(document).ready(function() {
  for (i = 0; i < document.getElementsByTagName("table").length; i++) {
    document.getElementsByTagName("table")[i].style.pageBreakBefore = "always";

  }

  var div_pageBreaker = '<div style="page-break-before:always;"></div>';
  var per_page = 15;
  $('table').each(function(index, element) {
    debugger;
    console.log($(element));
    //how many pages of rows have we got?
    //EDIT:?????????????????????????????????
    var pages = Math.ceil($(element).find('tbody tr').length / per_page);

    //if we only have one page no more
    if (pages == 1) {
      return;
    }
    //get the table we're splutting
    var table_to_split = $(element);

    var current_page = 1;
    //loop through each of our pages
    for (current_page = 1; current_page <= pages; current_page++) {
      //make a new copy of the table
      var cloned_table = table_to_split.clone();
      //remove rows on later pages
      $('tbody tr', table_to_split).each(function(loop, row_element) {
        //if we've reached our max
        if (loop >= per_page) {
          //get rid of the row
          $(row_element).remove();
        }
      });

      //loop through the other copy
      $('tbody tr', cloned_table).each(function(loop, row_element) {
        //if we are before our current page
        if (loop < per_page) {
          //remove that one
          $(row_element).remove();
        }
      });

      //insert the other table afdter the copy
      if (current_page < pages) {
        //check here if the table has trs then append else do nothing
        if (cloned_table.find('tbody tr').length > 0) {
          //EDIT:?????????????????????????????????
          $(div_pageBreaker).appendTo($(element));
          $(cloned_table).appendTo($(element));
        }
      }

      //make a break
      table_to_split = cloned_table;
    }
  });

});
  
&#13;
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body>
  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>

  <table>
    <thead>
      <tr>
        <td>
          <h4>Table 1</h4></td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
      </tr>
      <tr>
        <td>2</td>
      </tr>
      <tr>
        <td>3</td>
      </tr>
      <tr>
        <td>4</td>
      </tr>
      <tr>
        <td>5</td>
      </tr>
      <tr>
        <td>6</td>
      </tr>
      <tr>
        <td>7</td>
      </tr>
      <tr>
        <td>8</td>
      </tr>
      <tr>
        <td>9</td>
      </tr>
      <tr>
        <td>10</td>
      </tr>
      <tr>
        <td>11</td>
      </tr>
      <tr>
        <td>12</td>
      </tr>
      <tr>
        <td>13</td>
      </tr>
      <tr>
        <td>14</td>
      </tr>
      <tr>
        <td>15</td>
      </tr>
      <tr>
        <td>16</td>
      </tr>

    </tbody>
  </table>

  <table>
    <thead>
      <tr>
        <td>
          <h4>Table 2</h4></td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
      </tr>
      <tr>
        <td>2</td>
      </tr>
      <tr>
        <td>3</td>
      </tr>
      <tr>
        <td>4</td>
      </tr>
      <tr>
        <td>5</td>
      </tr>
      <tr>
        <td>6</td>
      </tr>
      <tr>
        <td>7</td>
      </tr>
      <tr>
        <td>8</td>
      </tr>
      <tr>
        <td>9</td>
      </tr>
      <tr>
        <td>10</td>
      </tr>
      <tr>
        <td>11</td>
      </tr>
      <tr>
        <td>12</td>
      </tr>
      <tr>
        <td>13</td>
      </tr>
      <tr>
        <td>14</td>
      </tr>
      <tr>
        <td>15</td>
      </tr>

    </tbody>
  </table>
  <div id="appendTable"></div>
</body>

</html>
&#13;
&#13;
&#13;

修改: 这是一个有效Demo 希望这能回答你的问题。