Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. $(document).ready(function () {
  2.  
  3. getTableData();
  4. getDropDown();
  5. });
  6.  
  7.  
  8.  
  9.  
  10.  
  11. function getTableData() {
  12. $.getJSON("index.php/getTableData").done(function (data) {
  13. $("#mainTable").empty();
  14. showMainTable(data);
  15. });
  16. }
  17.  
  18.  
  19.  
  20. function getDropDown() {
  21. $.getJSON("index.php/getDropDown", function (result) {
  22. var options = $("#mySelect");
  23. var options2 = $("#mySelect1");
  24. $.each(result, function () {
  25. options.append($("<option />").val(this.caretaker).text(this.caretaker));
  26. options2.append($("<option />").val(this.caretaker).text(this.caretaker));
  27. });
  28. });
  29. }
  30.  
  31.  
  32. function sortByDate(){
  33. $.getJSON("index.php/sortByDate").done(function(data){
  34. showMainTable(data);
  35. });
  36. }
  37.  
  38.  
  39. function sortByCare(){
  40. $.getJSON("index.php/sortByCare").done(function(data){
  41. showMainTable(data);
  42. });
  43. }
  44.  
  45.  
  46. function sortByDone(){
  47. $.getJSON("index.php/sortByDone").done(function(data){
  48. showMainTable(data);
  49. });
  50. }
  51.  
  52.  
  53. function getJobb() {
  54.  
  55. var index = $("#mySelect1 option:selected").text();
  56. $.getJSON("index.php/getJobb/" + index).done(function(data){
  57. showMainTable(data);
  58. });
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. function showMainTable(data) {
  66. $("main").empty();
  67.  
  68.  
  69. var mainTable = $("<table class='table table-striped table-hover'>");
  70. mainTable.attr("id", "mainTable");
  71.  
  72. var idHeader = $('<th>Fel Id</th>');
  73. var dateHeader = $('<th>Datum</th>');
  74. var wrongDescHeader = $('<th>Felbeskrivning</th>');
  75. var careHeader = $('<th>Fastighetsskötare</th>');
  76. var isDoneHeader = $('<th>Åtgjärdad</th>');
  77.  
  78. mainTable.append(idHeader);
  79. mainTable.append(dateHeader);
  80. mainTable.append(wrongDescHeader);
  81. mainTable.append(careHeader);
  82. mainTable.append(isDoneHeader);
  83.  
  84.  
  85.  
  86.  
  87. for (var i = 0; i < data.length; i++) {
  88.  
  89.  
  90. var tableRow = $("<tr>");
  91.  
  92. if (data[i].done === "1") {
  93. tableRow.attr("style", "background-color:lightgray");
  94. var radioButton = $('<input type="radio" checked>');
  95. radioButton.attr("id", "myRadio");
  96.  
  97. function updateClickHandlerOne(updateData) {
  98. radioButton.on('click', function () {
  99. updateItemNo(updateData, "0");
  100. });
  101. }
  102.  
  103. var updateData = data[i].id;
  104. updateClickHandlerOne(updateData);
  105.  
  106. }
  107.  
  108. if (data[i].done === "0") {
  109. tableRow.attr("style", "background-color:white");
  110. var radioButton = $('<input type="radio">');
  111. radioButton.attr("id", "myRadio");
  112.  
  113. function updateClickHandlerTwo(updateData) {
  114. radioButton.on('click', function () {
  115. updateItemYes(updateData, "1");
  116. });
  117. }
  118.  
  119. var updateData = data[i].id;
  120. updateClickHandlerTwo(updateData);
  121.  
  122. }
  123.  
  124. tableRow.append('<td>' + data[i].id + '</td>' +
  125. '<td>' + data[i].datum + '</td>' +
  126. '<td>' + data[i].beskrivning + '</td>' +
  127. '<td>' + data[i].caretaker + '</td>');
  128. tableRow.append(radioButton);
  129.  
  130.  
  131. tableRow.append('<br>');
  132. mainTable.append(tableRow);
  133.  
  134.  
  135.  
  136.  
  137. var addbutton = $("<button>");
  138. addbutton.html("Lägg till");
  139. addbutton.attr("id", "addButton");
  140. addbutton.on('click', function () {
  141. showAddItem();
  142. });
  143.  
  144. }
  145.  
  146. $("main").append(mainTable);
  147. $("main").append(addbutton);
  148. }
  149.  
  150. function radioValue() {
  151.  
  152. var radioValue;
  153.  
  154. if (document.getElementById('checkDis0').checked) {
  155. radioValue = "0";
  156. } else if (document.getElementById('checkDis1').checked) {
  157. radioValue = "1";
  158. }
  159. dropDownValue(radioValue);
  160.  
  161.  
  162. }
  163.  
  164.  
  165. function dropDownValue(radioValue) {
  166.  
  167. var dropDownValue;
  168. var x = document.getElementById('mySelect').selectedIndex;
  169. var y = document.getElementById('mySelect').options;
  170.  
  171. dropDownValue = y[x].text;
  172.  
  173. addItem(radioValue, dropDownValue);
  174. }
  175.  
  176.  
  177. function showAddItem() {
  178. $("#addDialog").dialog({
  179. show: 'fade', position: 'center', buttons: {"Lägg till": function () {
  180. radioValue();
  181. }}
  182. });
  183.  
  184.  
  185. }
  186.  
  187.  
  188. function addItem(radioValue, dropDownValue) {
  189. $.post("index.php/addItem/" + radioValue + "/" + dropDownValue, $("#addItemForm").serialize()).done(function () {
  190. //skriver ut all data igen
  191. getTableData();
  192. });
  193. $('#addDialog').dialog('close');
  194. }
  195.  
  196.  
  197.  
  198. function updateItemYes(id) {
  199. $.getJSON("index.php/updateItemYes/" + id).done(function () {
  200. getTableData();
  201. });
  202. }
  203.  
  204.  
  205. function updateItemNo(id) {
  206. $.getJSON("index.php/updateItemNo/" + id).done(function () {
  207. getTableData();
  208. });
  209. }
  210.  
  211. <?php
  212.  
  213. class Model {
  214.  
  215.  
  216. private $pdocon;
  217. private $dsn, $username, $password;
  218.  
  219.  
  220. function __construct() {
  221. $this->dsn = 'MY CONNECTION, THIS IS HIDDEN for safte purposes of the unversity I'm studying on';
  222. $this->username = 'db;
  223. $this->password = 'mypass';
  224. }
  225.  
  226.  
  227. private function openConnection() {
  228. try {
  229. if ($this->pdocon == NULL) {
  230. $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
  231. $this->pdocon = new PDO($this->dsn, $this->username, $this->password, $options);
  232. }
  233. } catch (PDOException $ex) {
  234. $this->pdocon = NULL;
  235. throw new Exception('DATABASFEL');
  236. }
  237. }
  238.  
  239.  
  240. public function getTableData() {
  241. $this->openConnection();
  242.  
  243. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_getTableData()');
  244.  
  245. $pdoStatement->execute();
  246.  
  247. $productArray = $pdoStatement->fetchAll();
  248.  
  249. $this->pdocon = NULL;
  250.  
  251. return $productArray;
  252. }
  253.  
  254.  
  255. public function sortByDate(){
  256. $this->openConnection();
  257.  
  258. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByDate()');
  259. $pdoStatement->execute();
  260. $productArray = $pdoStatement->fetchAll();
  261. $this->pdocon = NULL;
  262. return $productArray;
  263. }
  264.  
  265. public function sortByCare(){
  266. $this->openConnection();
  267. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByCare()');
  268. $pdoStatement->execute();
  269. $productArray = $pdoStatement->fetchAll();
  270. $this->pdocon = NULL;
  271. return $productArray;
  272. }
  273.  
  274. public function sortByDone(){
  275. $this->openConnection();
  276. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByDone()');
  277. $pdoStatement->execute();
  278. $productArray = $pdoStatement->fetchAll();
  279. $this->pdocon = NULL;
  280. return $productArray;
  281. }
  282.  
  283. public function addItem($radioValue, $dropDownData) {
  284.  
  285. $this->openConnection();
  286. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_AddItem(:datum, :beskrivning,:caretaker,:done)');
  287. $date = date("Y-m-d");
  288. $pdoStatement->bindParam(':beskrivning', filter_var($_POST['beskrivning'], FILTER_SANITIZE_STRING));
  289. $pdoStatement->bindParam(':caretaker', $dropDownData);
  290. $pdoStatement->bindParam(':datum', $date);
  291. $pdoStatement->bindParam(':done', $radioValue);
  292.  
  293. $pdoStatement->execute();
  294. $this->pdocon = NULL;
  295. }
  296.  
  297. public function getJobb($caretaker) {
  298. $this->openConnection();
  299. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_GetJobb(:caretaker)');
  300. $pdoStatement->bindParam(':caretaker', $caretaker);
  301. $pdoStatement->execute();
  302. $this->pdocon = NULL;
  303. }
  304.  
  305. public function updateItemYes($id) {
  306. $this->openConnection();
  307. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_updateItemYes(:id,:done)');
  308. $pdoStatement->bindParam(':id', $id);
  309. $radiobuttonValue = ("1");
  310. $pdoStatement->bindParam(':done', $radiobuttonValue);
  311. $pdoStatement->execute();
  312. $this->pdocon = NULL;
  313. }
  314.  
  315.  
  316. public function updateItemNo($id) {
  317. $this->openConnection();
  318.  
  319. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_updateItemNo(:id,:done)');
  320. $pdoStatement->bindParam(':id', $id);
  321. $radiobuttonValue = ("0");
  322. $pdoStatement->bindParam(':done', $radiobuttonValue);
  323. $pdoStatement->execute();
  324. $this->pdocon = NULL;
  325. }
  326.  
  327.  
  328. public function getDropDown() {
  329. try {
  330. $this->openConnection();
  331. $pdoStatement = $this->pdocon->prepare('CALL myprocedure_getDropDown()');
  332. $pdoStatement->execute();
  333. $produkter = $pdoStatement->fetchAll();
  334. $this->pdocon = NULL;
  335. return $produkter;
  336. } catch (Exception $ex) {
  337. throw new Exception('Databasfel - det gick inte att hämta databasens innehåll!');
  338. }
  339. }
  340.  
  341. }
  342.  
  343. <!DOCTYPE html>
  344.  
  345.  
  346.  
  347. <html>
  348. <head>
  349. <meta charset="UTF-8">
  350.  
  351. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  352. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  353. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  354. <script src="script.js"></script>
  355.  
  356.  
  357.  
  358. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  359. <meta name="robots" content="noindex, nofollow" />
  360. <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  361.  
  362. <link href="main.css" rel="stylesheet" type="text/css" />
  363. <!-- Bootstrap för popup fönster--><link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
  364. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
  365. <title>Testerino</title>
  366.  
  367. </head>
  368.  
  369. <body>
  370.  
  371. <div id="container">
  372.  
  373. <header>
  374.  
  375. <h4>
  376. Här kan du rapportera dina fel och även hålla koll på om dom är fixade eller inte
  377. </h4>
  378. <br>
  379. <label>En meny där du kan välja en fastighetsskötare
  380. och se dens jobb</label>
  381. <br>
  382. <select name="getJobbSelect" id="mySelect1" onchange="getJobb();">
  383. </select>
  384. <br>
  385. <a href="#" onclick="sortByDate();" >Sortera på datum</a>
  386. <br>
  387. <a href="#" onclick="sortByCare();" >Sortera på Fastighetsskötare</a>
  388. <br>
  389. <a href="#" onclick="sortByDone();" >Sortera på Åtgjärdad</a>
  390. </header>
  391.  
  392.  
  393. <div id="addDialog" style="display:none">
  394. <form id="addItemForm" action="Model.php" method="POST">
  395. <h3>Fel:</h3>
  396. <label>Felbeskrivning</label>
  397. <input type="text" name="beskrivning" placeholder="Felbeskrivning">
  398. <br>
  399. <label>Välj fastighetsskötare</label>
  400. <select name="mySelect" id="mySelect">
  401. </select>
  402. <br>
  403. <label>Åtgärdad: Nej</label>
  404. <input type="radio" name="checkDis" id="checkDis0" value="0"/>
  405. <br>
  406. <label>Åtgärdad: Ja</label>
  407. <input type="radio" name="checkDis" id="checkDis1" value="1" />
  408.  
  409. </form>
  410. </div>
  411.  
  412. </div>
  413.  
  414. <main>
  415.  
  416.  
  417. </main>
  418.  
  419. </body>
  420. </html>
  421.  
  422. <?php
  423.  
  424. include_once '../models/Model.php';
  425.  
  426.  
  427. $queryArr = explode('/', $_SERVER['PATH_INFO']);
  428.  
  429.  
  430. $model = new Model();
  431.  
  432.  
  433. if (method_exists($model, $queryArr[1])) {
  434.  
  435. switch ($queryArr[1]) {
  436.  
  437.  
  438. case 'getTableData':
  439. echo json_encode($model->$queryArr[1]());
  440. break;
  441. case 'sortByDate':
  442. echo json_encode($model->$queryArr[1]());
  443. break;
  444. case 'sortByCare':
  445. echo json_encode($model->$queryArr[1]());
  446. break;
  447. case 'sortByDone':
  448. echo json_encode($model->$queryArr[1]());
  449. break;
  450.  
  451. // ========= CRUD CRUD CRUD ============
  452. case 'addItem':
  453. echo json_encode($model->$queryArr[1]($queryArr[2],$queryArr[3]));
  454. break;
  455.  
  456. case 'updateItemYes':
  457. echo json_encode($model->$queryArr[1]($queryArr[2]));
  458. break;
  459. case 'updateItemNo':
  460. echo json_encode($model->$queryArr[1]($queryArr[2]));
  461. break;
  462. case 'getDropDown':
  463. echo json_encode($model->$queryArr[1]());
  464. break;
  465. case 'getJobb':
  466. echo json_encode($model->$queryArr[1]($queryArr[2]));
  467. break;
  468. }
  469. }
  470. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement