Evaluate DustJS variables from jquery

孤街醉人 提交于 2019-12-13 08:28:45

问题


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:

How do I evaluate those variables???


回答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>';
});


来源:https://stackoverflow.com/questions/38347515/evaluate-dustjs-variables-from-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!