table.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. data="";
  2. function creatTable(){
  3. //這個函式的引數可以是從後臺傳過來的也可以是從其他任何地方傳過來的
  4. //這裡我假設這個data是一個長度為5的字串陣列 我要把他放在表格的一行裡面,分成五列
  5. //alert("eee");
  6. var tableData = '<div class="card"><div style="padding:15px;"><table style="font-size: 16px; border-color:#4c6caa; border-top:1px solid #4c6caa;" id="system-table" class="table"><thead style="background:#e4f8ff; color:#4c6caa;"><tr><th class="border-0">單位分類</th><th class="border-0">使用者分類</th><th class="border-0">帳號</th><th class="border-0">電子郵件</th><th class="border-0">權限設定</th><th class="border-0">備註</th><th class="border-0">+新增</th></tr></thead><tbody></div>'
  7. //動態增加5個td,並且把data陣列的五個值賦給每個td
  8. // for(var i=0;i < data.length;i++){
  9. // tableData += "<td>" + data[i] + "</td>"
  10. // }
  11. for(var i = 0; i < data.length; i++) {
  12. tableData += "<tr class='bg-transparent'>";
  13. str = "";
  14. str2="";
  15. if(data[i].roleType==1){
  16. str = "第一層(集團)";
  17. }
  18. else if(data[i].roleType==2){
  19. str = "第二層(公司)";
  20. }
  21. else if(data[i].roleType==3){
  22. str = "第三層(廠區)";
  23. }
  24. else{
  25. str = "第四層(課)";
  26. }
  27. if(data[i].isAdmin==1){
  28. str2 = data[i].role_name;
  29. }
  30. else{
  31. str2 = "檢視者";
  32. }
  33. tableData += "<td style='vertical-align:middle;'>" + str + "</td>" +
  34. "<td style='vertical-align:middle;'>" + str2 + "</td>" +
  35. "<td style='vertical-align:middle;'>" + data[i].username + "</td>" +
  36. "<td></td>" +
  37. "<td><button style='padding:5px 10px; background:#002e86; color:#fff; border: 1px solid #002E86; border-radius: 10px; font-size:14px;' class='tower-btn my-3' onclick=\"member_auth(\'"+data[i].username+"\');\"><i style='color:#fff;' class='fas fa-cog me-1'></i>設定</button></td>"+
  38. "<td></td>" +
  39. "<td></td>" ;
  40. tableData += "</tr>";
  41. }
  42. tableData+= "</tbody></table></div>";
  43. //現在tableData已經生成好了,把他賦值給上面的tbody
  44. $("#info_table").html(tableData);
  45. }
  46. // function member_auth(username){
  47. // $("#info_table").html("");
  48. // inner = "<p>請設定以下權限:</p><br>";
  49. // inner += "<table class='table table-borderless' id = \"change\"> <tr> <th>帳號</th><th>管理員</th><th>可用</th></tr>" +
  50. // " <tr><td >" + username + "</td><td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isAdmin\" >"+ " </td><td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isEnable\" ></td></tr></table>";
  51. // inner += "<br><button type='button' class='btn btn-primary me-3' onclick=\"put();\">確認 </button>" + "<button type='button' class='btn btn-primary me-3' onclick=\"start();\">取消 </button>"
  52. // $("#member_auth").html(inner);
  53. // console.log($("#isAdmin").prop('checked'));
  54. // }
  55. function member_auth(username){
  56. $("#info_table").html("");
  57. inner = "\
  58. <div class='card mt-0'>\
  59. <h1 style='color:#002e86'>請設定以下權限:</h1>\
  60. <div style='font-size: 16px;' class='py-3'>\
  61. <div style='padding:15px'>\
  62. <table style='font-size: 16px; border-color:#4c6caa; border-top:1px solid #4c6caa;' class='table' id = \"change\"><thead style='background:#e4f8ff; color:#4c6caa;'><tr><th>帳號</th><th>管理員</th><th>可用</th></tr></thead>\
  63. <tr>\
  64. <td >" + username + "</td>\
  65. <td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isAdmin\" >"+ " </td>\
  66. <td><input class='form-check-input ms-3' type='checkbox' type=\"checkbox\" id=\"isEnable\" ></td>\
  67. </tr>\
  68. </table>\
  69. </div>\
  70. <div style='padding:15px;' class='mt-4 pb-3'>\
  71. <button type='button' style='background:#002e86; color:#fff; border: 1px solid #002E86; border-radius: 10px;' class='btn text-white me-2' onclick=\"put();\">確認 </button>\
  72. <button type='button' style='background:#002e86; color:#fff; border: 1px solid #002E86; border-radius: 10px;' class='btn text-white' onclick=\"start();\">取消 </button>\
  73. </div>\
  74. </div>\
  75. </div>\
  76. ";
  77. $("#member_auth").html(inner);
  78. console.log($("#isAdmin").prop('checked'));
  79. }
  80. function put(){
  81. var xhr2 = new XMLHttpRequest();
  82. var url2 = "member/edit/?name=" + document.getElementById("change").rows[1].cells[0].innerHTML;
  83. if($("#isAdmin").prop('checked')){
  84. url2 += "&isAdmin=1" ;
  85. }
  86. else{
  87. url2 += "&isAdmin=0" ;
  88. }
  89. if($("#isEnable").prop('checked')){
  90. url2 += "&isEnable=1" ;
  91. }
  92. else{
  93. url2 += "&isEnable=0" ;
  94. }
  95. xhr2.open("GET",url2);
  96. xhr2.onload = function(){
  97. obj = JSON.parse(xhr2.responseText);
  98. alert(obj[0].msg);
  99. start();
  100. $("#member_auth").html("");
  101. //insertHTML();
  102. };
  103. xhr2.send();
  104. }
  105. //alert("response");
  106. start();
  107. // 之後透過 fetch 取得fastapi後端資料!
  108. function start(){
  109. var xhr = new XMLHttpRequest();
  110. var url = "member";
  111. obj = "";
  112. xhr.open("GET",url);
  113. xhr.onload = function(){
  114. obj = JSON.parse(xhr.responseText);
  115. data = obj ;
  116. creatTable();
  117. //insertHTML();
  118. };
  119. xhr.send();
  120. $("#member_auth").html("");
  121. console.log('Table data 寫入!');
  122. }
  123. // function toggless(){
  124. // $(this).addClass("tower-active");
  125. // $(".tower-active").siblings().removeClass('tower-active');
  126. // }
  127. $('.system-btn').click(function() {
  128. $(this).addClass("tower-active");
  129. $(".tower-active").siblings().removeClass('tower-active');
  130. });