简体   繁体   English

Rails:如何在render:pdf中从HTML调用Javascript函数

[英]Rails: How to call a Javascript function from HTML in render :pdf

I need to show a text result of an amount, and for this I'm using a javascript function "toWords". 我需要显示一定数量的文本结果,为此,我正在使用JavaScript函数“ toWords”。 This function is working great when I use onInput or onClick, but I need to call this function when the document opens. 当我使用onInput或onClick时,此函数很好用,但是在文档打开时我需要调用此函数。 So this is what I tried: 所以这就是我尝试的:

  <div class="label-field-pair3-text-area">
    <p id="demo"></p>

    </div>


<script type='text/javascript'>



document.getElementById("demo").innerHTML = window.toWords("<%= @amount.to_f %>"); 

..... </script>

But this doesn't work. 但这是行不通的。 I also tried this: 我也试过这个:

 j(document).ready( function(){ text_amount();});
 function text_amount(){
  var i= getElementbyId('payment_text');
  i.val(toWords("<%= @amount.to_f %>"));}

And this: 和这个:

   <div class="label-field-pair3-text-area" id='payment_text' onload = "text_amount(150)">

But none of this worked, and nothing is displayed. 但是这些都不起作用,什么也不显示。

UPDATE: When I removed render :pdf from controller, it worked. 更新:当我从控制器中删除render:pdf时,它起作用了。 So the problem is in running this function onLoad upon rendering my PDF file. 因此,问题在于在渲染我的PDF文件时运行此功能onLoad。

It looks like you have a mix of jquery and regular js there: you define i with getElementById and then call the jquery object function val() on it. 看起来您在那里混合了jquery和常规js:使用getElementById定义i ,然后在其上调用jquery对象函数val() val() needs to be called on a jquery object, which i is not. val()需要在jquery对象上调用,而i不是。 Try this instead: 尝试以下方法:

 function text_amount(){
   var i = $('#payment_text');
   i.val(toWords("<%= @amount.to_f %>"));
 }

or, more simply, 或者,更简单地说,

 function text_amount(){
   $('#payment_text').val(toWords("<%= @amount.to_f %>"));
 }

Check if the method toWords is loaded by 检查方法toWords是否被加载

console.log(window.toWords);

before you use it 使用之前

The html page load the script in sequence, if you add the method after you use it, it is undefined. html页面按顺序加载脚本,如果在使用后添加方法,则该脚本是未定义的。

I'm also having problems with render :pdf, so I'm using this instead: 我在render:pdf方面也遇到了问题,所以我改用了这个:

    <%= link_to t('print'), "#", {:class  => 'grey-button-large themed_bg themed-dark-hover-background',  :href=>'javascript:printReceipt()'}%>

Then in script: 然后在脚本中:

 function printReceipt()
{ 
  //$('#page-yield').printElement(); 
   var divName = "page-yield";
    var printContents = document.getElementById(divName).innerHTML;
    var originalContents = document.body.innerHTML;

    document.body.innerHTML = printContents;

    window.print();

    document.body.innerHTML = originalContents;
}

This is a temporary solution that will open your file as a page and when the user clicks on print, it gets printed. 这是一个临时解决方案,它将以页面形式打开您的文件,并且当用户单击“打印”时,文件将被打印。

However, I advice you to check PRAWN: 但是,我建议您检查PRAWN:

http://www.sitepoint.com/pdf-generation-rails/ http://www.sitepoint.com/pdf-generation-rails/

How to render format in pdf in rails 如何在Rails中呈现pdf格式

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

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