2016-03-02 600 views
1

我的網頁上有一個ACE代碼編輯器,當下拉選項被更改時,它應該用一個新值進行刷新。目前,當通過下拉菜單的「onchange」調用該功能時,會出現警告消息。但是,我無法更改ACE代碼編輯器中顯示的內容。如何更改jquery-ace代碼編輯器文本區域的值?

我正在使用這個jQuery插件 - http://cheef.github.io/jquery-ace/。它沒有太多的文檔。

<div class="container"> 
<div class="column-left"> 
<h2>Sources</h2> 
    <%= collection_select(:source, :select_source, Source.all, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea()"}) %> 
</div> 
<div class="column-center"> 
<h2>Code</h2> 
    <textarea id="txtbox1" class="my-code-area" rows="20" style="width: 100%">awaiting source...</textarea> 
</div> 
</div> 


<script> 
    $('.my-code-area').ace({ 
     theme: 'twilight', 
     lang: 'ruby' 
    }) 
    var decorator = $('.my-code-area').data('ace'); 
    var aceInstance = decorator.ace; 

    function updateTextArea() { 
     var text = "source changed" 
     alert(text); 
     aceInstance.edit("txtbox1").setValue("source changed"); 
    }  
</script> 
+0

嘗試將setValue()替換爲值() –

+0

這沒有幫助。它實際上似乎是「編輯」本身是有問題的 - 未捕獲TypeError:無法讀取未定義的屬性'編輯'。所以我可能會錯誤地認爲aceInstance等同於var editor = ace.edit(「editor」); (https://ace.c9.io/#nav=embedding) – A21

+0

在沙盒中創建示例plz –

回答

0

可以使用

var editor = $('.my-code-area').data('ace').editor.ace 

得到jQuery的包裝ACE實例,然後調用

editor.setValue("text") 

或如果你想重置撤消管理器

editor.session.setValue("text") 
+0

editor.session.setValue(「text」)比每次創建和銷燬ace實例都更好。此外,它還解決了每次調用updateTextArea函數時都不會追加到代碼編輯器中已存在的字符串的問題。 – A21

0

我找到方法來改變值...

$('.my-code-area').ace({ 
    theme: 'twilight', 
    lang: 'ruby' 
    }); 

    function updateTextArea() { 
    var decorator = $('.my-code-area').data('ace'); 
    var aceInstance = decorator; 
    var text = "source changed" 
    aceInstance.destroy(); 
    aceInstance.create(); 
    aceInstance.editor.theme('twilight'); 
    aceInstance.editor.lang('ruby'); 
    aceInstance.editor.value('source changed'); 
    } 
+0

啊,謝謝!基本上,必須銷燬ace代碼編輯器實例,並通過更改下拉菜單重新創建。 – A21

相關問題