Friday, October 31, 2014

jQuery deal with post data




To deal with .post in jQuery
var tutorialID = 1;
            $.post('data_process.php',
                {funct:'updateSITA',
                tutorialID:tutorialID,
                course_id:course_id,
                courses_offered_id:courses_offered_id,
                section:section,
                ta:ta,
                type:type,
                 status:status}, function(data){
                formatJson(data, "div#notice", null);
            });


In  data_process.php, we can hand data from post, i.e. $_POST
for example $_POST['funct']
if($_POST['funct'] == "updateSITA") {

jQuery, get value from cell




jQuery, get value from cell
1. Case 1
html:
 <td class='type'>car</td>
jQuery
var row = $(this).parent().parent();
var type = row.find('td.type').html();

2. Case 2
html
<td class='button1'> <input class='courses_offered_id'/></td>
jQuery
row.find('td input.courses_offered_id').val();

3. Case 3
html
 <td class='Ta'><select id="mySelect"><option value=" ">=No TA Assigned=</option></select></td>
jQuery
row.find('td.Ta select').val();

Linux, list directory size and sort




 Linux, command to list directory size and sort:, for example for directory /
 sudo du -mhc  / |sort -h 
For only total size
 sudo du -mhcs  / |sort -h 
Total disk available in G
 df -H
Return:
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_sys-lv_root
                      279G   12G  253G   5% /
tmpfs                 2.1G     0  2.1G   0% /dev/shm
/dev/sda1             500M   86M  388M  19% /boot
 

Thursday, October 30, 2014

jQuery, duplicate a row in a table and modify after click a button




For example in HTML
<div class='ajax_list'>
        <table class='template list'>

                <tr class='template worksheet-row' >
                    <td class='index' id='index'></td>
                    <td class='Term'></td>
                    <td class='Course'></td>
                    <td class='TutSection'><div type='text' name='TutorialSection' style='width:60px'/></td>
                    <td class='TaList'>list of TA</td>
               
                    <td class='EnrolMax'></td>
                    <td class='Status'></td>
                    <td class='type'></td>
                    <td class='button1'><input type='button' class='duplication' name='duplicate' value="duplicate"/>
                        <input type='button' class='SaveButton1' name='update' value="Save1"/>
                        <input type='hidden' class='tutorialID'/>
                        <input type='hidden' class='course_id'/>
                        <input type='hidden' class='courses_offered_id'/>
                    </td>
                </tr>
            </table>

</div>
To duplicate a row in a table and modify in jQuery via click duplicate button
    $('div.ajax_list input.duplication').live('click', function(){
           var $tr    = $(this).closest('.worksheet-row');
           var $clone = $tr.clone();

//Remove duplicate button in new row
           $clone.find('.duplication').remove();

//empty original content
            $clone.find('.TaList').empty();

//Now append a dropdown menu
 $clone.find('td.TaList').append('<select><option value=" ">=No Assigned</option></select>');
 $clone.find('td.TaList select').append('<option value="1">1</option>');
 $clone.find('td.TaList select').append('<option value="2">2</option>');
//Update button to Update
           $clone.find(':button').val('Update');

//Change cell value to marker
           $clone.find('.type').html('Marker');

//Set new row background color light green
           $clone.css('background-color','lightgreen');

//add new row in table
           $tr.after($clone);
      });     

  For passing the data to function,
$(selector).live(event,data,function)
Example
     $('div.ajax_list input.duplication').live('click',data, function(){

Wednesday, October 29, 2014

jQuery, clone a row via click add button in the same row




HTML table with add button
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data">
    <tr>
        <td>Name</td>
        <td>Location</td>
        <td>From</td>
        <td>To</td>
        <td>Add</td>
    </tr>
    <tr class="tr_clone">
        <td><input type="text" autofocus placeholder="who" name="who" ></td>
        <td><input type="text" autofocus placeholder="location" name="location" ></td>
        <td><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker"></td>
        <td><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker"></td>
        <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
    </tr>
</table><!-- /table#table-data -->


jQuery:
$("input.tr_clone_add").live('click', function() {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});

Friday, October 24, 2014

Using ajax in filter or search




Assume we have a form:
     <form id="filterForm">
            <input type="button" id='FilterButton' value="Filter" />
      </form>

We we click the Filter button
$(document).ready(function(){
    $('#FilterButton').click(function(){
            submitFilter();
    });


}
 submiteFilter function using ajax
function submitFilter(){
    $.post('handlesubmit.php', $("form#filterForm").serialize(), function(data) {
                formatJson(data, 'div.ajax_list', setDefaultContent);
         });
}


function setDefaultContent is used to handle the data from ajax post.
function setDefaultContent(formattedJson){

}

Thursday, October 23, 2014

Add content in dropdown menu dynamically and set its default value in jQuery




 To add content in dropdown menu and set its default value in jQuery,
1) first we have a empty selection dropdown
 <select id="mySelect"><option value=" ">=No TA Assigned=</option></select>
2) Then we have JS function  myFunction() to deal with onclick to add items to dropdown, this is very useful as we can change dropdown menu dynamically.
3) We need to prevent duplication of items  dropdown, also we can set the default value
   row.find('td.Ta select').val('2');
Complete Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<style>
table {
    border-collapse: collapse;
}
table, th, td {
    border: 1px solid black;
}
</style>
<script>
function myFunction() {
   var taList = {};
   taList={
     '1': {id:1, name:'Andy'},
     '2':{id:2,name:'Jiansen'},
     '3': {id:3, name:'Tommy'} };
   var row = $('table.template tr.template');
//add other options
    for (var id0 in taList){
        var x = document.getElementById("mySelect");
        var i, add='y';
        //prevent duplication           
        for (i = 0; i < x.length; i++) {
        if(taList[id0].id ==i) add='n'
        }   
           if(add=='y')     row.find('td.Ta select').append('<option value="'+taList[id0].id+'">'+taList[id0].name+'</option>');          
      }
//Set default value, for example Andy
    row.find('td.Ta select').val('2');
}
</script>

<body>
<h1>Add content in dropdown menu and set its default value in jQuery</h1>
 <table class='template' style="border:1" >
<tr>
   <th>Courses</th><th>Pick TA</th>
<tr>
<tr class='template'>
 <td>Physics</td>   <td class='Ta'><select id="mySelect"><option value=" ">=No TA Assigned=</option></select></td>
</tr>
</table>
<button onclick="myFunction()">Click me to add TAs TA dropdown list</button>
<p id="demo"></p>
</body>
</html>

Saturday, October 18, 2014

Java, can not load main class Mineshafter-launcher.jar



Install Java (Java(TM) Platform SE binary) from
 http://java.com/en/
Run
 java Mineshafter-launcher.jar
Return error
"can not load main class Mineshafter-launcher.jar"

run
 java -jar  Mineshafter-launcher.jar
fix the problem

For convinence, write  a window bat script such as Minecraft.bat
java -jar  Mineshafter-launcher.jar
pause


i.e open Notepad, copy above script and save as 
Minecraft.bat
Note  Minecraft.bat should be in the same location (directory) with Mineshafter-launcher.jar
click 
Minecraft.bat

Thursday, October 16, 2014

MySql, find email address




Find email address end of @sfu.ca (table mail, column email_add)
trim(mail.email_addr)  like '%@sfu.ca'
Without containing . in id part:
trim(mail.email_addr)  not like '%\.%@sfu.ca'
Extract id part of email address, ie. the string before @sfu.ca
LEFT(mail.EMAIL_ADDR, LOCATE('@', mail.EMAIL_ADDR) - 1)

MySQL, find those values not in another table




In MySQl, Find those emplid in courses_offered_has_stakeholders table, but not in stakeholders table
      SELECT DISTINCT table1.emplid
      FROM courses_offered_has_stakeholders as table1 LEFT JOIN stakeholders as table2 ON          

      table2.emplid=table1.emplid
      WHERE table2.emplid IS NULL;"

Tuesday, October 14, 2014

Linux bash, change command prompt




To change command prompt in Linux bash:
modify .bashrc in home directory.
Original .bashrc:
 # .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
After modifying .bashrc, run
bash

Wednesday, October 8, 2014

MySQL, check duplication and delete all duplication and keep one.




For example, we have names MySQl table:
SELECT * FROM names;
+----+--------+
| id | name   |
+----+--------+
| 1  | nissan |
| 2  | nissan |
| 3  |  GM    |
| 4  |  EM    |
| 5  |  GM    |
| 6  |  EM    |
+----+--------+
First we want to check duplication:
SELECT distinct name, count(name) FROM `names` group by name having count(name)>1;

Delete duplication and keep the row with the lowest id value:

DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name

 

Monday, October 6, 2014

Loading external sql files in PHP




I have a large sql file. I preferred  to separate it from PHP code.
I created a SQL directory to put these sql files. Using PHP function file_get_contents to laod
external sql files.
    $db2 = new ConnectionDB2();
    $sql = file_get_contents(SITE_SERVER_ROOT."/_models/SQL/CSRPTcourses_lut.sql");
    $result = $db2->query($sql);
    $data = array();
    while ($row = $db2->getArray($result)) {
            $data[] = $row;
        }

     $db2->close();

Friday, October 3, 2014

Unix Utils and wget in Windows




Unix Utils download in Windows: (sh.exe):
http://sourceforge.net/projects/unxutils/
wget in Windows:
http://users.ugent.be/~bpuype/wget/