概要
フロント:HTML5, CSS, JavaScript, jQuery
バックエンド:python(3.7.1)、Django(2.1)
にて、Webアプリを学習しながら作っています。
店舗別、月別の売上高、変動比率、固定費をテーブルで保持し、利益を計算したものを一つの表に出力しています。店舗は2つです。
この表の中で、各店舗合計と総合計(2店舗合計)を出したいと思っています。
pythonのコードではなく、JavaScript、jQueryでの解決方法を探しています。
<現状>
店舗 | 月 | 売上 | 変動費 | 固定費 | 利益 |
---|---|---|---|---|---|
有楽町 | 1月 | 500 | 250 | 200 | 50 |
有楽町 | 2月 | 500 | 250 | 200 | 50 |
有楽町 | 3月 | 500 | 250 | 200 | 50 |
秋葉原 | 1月 | 500 | 250 | 200 | 50 |
秋葉原 | 2月 | 500 | 250 | 200 | 50 |
秋葉原 | 3月 | 500 | 250 | 200 | 50 |
<あるべき結果>
店舗 | 月 | 売上 | 変動費 | 固定費 | 利益 |
---|---|---|---|---|---|
有楽町 | 1月 | 500 | 250 | 200 | 50 |
2月 | 500 | 250 | 200 | 50 | |
3月 | 500 | 250 | 200 | 50 | |
小計 | 1500 | 750 | 600 | 150 | |
秋葉原 | 1月 | 500 | 250 | 200 | 50 |
2月 | 500 | 250 | 200 | 50 | |
3月 | 500 | 250 | 200 | 50 | |
小計 | 1500 | 750 | 600 | 150 | |
全店舗 | 合計 | 3000 | 1500 | 1200 | 300 |
現在のソースコード(参考)
views.py
python
1from django.conf import settings 2from django.shortcuts import get_object_or_404, render, redirect, resolve_url 3from django.http import HttpResponse 4from django.views import generic 5from django.views.generic import ListView, DetailView, TemplateView 6from .models import Plan, Shop 7 8 9class IndexView(TemplateView): 10 def get(self, request, *args, **kwargs): 11 plan_list = Plan.objects.all().order_by('shop', 'month') 12 params = { 13 'plan_list': plan_list, 14 } 15 return render(request, 'myapp/index.html', params)
models.py
python
1from django.db import models 2 3 4class Shop(models.Model): 5 name = models.CharField('店舗名', max_length=20) 6 def __str__(self): 7 return self.name 8 9 10class Plan(models.Model): 11 shop = models.ForeignKey(Shop, on_delete=models.PROTECT, blank=True) 12 month = models.PositiveIntegerField('月') 13 sales = models.PositiveIntegerField('売上高') 14 variable_rate = models.DecimalField('変動比率', max_digits=4, decimal_places=2) 15 fixed_cost = models.PositiveIntegerField('固定費') 16 17 def __int__(self): 18 return self.sales 19 20 def variable_cost(self): 21 variable_cost = self.sales * self.variable_rate / 100 22 return variable_cost 23 24 def calc_profit(self): 25 profit = self.sales - self.variable_cost() - self.fixed_cost 26 return profit
index.html
html
1<!doctype html> 2<html> 3 <head> 4 <!-- Required meta tags --> 5 <meta charset="utf-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 7 <!-- Bootstrap CSS --> 8 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 9 <title>WebAPP</title> 10 </head> 11 <body> 12 <div class="container"> 13 <div class="row"> 14 <div class="col"> 15 <h1>計画APP</h1> 16 <br><br> 17 <table id="mytable" class="table"> 18 <thead> 19 <th>店舗</th> 20 <th>月</th> 21 <th>売上</th> 22 <th>変動費</th> 23 <th>固定費</th> 24 <th>利益</th> 25 </thead> 26 {% for item in plan_list %} 27 <tbody> 28 <td>{{ item.shop }}</td> 29 <td>{{ item.month }}</td> 30 <td>{{ item.sales }}</td> 31 <td>{{ item.variable_cost }}</td> 32 <td>{{ item.fixed_cost }}</td> 33 <td>{{ item.calc_profit }}</td> 34 </tbody> 35 {% endfor %} 36 </table> 37 </div> 38 </div> 39 </div> 40 <!-- Optional JavaScript --> 41 <!-- jQuery first, then Popper.js, then Bootstrap JS --> 42 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> 43 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> 44 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 45 </body> 46</html>
ご教授の程、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー