2016-07-13 3 views
-1

jQueryを使用してDustJS変数を含むHTMLを追加したいとします。ここで私はjQueryの中でやろうとしていますものです:ここではjqueryからDustJS変数を評価する

$(document).on('ready', function(){ 
    $("tr").click(function(){ 
     $(this).after('<tr class="row-details">\ 
          <td></td>\ 
          <td colspan="4">\ 
          <table class="sortable draggable">\ 
           <thead>\ 
            <tr>\ 
             <th class="col-itemName">Item Name</th>\ 
             <th class="col-quantity">Quantity</th>\ 
             <th class="col-rate">Rate</th>\ 
             <th class="col-amount">Amount</th>\ 
            </tr>\ 
           </thead>\ 
           <tbody>\ 
            {#items}\ 
            <tr>\ 
             <td>{.item.itemName}</td>\ 
             <td>{.quantity}</td>\ 
             <td>{.rate}</td>\ 
             <td>{@math key="{.quantity}" method="multiply" operand="{.rate}"/}</td>\ 
            </tr>\ 
            {/items}\ 
           </tbody>\ 
          </table>\ 
          </td>\ 
         </tr>'); 
    }); 
    }); 

は私の出力です:

enter image description here

私はこれらの変数を評価するにはどうすればよいです?

+0

@ゴートそのページの内容はわかりません。あなたは何を説明しようとしていますか? – Vishal

+0

jQueryでHTMLを追加するだけです。コードにダストを使用していることを示すものは何もありません。 jQueryはどのようにダストのパラメータをどうするべきかを知っていますか? DOMでHTMLをレンダリングしようとする前に、HTML文字列を 'dust.render'に渡して変数を出力させる必要があります。 – Simon

+0

@サイモンあなたは私に例を教えてもらえますか? – Vishal

答えて

1
$(document).on('ready', function() { 
    $("tr").click(function() { 
     var self = this, 
      templateData = {}; // some set of data you want to render in the template 

     dust.render(templateString, templateData, function (err, out) { 
       if (err && typeof console !== 'undefined' && console.error) { 
        console.error(err); 
       } 

       $(self).after(out); 
     }); 
    }); 

    var templateString = '<tr class="row-details">\ 
          <td></td>\ 
          <td colspan="4">\ 
          <table class="sortable draggable">\ 
           <thead>\ 
            <tr>\ 
             <th class="col-itemName">Item Name</th>\ 
             <th class="col-quantity">Quantity</th>\ 
             <th class="col-rate">Rate</th>\ 
             <th class="col-amount">Amount</th>\ 
            </tr>\ 
           </thead>\ 
           <tbody>\ 
            {#items}\ 
            <tr>\ 
             <td>{.item.itemName}</td>\ 
             <td>{.quantity}</td>\ 
             <td>{.rate}</td>\ 
             <td>{@math key="{.quantity}" method="multiply" operand="{.rate}"/}</td>\ 
            </tr>\ 
            {/items}\ 
           </tbody>\ 
          </table>\ 
          </td>\ 
         </tr>'; 
}); 
+0

ここでは、templateDataをオンザフライで定義していますが、データは.dustページのデータベースから取得しています。したがって、クリックされた要素は、それらのダスト変数の現在のコンテキストになります。それでは、どのようにデータを取得できますか?それとも、私はここにアヤックスコールをしなければならないのですか?もしそうなら、現在選択されているtrのIDを取得する方法は? – Vishal

+0

さて、私はそれを得た。しかし、今すぐあなたのコードに来る:私は、塵が定義されていないと言っているエラーを取得します。だから、どうすればjQueryでほこりを定義できますか? – Vishal

+0

jQueryをインクルードしたのと同じように、ページにダストスクリプトを含める必要があります。 – Simon

関連する問題