Thursday, December 22, 2016

jQuery Selectors

Links
http://www.w3schools.com/jquery/jquery_ref_selectors.asp

Selector Example Selects
* $("*") All elements
#id $("#lastname") The element with id="lastname"
.class $(".intro") All elements with class="intro"
.class,.class $(".intro,.demo") All elements with the class "intro" or "demo"
element $("p") All <p> elements
el1,el2,el3 $("h1,div,p") All <h1>, <div> and <p> elements
     
:first $("p:first") The first <p> element
:last $("p:last") The last <p> element
:even $("tr:even") All even <tr> elements
:odd $("tr:odd") All odd <tr> elements
     
:first-child $("p:first-child") All <p> elements that are the first child of their parent
:first-of-type $("p:first-of-type") All <p> elements that are the first <p> element of their parent
:last-child $("p:last-child") All <p> elements that are the last child of their parent
:last-of-type $("p:last-of-type") All <p> elements that are the last <p> element of their parent
:nth-child(n) $("p:nth-child(2)") All <p> elements that are the 2nd child of their parent
:nth-last-child(n) $("p:nth-last-child(2)") All <p> elements that are the 2nd child of their parent, counting from the last child
:nth-of-type(n) $("p:nth-of-type(2)") All <p> elements that are the 2nd <p> element of their parent
:nth-last-of-type(n) $("p:nth-last-of-type(2)") All <p> elements that are the 2nd <p> element of their parent, counting from the last child
:only-child $("p:only-child") All <p> elements that are the only child of their parent
:only-of-type $("p:only-of-type") All <p> elements that are the only child, of its type, of their parent
     
parent > child $("div > p") All <p> elements that are a direct child of a <div> element
parent descendant $("div p") All <p> elements that are descendants of a <div> element
element + next $("div + p") The <p> element that are next to each <div> elements
element ~ siblings $("div ~ p") All <p> elements that are siblings of a <div> element
     
:eq(index) $("ul li:eq(3)") The fourth element in a list (index starts at 0)
:gt(no) $("ul li:gt(3)") List elements with an index greater than 3
:lt(no) $("ul li:lt(3)") List elements with an index less than 3
:not(selector) $("input:not(:empty)") All input elements that are not empty
     
:header $(":header") All header elements <h1>, <h2> ...
:animated $(":animated") All animated elements
:focus $(":focus") The element that currently has focus
:contains(text) $(":contains('Hello')") All elements which contains the text "Hello"
:has(selector) $("div:has(p)") All <div> elements that have a <p> element
:empty $(":empty") All elements that are empty
:parent $(":parent") All elements that are a parent of another element
:hidden $("p:hidden") All hidden <p> elements
:visible $("table:visible") All visible tables
:root $(":root") The document's root element
:lang(language) $("p:lang(de)") All <p> elements with a lang attribute value starting with "de"
     
[attribute] $("[href]") All elements with a href attribute
[attribute=value] $("[href='default.htm']") All elements with a href attribute value equal to "default.htm"
[attribute!=value] $("[href!='default.htm']") All elements with a href attribute value not equal to "default.htm"
[attribute$=value] $("[href$='.jpg']") All elements with a href attribute value ending with ".jpg"
[attribute|=value] $("[title|='Tomorrow']") All elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen
[attribute^=value] $("[title^='Tom']") All elements with a title attribute value starting with "Tom"
[attribute~=value] $("[title~='hello']") All elements with a title attribute value containing the specific word "hello"
[attribute*=value] $("[title*='hello']") All elements with a title attribute value containing the word "hello"
     
:input $(":input") All input elements
:text $(":text") All input elements with type="text"
:password $(":password") All input elements with type="password"
:radio $(":radio") All input elements with type="radio"
:checkbox $(":checkbox") All input elements with type="checkbox"
:submit $(":submit") All input elements with type="submit"
:reset $(":reset") All input elements with type="reset"
:button $(":button") All input elements with type="button"
:image $(":image") All input elements with type="image"
:file $(":file") All input elements with type="file"
:enabled $(":enabled") All enabled input elements
:disabled $(":disabled") All disabled input elements
:selected $(":selected") All selected input elements
:checked $(":checked") All checked input elements

Friday, October 28, 2016

How to force HTML table cell as text in Excel

<td> style to force the cell as text: mso-number-format:"\@";
<td> style to force the cell as number: mso-number-format:General;

Code Behind(cs) file
        public PartialViewResult ExcelFile()
        {
            StringBuilder table = new StringBuilder();
            table.AppendLine("<table>");
            table.AppendLine("<thead>");
            table.AppendLine("<tr>");
            table.AppendLine("<th>Date</th>");
            table.AppendLine("<th>Store</th>");
            table.AppendLine("</tr>");
            table.AppendLine("</thead>");

            table.AppendLine("<tbody>");
            table.AppendLine("<tr>");
            table.Append("<td>10/27/2016</td>");
            table.Append("<td style='mso-number-format:\"\\@\"'>0001</td>");
            table.AppendLine("</tr>");
            table.AppendLine("<tr>");
            table.Append("<td>10/27/2016</td>");
            table.Append("<td style='mso-number-format:\"\\@\"'>0002</td>");
            table.AppendLine("</tr>");
            table.AppendLine("</tbody>");
            table.AppendLine("</table>");

            Response.ContentType = "application/force-download";
            Response.AddHeader("content-disposition", "attachment; filename=ExcelFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
            Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            Response.Write("<head>");
            Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            Response.Write("<!--[if gte mso 9]><xml>");
            Response.Write("<x:ExcelWorkbook>");
            Response.Write("<x:ExcelWorksheets>");
            Response.Write("<x:ExcelWorksheet>");
            Response.Write("<x:Name>ExcelFile</x:Name>");
            Response.Write("<x:WorksheetOptions>");
            Response.Write("<x:Print>");
            Response.Write("<x:ValidPrinterInfo/>");
            Response.Write("</x:Print>");
            Response.Write("</x:WorksheetOptions>");
            Response.Write("</x:ExcelWorksheet>");
            Response.Write("</x:ExcelWorksheets>");
            Response.Write("</x:ExcelWorkbook>");
            Response.Write("</xml>");
            Response.Write("<![endif]--> ");
            Response.Write("</head>");
            Response.Write(table.ToString());
            Response.Flush();

            return PartialView();
        }

Wednesday, April 6, 2016

How to launch the Android emulator from the command line?

1. Go to SDK folder
Go to android sdk folder in which "android.bat" and "emulator.exe" are located.
ex) Default folder on Windows: C:\Users\{user}\AppData\Local\Android\sdk\tools

2. Run AVD manager
Type in "android avd" in the command prompt to launch the Android Virtual Devices manager.

3. Create AVD
Create a AVD by clicking "Create..." button.
Or you can just use the one already created from the list.

4. Launch emulator
Run the AVD either by using command "emulator -avd <name>" or by clicking "Start..." button on the AVD manager.

Tuesday, April 5, 2016

How to install app in Android emulator?

1. Launch emulator
Go to here to see how to launch Android Emulator.

2. Copy the apk file to platform-tools folder
ex) Default folder on Windows: C:\Users\{user}\AppData\Local\Android\sdk\platform-tools

3. Go to command prompt
Type "adb install filename.apk" to install
Type "adb install -r filename.apk" to re-install

4. Run emulator then you will see the app is installed.

Friday, March 11, 2016

How to pass javascript array to C# code behind

Used jquery version: jquery-1.8.3.min.js

html(cshtml) file
function updateNotification() {
    var ids = [1, 2, 3, 4, 5];

    if (ids.length > 0) {
        $.ajax({
            traditional: true,             // Should be true
            type: "POST",
            url: "/Notification/Update",   // Url to pass
            data: { ids: ids },
            dataType: "json",
            success: function (result) {
            },
            error: function (result) {
            }
        });
    }
}

Code Behind(cs) file
        [HttpPost]
        public JsonResult Update(int[] ids)
        {
            // ids is int array

            return Json(new { result = true });
        }