2016-07-13 3 views
-1

jQuery를 사용하여 DustJS 변수가 포함 된 HTML을 추가하고 싶습니다.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>'); 
    }); 
    }); 

내 출력됩니다 : 여기에 내가 jQuery를에서 할 노력하고 무엇

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

관련 문제