Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to append some HTML containing DustJS variables using jQuery. Here is what I am trying to do in jQuery:

$(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>');
    });
  });

Here is my output:

enter image description here

How do I evaluate those variables???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

$(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>';
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...