2016-04-16 7 views
0

テーブルの一部として選択(オプション付き)をレンダリングするにはどうすればよいですか?Prefill jsrenderテンプレート

$('select').append($('<option>').text('single')); 
 
$('table').append($('#rowTmpl').render({name:'peter'}));
th,td {border:1px solid black;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://rawgit.com/BorisMoore/jsrender/master/jsrender.min.js"></script> 
 
<div style="display:none" id="statis"> 
 
    <select></select> 
 
</div> 
 
<script id="rowTmpl" type="text/x-jsrender"> 
 
    <tr> 
 
    <td>{{:name}}</td> 
 
    <td>{{include tmpl=#statis /}}</td> 
 
    </tr> 
 
</script> 
 

 
<table> 
 
    <tr> 
 
    <th>Name</th> 
 
    <th>Stat</th> 
 
    </tr> 
 
</table>

最初のジャバスクリプトのラインは、選択(4 HTMLライン)にオプションを追加します。 2番目のjavascript行は、peter人のテーブルに行を表示する必要があります。

問題:

  1. 最初のJavaScriptの行が選択タグを見つけることができませんので、私は、スクリプト・タグにdiv要素を作成することはできません。
  2. {{includeでdivを表示することはできません。#statisはdivでスクリプトタグではないからです。
+0

あなたはより多くを説明できますか?! – Gintoki

+0

NameとStatの名前が「Peter」、2列目の選択フィールドがstatであるテーブルが必要です。 JSRenderは、jsonを使用してhtmlをレンダリングし、テーブルのデータ行をレンダリングするテンプレートエンジンです。何を正確に説明するのですか? –

答えて

1

{{include}}に構文エラーがあります。それは次のようになります。

<td>{{include tmpl="#statis" /}}</td> 

実際にあなたが隠されたdivの中JsRenderテンプレートを宣言することができますが、副作用があり得るので、それは、一般的に良い習慣ではありません。参照:https://stackoverflow.com/a/25609895/1054484

文字列からテンプレートを宣言し、必要に応じて動的に文字列を事前に操作することもできます。

も参照してください:

$('select').append($('<option>').text('single')); 
 
$('table').append($('#rowTmpl').render({name:'peter'}));
th,td {border:1px solid black;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://rawgit.com/BorisMoore/jsrender/master/jsrender.min.js"></script> 
 
<div style="display:none" id="statis"> 
 
    <select></select> 
 
</div> 
 
<script id="rowTmpl" type="text/x-jsrender"> 
 
    <tr> 
 
    <td>{{:name}}</td> 
 
    <td>{{include tmpl="#statis" /}}</td> 
 
    </tr> 
 
</script> 
 

 
<table> 
 
    <tr> 
 
    <th>Name</th> 
 
    <th>Stat</th> 
 
    </tr> 
 
</table>

+0

http://www.jsviews.com/#samples/jsr/composition/tmpl私の構文が正しいことを教えてくれた.... –

+1

よくない、tmpl =#そのサンプルの内容はjQueryセレクタではなく、ビューを使用する。コンテンツ。 http://www.jsviews.com/#[email protected]を参照してください。だからそれは引用符を持っていないのです。 #content is view.content - ビューのパスを参照してください:http://www.jsviews.com/#paths – BorisMoore

+0

私は理解する時間がなかったし、他の人に教える時間もなかった。とにかく良い仕事と魚のためのありがとう。 –

関連する問題