Category.cshtml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. @page
  2. @model Admin.Pages.Crypto.CategoryModel
  3. @{
  4. ViewData["Title"] = "코인 카테고리";
  5. }
  6. <div class="container-fluid">
  7. <h3>@ViewData["Title"]</h3>
  8. <hr />
  9. <partial name="_StatusMessage" />
  10. <div class="row g-2 align-items-end mt-2">
  11. <div class="col">
  12. Total : @Model.Total
  13. </div>
  14. <div class="col text-end">
  15. <button type="button" id="btnAdd" class="btn btn-primary" form="fAdminWrite">추가</button>
  16. <button type="submit" id="btnSave" class="btn btn-success" form="fAdminWrite">저장</button>
  17. </div>
  18. </div>
  19. <div class="table-responsive">
  20. <form id="fAdminWrite" method="post" accept-charset="utf-8" autocomplete="off"></form>
  21. <table class="table table-striped table-bordered table-hover mt-3">
  22. <caption>
  23. 코인이 등록된 카테고리는 삭제할 수 없습니다.<br />
  24. 카테고리를 삭제하려면 해당 코인의 카테고리를 먼저 변경해 주세요.
  25. </caption>
  26. <colgroup>
  27. <col style="width: 5%;" />
  28. <col />
  29. <col />
  30. <col />
  31. <col />
  32. <col />
  33. <col />
  34. <col />
  35. <col />
  36. </colgroup>
  37. <thead>
  38. <tr class="text-center">
  39. <th>ID</th>
  40. <th>Code</th>
  41. <th>카테고리 명</th>
  42. <th>코인 수</th>
  43. <th>순서</th>
  44. <th>사용</th>
  45. <th>등록일시</th>
  46. <th>수정일시</th>
  47. <th>비고</th>
  48. </tr>
  49. </thead>
  50. <tbody id="categories">
  51. @if (Model.List == null || Model.List.Count <= 0)
  52. {
  53. <tr>
  54. <td colspan="9">No Data.</td>
  55. </tr>
  56. }
  57. else
  58. {
  59. @foreach (var row in Model.List)
  60. {
  61. var index = row.Index;
  62. <tr>
  63. <td>
  64. <input type="text" readonly class="form-control-plaintext text-center @(row.CoinCount > 0 ? "text-white bg-danger" : "")" value="@row.Num" />
  65. <input type="hidden" name="request[@index].ID" readonly class="form-control-plaintext text-center" required form="fAdminWrite" value="@row.ID" />
  66. </td>
  67. <td>
  68. <input type="text" name="request[@index].Code" class="form-control" maxlength="30" required form="fAdminWrite" value="@row.Code" />
  69. </td>
  70. <td>
  71. <input type="text" name="request[@index].Name" class="form-control" maxlength="100" required form="fAdminWrite" value="@row.Name" />
  72. </td>
  73. <td>@row.CoinCount</td>
  74. <td>
  75. <input type="number" name="request[@index].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" value="@row.Order" />
  76. </td>
  77. <td>
  78. <div class="form-check-inline">
  79. <input class="form-check-input" type="checkbox" id="request_@(index)_IsActive" name="request[@index].IsActive" checked="@Model.Data[index].IsActive" form="fAdminWrite" value="true" />
  80. <label class="form-check-label" for="request_@(index)_IsActive">
  81. 사용
  82. </label>
  83. </div>
  84. </td>
  85. <td><input type="text" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.CreatedAt" /></td>
  86. <td><input type="text" readonly class="form-control-plaintext text-center" form="fAdminWrite" value="@row.UpdatedAt" /></td>
  87. <td>
  88. <button type="button" class="btn btn-sm btn-danger btn-delete">삭제</button>
  89. </td>
  90. </tr>
  91. }
  92. }
  93. </tbody>
  94. </table>
  95. </div>
  96. </div>
  97. @section Scripts {
  98. <script>
  99. $(function() {
  100. let $categories = $("#categories");
  101. let total = Number(@Model.Total);
  102. $(document).on("click", "#btnAdd", function() {
  103. if (total <= 0) {
  104. $categories.empty();
  105. }
  106. let tableRow = `
  107. <tr>
  108. <td>-</td>
  109. <td>
  110. <input type="text" name="request[${total}].Code" class="form-control" maxlength="30" required form="fAdminWrite" />
  111. </td>
  112. <td>
  113. <input type="text" name="request[${total}].Name" class="form-control" maxlength="100" required form="fAdminWrite" />
  114. </td>
  115. <td>-</td>
  116. <td>
  117. <input type="number" name="request[${total}].Order" class="form-control" min="-999" max="999" required form="fAdminWrite" />
  118. </td>
  119. <td>
  120. <div class="form-check-inline">
  121. <input class="form-check-input" type="checkbox" id="request_${total}_IsActive" name="request[${total}].IsActive" checked form="fAdminWrite" value="true" />
  122. <label class="form-check-label" for="request_${total}_IsActive">
  123. 사용
  124. </label>
  125. </div>
  126. </td>
  127. <td>-</td>
  128. <td>-</td>
  129. <td>
  130. <button type="button" class="btn btn-danger btn-sm btn-delete">삭제</button>
  131. </td>
  132. </tr>
  133. `;
  134. $categories.append(tableRow);
  135. total++;
  136. recalculateIndices();
  137. });
  138. $(document).on("click", "button.btn-delete", function(e) {
  139. e.target.closest("tr").remove();
  140. total--;
  141. if (total <= 0) {
  142. $categories.append(
  143. `<tr><td colspan="9">No Data.</td></tr>`
  144. );
  145. total = 0;
  146. } else {
  147. recalculateIndices();
  148. }
  149. });
  150. $(document).on("click", "#btnSave", function() {
  151. if (confirm("저장 하시겠습니까?")) {
  152. let form = document.getElementById("fAdminWrite");
  153. if (form.checkValidity()) {
  154. form.submit();
  155. } else {
  156. form.reportValidity();
  157. }
  158. }
  159. return false;
  160. });
  161. function recalculateIndices() {
  162. $categories.find("tr").each(function(index, tr) {
  163. $(tr)
  164. .find("input, label")
  165. .each(function() {
  166. let name = $(this).attr("name");
  167. let id = $(this).attr("id");
  168. if (name) {
  169. $(this).attr("name", name.replace(/\[\d+\]/, `[${index}]`));
  170. }
  171. if (id) {
  172. $(this).attr("id", id.replace(/_\d+_/, `_${index}_`));
  173. }
  174. });
  175. $(tr)
  176. .find("label")
  177. .each(function() {
  178. let labelFor = $(this).attr("for");
  179. if (labelFor) {
  180. $(this).attr("for", labelFor.replace(/_\d+_/, `_${index}_`));
  181. }
  182. });
  183. });
  184. }
  185. });
  186. </script>
  187. }