Position.cshtml 7.7 KB

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