使用Coffeescript的汇总表?

时间:2015-12-02 14:56:15

标签: coffeescript

我编写了一张摘要图表,我试图添加" Totals"将总结三列的行。练习由许多问题组成,参与者可以增加,减少或维持一定的金额。 B列总计显示初始值(滑块开始的位置)。 C列显示参与者增加或减少的金额,最后一列显示结果金额。 (B + C)

这是摘要图表的一个例子

A栏--- B ------ C ------ D

问题1 | 100 $ | + 28 $ | 128 $ |
问题2 | 150 $ | (10 $)| 140 $ |

总计------ | 250 $ | + 18 $ | 268 $ |

到目前为止,我已经能够对B列和D列的总计进行编程,但我无法弄清楚如何显示C列的总计。

class window.CustomSimulator extends window.TaxSimulator
  constructor: (@options = {}) ->
    super
    @updateTable()

update: ->
  super
  @updateTable()

updateTable: ->
  self = this

  $table = $('<table class="table table-condensed"><thead><tr><th>Category</th><th>Before</th><th>Your Choice</th><th>After</th></tr></table>')
  $tbody = $('<tbody></tbody>')

  before_total = 0
  after_total = 0

  @scope.find('.slider').each ->
    $this = $(this)
    $parents = $this.parents('tr')

    category    = $parents.find('.header').clone().children().remove().end().text().replace(/How would you adjust service levels and property tax funding for /, '').replace('?', '')
    before      = self.tipScaler($this, $this.data('initial'))
    your_choice = $parents.find('.value').text()
    after       = $parents.find('.tip-content').text()

  if $parents.find('.key').text() == 'Decrease:'
    css_class = 'decrease'
    your_choice = "(#{your_choice})"
  else
    css_class = 'increase'

  $tr = $("""<tr><td>#{category}</td><td>#{before}</td><td class="table-#{css_class}">#{your_choice}</td><td>#{after}</td></tr>""")
  $tbody.append($tr)

  before_total += parseFloat(before.replace('$', ''))
  after_total += parseFloat(after.replace('$', ''))

  before_total = SimulatorHelper.number_to_currency(before_total, precision: 2, strip_insignificant_zeros: true)
  after_total = SimulatorHelper.number_to_currency(after_total, precision: 2, strip_insignificant_zeros: true)
  $("""<tfoot><tr><th>Totals</th><th>#{before_total}</th></th><th><th>#{after_total}</th></tr></tfoot>""").appendTo($table)

$table.append($tbody)
$('#summary-table').html($table)

我对此很新,所以我不确定这是否足够了。

谢谢!

0 个答案:

没有答案