Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Tuesday, September 27, 2016

Convert All Table TD data into Titlecase with 1 line of code

Lets say.. you receive input from server and that input is unstructured. No proper title case. 


Just like this. 


And You want like this (in just one click or unload )



Here is the code (Copy link is below the screenshot)



Download Full Code From Here

Tuesday, September 20, 2016

How to check a value exists in javascript array

Lets say user send a input "abcd" or "123". Now i want to check that incoming input should be  valid as assign in javascript array.

Like this -:



And you want output like this



var validFormatsArray = ['varchar','email','int','phone','sudhir'];
var isValidFormat = $.inArray('int'.toLowerCase(), validFormatsArray);

if(isValidFormat == -1){
  alert('Invalid format detected');
}else{
  alert('valid format');
}

Checkout this video tutorial







Also Read-:

Top most 15 haunted places in india which will dread your body



Thursday, September 15, 2016

Export Html Table into Excel using Javascript in 2 Minute

Hey guys, Want to export your small/Large Table data with row header in excel.
Also, you want to put some color in first row. then you are in right place.

This code (few lines of code ) will export the same.

No rocket science required.
No external setup
Only Vanilla JavaScript

----------------------------------------------------------


HTML-:

  <table id="exTable" border="1">
    <thead class="lockedRecordsBg">
        <tr>
            <th>S#</th>
            <th>name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Country</th>
    </thead>
    <tbody>
        <tr>
            <td rel="client">1</td>
            <td rel="regionName">Sudhir K Gupta</td>
            <td rel="date">sudhirgupta.456@gmail.com</td>
            <td rel="shift">+91 89********</td> 
            <td rel="shift">India</td>             
        </tr>
        <tr>
            <td rel="client">2</td>
            <td rel="regionName">Sudhir K Gupta</td>
            <td rel="date">test@gmail.com</td>
            <td rel="shift">+91 89********</td> 
            <td rel="shift">USA</td>             
        </tr>
    </tbody>
</table> 


Javascript-:

<script>
function exportToExcel(tableID){
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6' style='height: 75px; text-align: center; width: 250px'>";
    var textRange; var j=0;
    tab = document.getElementById(tableID); // id of table

    for(j = 0 ; j < tab.rows.length ; j++)
    {

        tab_text=tab_text;

        tab_text=tab_text+tab.rows[j].innerHTML.toUpperCase()+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text= tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); //remove if u want images in your table
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); //remove input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer

    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write( 'sep=,\r\n' + tab_text);
        txtArea1.document.close();
        txtArea1.focus();
        sa=txtArea1.document.execCommand("SaveAs",true,"sudhir123.txt");
    }

    else {
       sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
    }
    
    return (sa);
}
</script>

Now Put button below the script (must) or put jquery window.load function for this click event.
otherwise it will not work.

<input type="button" value="export" onclick="exportToExcel('exTable')" />




Download Code From Here


Ads.


Know top 10 most interesting and awesome YouTube fact

Do you know why are headphones printed L and R

job doesn’t define how good human being you are

5 funny and interesting fact about your jeans






Author-

sudhir k gupta | sudhir gupta | sudhir kumar gupta | eghara
sudhir k gupta | sudhir gupta | sudhir kumar gupta | eghara