Guest User

Untitled

a guest
Sep 4th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*EXAMPLE OUTPUT of dyn_loader.php for the workertable
  2.  
  3. {"meta":{"header":{"uid":"ID","barcode":"Strichcode","name":"Name"},"count":"1001","display":10},"data":[{"uid":"50","barcode":"PS25HYM6","name":"TLWJZYSr5OqeN1W"},{"uid":"51","barcode":"xcOflSSI","name":"7Wt9PZiWW6zTrS1"},{"uid":"52","barcode":"nLoQeUhN","name":"MkB3TZXmTnrWErE"},{"uid":"53","barcode":"vtFGge18","name":"WgX5Kps8q0ZnbUw"},{"uid":"54","barcode":"pJsvjhvW","name":"HznhEOWtLJv18xX"},{"uid":"55","barcode":"TmWBwwbj","name":"UeXpwyvKj7wFKZg"},{"uid":"56","barcode":"1OMp8Yzb","name":"ScXkzW334bq4Iob"},{"uid":"57","barcode":"BmyUmk6H","name":"zt1aX9NnIbnWBVe"},{"uid":"58","barcode":"XNdk03p9","name":"azlF43mkNKj6Vnw"},{"uid":"59","barcode":"uy7RqiJE","name":"oeqJmWUtMxpHECD"}]}
  4.  
  5. */
  6.  
  7. /*The purpose of this is to build or various website elements given a set of data */
  8.  
  9. //we assume we already got jquery right?
  10.  
  11. function revertEdit()
  12. {
  13.     //GOAL: Transform ALL edit rows back to their normal state, in theory there should be always only one, but who knows..
  14.  
  15.     $('.edit_row').each(function() {
  16.         var set = $(this).children('td');
  17.         var length = set.length;
  18.         //$row.empty();
  19.         $(this).removeClass();
  20.         set.each(function (index) {
  21.             if( index != (length - 1) )
  22.             {
  23.                 $(this).html($(this).children().attr('value'));
  24.                 //var $cell = $('<td id="'+$(this).attr('id')+'">').$(this).attr('value');
  25.                 //$inputfield.append($('<input type="text" name="'+$(this).attr('id')+'" value="'+$(this).text()+'">'));
  26.                 //$row.append($cell);
  27.             }
  28.             else{
  29.                 $(this).empty();
  30.                 $(this).append($('<input type="button" uid="'+$(this).parent().attr('id')+'" id="editbutton" value="edit">'));
  31.             }
  32.          });
  33.     });
  34. }
  35.  
  36. $(document).ready(function(){
  37.     //Cancels ALL active Edit fields (those should usually be exactly one)
  38.     $(document).on('click', "#tablecancelbutton", function(){
  39.         revertEdit();
  40.     });
  41.     //=======================================================================
  42.     //CLOSE FIELD END
  43.     //=======================================================================
  44.  
  45.     //changes the DOM of the given ROW to an edit field set.
  46.     $(document).on('click', "#editbutton", function(){
  47.    
  48.     revertEdit();
  49.     //temp vars    
  50.     var ele_id = $(this).parent().parent().attr("id");
  51.     var type = $(this).parent().parent().parent().attr("id");
  52.     var $row = $(this).parent().parent(); //tr>td>button
  53.     //turns out, form cannot be the child of a table, i did in fact not knew that, fun times
  54.     //var $form = $('<form action="dyn_putter.php" method="post">');
  55.  
  56.     var set = $row.children('td');
  57.     var length = set.length;
  58.     $row.empty();
  59.     $row.attr('class', 'edit_row');
  60.     set.each(function (index) {
  61.         if( index != (length - 1) )
  62.         {
  63.             var $inputfield = $('<td id="'+$(this).attr('id')+'">');
  64.             $inputfield.append($('<input type="text" name="'+$(this).attr('id')+'" value="'+$(this).text()+'">'));
  65.             $row.append($inputfield);
  66.         }
  67.         else
  68.         {
  69.             var $inputfield = $('<td class="control">');
  70.             $inputfield.append($('<input type="button" value="OK" id="tablepostbutton">'));
  71.             $inputfield.append($('<input type="button" value="Cancel" id="tablecancelbutton">'));
  72.             $row.append($inputfield);
  73.         }
  74.      });
  75.     });
  76.     //=======================================================================
  77.     //EDIT FIELD END
  78.     //=======================================================================
  79.  
  80.     //requests change
  81.     $(document).on('click', "#tablepostbutton", function(){
  82.         //i have two choices here, either i do the lazy way and just take all input fields
  83.         //on the page or i do the slightly more sane way...yes i know, js files are public
  84.         var dataset = {status: "test", information = "zero"};
  85.         $.post("dyn_writer.php", dataset)
  86.          .done(function(data) {
  87.             console.log(data);
  88.          });
  89.     });
  90.     //=======================================================================
  91.     //POST END
  92.     //=======================================================================
  93. });
  94.  
  95. //turns out i could have done this as an onload event of the document,
  96. //refactoring could be useful.
  97. function buildDTable(type, element,lbound, queries)
  98. {
  99.     $('#'+element).html('<div align="center"><img src="pics\\loader_red.apng" width="64px" height="64px"></div>');
  100.     var query = "dyn_loader.php?dataid=" + type + "&lbound="+lbound+"&queries="+queries;
  101.     $.getJSON(query, function( data ) {
  102.  
  103.         $('#'+element).empty();
  104.         var $table = $("<table class=\"dataset\" id=\""+type+"\">");
  105.  
  106.         var $trhead = $('<tr class=\"header\">');  
  107.  
  108.         var header = data['meta']['header'];
  109.         //the mixture of basic javascript and jquery is a bit annoying and probably not the best performance wise..but it works.
  110.         //it would probably work with foreach() or Object.keys() but this is the solution i found first and i pray to fortuna that his never becomes a problem
  111.         //console.log(data);
  112.         $.each(header, function(index, entry)
  113.         {
  114.             $trhead.append($('<td>').text(entry));
  115.         });
  116.  
  117.         //header gets attached
  118.         $($table).append($trhead);
  119.  
  120.         data['data'].forEach(function(entry)
  121.         {
  122.             //edit variable for later use
  123.             if (typeof entry['uid'] !== 'undefined') {
  124.                 var $tr= $('<tr id="'+entry['uid']+'">');
  125.             } else { var $tr= $('<tr>');}
  126.  
  127.             for( var key in header)
  128.             {
  129.                 $tr.append($('<td id="'+key+'">').text(entry[key]));
  130.             }      
  131.  
  132.             //if an identifier exist offer edit/delete buttons
  133.             if (typeof entry['uid'] !== 'undefined') {
  134.                 $tr.append($('<td class="control">').append($('<input type="button" uid="'+entry['uid']+'" id="editbutton" value="edit">')));
  135.             }
  136.  
  137.             $table.append($tr);
  138.         });
  139.         //in case there is more data than what is supposed to be displayed per page:  
  140.         //convert to number
  141.         $('#'+element).append($table);
  142.  
  143.         //turns out i cannot do pagination pretty, that sucks
  144.         var count =  parseInt(data['meta']['count']);
  145.         var display = parseInt(data['meta']['display']);
  146.         if( count > display )
  147.         {
  148.             console.log("Pagination needed");
  149.             if( count > display *10)
  150.             {
  151.                 //in case there is more data than 10 Pages
  152.  
  153.             }
  154.             else
  155.             {
  156.                
  157.             }
  158.             var $pages = $('<tr class=\"pagination\">');
  159.                    
  160.             for(var i = 0; i < 10; i++)
  161.             {
  162.                 if( Math.round(lbound / display) == i){
  163.                     $pages.append($('<td class="current">').text(i+1));
  164.                 }
  165.                 else{
  166.                     $pages.append($('<td onClick="buildDTable(\'workertable\',\'workers\', '+display*(i)+', '+display+');">').text(i+1));
  167.                 }
  168.             }
  169.             $('#'+element).append($('<table>').append($pages));
  170.             //==================================================================
  171.             //TODO: Build function for Datasets bigger than 100
  172.             //==================================================================
  173.         }  
  174.     });
  175.    
  176. }
Advertisement
Add Comment
Please, Sign In to add comment