Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- getTableData();
- getDropDown();
- });
- function getTableData() {
- $.getJSON("index.php/getTableData").done(function (data) {
- $("#mainTable").empty();
- showMainTable(data);
- });
- }
- function getDropDown() {
- $.getJSON("index.php/getDropDown", function (result) {
- var options = $("#mySelect");
- var options2 = $("#mySelect1");
- $.each(result, function () {
- options.append($("<option />").val(this.caretaker).text(this.caretaker));
- options2.append($("<option />").val(this.caretaker).text(this.caretaker));
- });
- });
- }
- function sortByDate(){
- $.getJSON("index.php/sortByDate").done(function(data){
- showMainTable(data);
- });
- }
- function sortByCare(){
- $.getJSON("index.php/sortByCare").done(function(data){
- showMainTable(data);
- });
- }
- function sortByDone(){
- $.getJSON("index.php/sortByDone").done(function(data){
- showMainTable(data);
- });
- }
- function getJobb() {
- var index = $("#mySelect1 option:selected").text();
- $.getJSON("index.php/getJobb/" + index).done(function(data){
- showMainTable(data);
- });
- }
- function showMainTable(data) {
- $("main").empty();
- var mainTable = $("<table class='table table-striped table-hover'>");
- mainTable.attr("id", "mainTable");
- var idHeader = $('<th>Fel Id</th>');
- var dateHeader = $('<th>Datum</th>');
- var wrongDescHeader = $('<th>Felbeskrivning</th>');
- var careHeader = $('<th>Fastighetsskötare</th>');
- var isDoneHeader = $('<th>Åtgjärdad</th>');
- mainTable.append(idHeader);
- mainTable.append(dateHeader);
- mainTable.append(wrongDescHeader);
- mainTable.append(careHeader);
- mainTable.append(isDoneHeader);
- for (var i = 0; i < data.length; i++) {
- var tableRow = $("<tr>");
- if (data[i].done === "1") {
- tableRow.attr("style", "background-color:lightgray");
- var radioButton = $('<input type="radio" checked>');
- radioButton.attr("id", "myRadio");
- function updateClickHandlerOne(updateData) {
- radioButton.on('click', function () {
- updateItemNo(updateData, "0");
- });
- }
- var updateData = data[i].id;
- updateClickHandlerOne(updateData);
- }
- if (data[i].done === "0") {
- tableRow.attr("style", "background-color:white");
- var radioButton = $('<input type="radio">');
- radioButton.attr("id", "myRadio");
- function updateClickHandlerTwo(updateData) {
- radioButton.on('click', function () {
- updateItemYes(updateData, "1");
- });
- }
- var updateData = data[i].id;
- updateClickHandlerTwo(updateData);
- }
- tableRow.append('<td>' + data[i].id + '</td>' +
- '<td>' + data[i].datum + '</td>' +
- '<td>' + data[i].beskrivning + '</td>' +
- '<td>' + data[i].caretaker + '</td>');
- tableRow.append(radioButton);
- tableRow.append('<br>');
- mainTable.append(tableRow);
- var addbutton = $("<button>");
- addbutton.html("Lägg till");
- addbutton.attr("id", "addButton");
- addbutton.on('click', function () {
- showAddItem();
- });
- }
- $("main").append(mainTable);
- $("main").append(addbutton);
- }
- function radioValue() {
- var radioValue;
- if (document.getElementById('checkDis0').checked) {
- radioValue = "0";
- } else if (document.getElementById('checkDis1').checked) {
- radioValue = "1";
- }
- dropDownValue(radioValue);
- }
- function dropDownValue(radioValue) {
- var dropDownValue;
- var x = document.getElementById('mySelect').selectedIndex;
- var y = document.getElementById('mySelect').options;
- dropDownValue = y[x].text;
- addItem(radioValue, dropDownValue);
- }
- function showAddItem() {
- $("#addDialog").dialog({
- show: 'fade', position: 'center', buttons: {"Lägg till": function () {
- radioValue();
- }}
- });
- }
- function addItem(radioValue, dropDownValue) {
- $.post("index.php/addItem/" + radioValue + "/" + dropDownValue, $("#addItemForm").serialize()).done(function () {
- //skriver ut all data igen
- getTableData();
- });
- $('#addDialog').dialog('close');
- }
- function updateItemYes(id) {
- $.getJSON("index.php/updateItemYes/" + id).done(function () {
- getTableData();
- });
- }
- function updateItemNo(id) {
- $.getJSON("index.php/updateItemNo/" + id).done(function () {
- getTableData();
- });
- }
- <?php
- class Model {
- private $pdocon;
- private $dsn, $username, $password;
- function __construct() {
- $this->dsn = 'MY CONNECTION, THIS IS HIDDEN for safte purposes of the unversity I'm studying on';
- $this->username = 'db;
- $this->password = 'mypass';
- }
- private function openConnection() {
- try {
- if ($this->pdocon == NULL) {
- $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
- $this->pdocon = new PDO($this->dsn, $this->username, $this->password, $options);
- }
- } catch (PDOException $ex) {
- $this->pdocon = NULL;
- throw new Exception('DATABASFEL');
- }
- }
- public function getTableData() {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_getTableData()');
- $pdoStatement->execute();
- $productArray = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $productArray;
- }
- public function sortByDate(){
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByDate()');
- $pdoStatement->execute();
- $productArray = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $productArray;
- }
- public function sortByCare(){
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByCare()');
- $pdoStatement->execute();
- $productArray = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $productArray;
- }
- public function sortByDone(){
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_sortByDone()');
- $pdoStatement->execute();
- $productArray = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $productArray;
- }
- public function addItem($radioValue, $dropDownData) {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_AddItem(:datum, :beskrivning,:caretaker,:done)');
- $date = date("Y-m-d");
- $pdoStatement->bindParam(':beskrivning', filter_var($_POST['beskrivning'], FILTER_SANITIZE_STRING));
- $pdoStatement->bindParam(':caretaker', $dropDownData);
- $pdoStatement->bindParam(':datum', $date);
- $pdoStatement->bindParam(':done', $radioValue);
- $pdoStatement->execute();
- $this->pdocon = NULL;
- }
- public function getJobb($caretaker) {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_GetJobb(:caretaker)');
- $pdoStatement->bindParam(':caretaker', $caretaker);
- $pdoStatement->execute();
- $this->pdocon = NULL;
- }
- public function updateItemYes($id) {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_updateItemYes(:id,:done)');
- $pdoStatement->bindParam(':id', $id);
- $radiobuttonValue = ("1");
- $pdoStatement->bindParam(':done', $radiobuttonValue);
- $pdoStatement->execute();
- $this->pdocon = NULL;
- }
- public function updateItemNo($id) {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_updateItemNo(:id,:done)');
- $pdoStatement->bindParam(':id', $id);
- $radiobuttonValue = ("0");
- $pdoStatement->bindParam(':done', $radiobuttonValue);
- $pdoStatement->execute();
- $this->pdocon = NULL;
- }
- public function getDropDown() {
- try {
- $this->openConnection();
- $pdoStatement = $this->pdocon->prepare('CALL myprocedure_getDropDown()');
- $pdoStatement->execute();
- $produkter = $pdoStatement->fetchAll();
- $this->pdocon = NULL;
- return $produkter;
- } catch (Exception $ex) {
- throw new Exception('Databasfel - det gick inte att hämta databasens innehåll!');
- }
- }
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <script src="http://code.jquery.com/jquery-latest.min.js"></script>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
- <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
- <script src="script.js"></script>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <meta name="robots" content="noindex, nofollow" />
- <meta name="viewport" content="width=device-width,initial-scale=1.0" />
- <link href="main.css" rel="stylesheet" type="text/css" />
- <!-- Bootstrap för popup fönster--><link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
- <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
- <title>Testerino</title>
- </head>
- <body>
- <div id="container">
- <header>
- <h4>
- Här kan du rapportera dina fel och även hålla koll på om dom är fixade eller inte
- </h4>
- <br>
- <label>En meny där du kan välja en fastighetsskötare
- och se dens jobb</label>
- <br>
- <select name="getJobbSelect" id="mySelect1" onchange="getJobb();">
- </select>
- <br>
- <a href="#" onclick="sortByDate();" >Sortera på datum</a>
- <br>
- <a href="#" onclick="sortByCare();" >Sortera på Fastighetsskötare</a>
- <br>
- <a href="#" onclick="sortByDone();" >Sortera på Åtgjärdad</a>
- </header>
- <div id="addDialog" style="display:none">
- <form id="addItemForm" action="Model.php" method="POST">
- <h3>Fel:</h3>
- <label>Felbeskrivning</label>
- <input type="text" name="beskrivning" placeholder="Felbeskrivning">
- <br>
- <label>Välj fastighetsskötare</label>
- <select name="mySelect" id="mySelect">
- </select>
- <br>
- <label>Åtgärdad: Nej</label>
- <input type="radio" name="checkDis" id="checkDis0" value="0"/>
- <br>
- <label>Åtgärdad: Ja</label>
- <input type="radio" name="checkDis" id="checkDis1" value="1" />
- </form>
- </div>
- </div>
- <main>
- </main>
- </body>
- </html>
- <?php
- include_once '../models/Model.php';
- $queryArr = explode('/', $_SERVER['PATH_INFO']);
- $model = new Model();
- if (method_exists($model, $queryArr[1])) {
- switch ($queryArr[1]) {
- case 'getTableData':
- echo json_encode($model->$queryArr[1]());
- break;
- case 'sortByDate':
- echo json_encode($model->$queryArr[1]());
- break;
- case 'sortByCare':
- echo json_encode($model->$queryArr[1]());
- break;
- case 'sortByDone':
- echo json_encode($model->$queryArr[1]());
- break;
- // ========= CRUD CRUD CRUD ============
- case 'addItem':
- echo json_encode($model->$queryArr[1]($queryArr[2],$queryArr[3]));
- break;
- case 'updateItemYes':
- echo json_encode($model->$queryArr[1]($queryArr[2]));
- break;
- case 'updateItemNo':
- echo json_encode($model->$queryArr[1]($queryArr[2]));
- break;
- case 'getDropDown':
- echo json_encode($model->$queryArr[1]());
- break;
- case 'getJobb':
- echo json_encode($model->$queryArr[1]($queryArr[2]));
- break;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement