Delicious Facebook RSS Feed

Count Items in special folder by EcmaScript

Method 1: best practice for only counting items

function ViewAllFiles()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
folder =web.getFolderByServerRelativeUrl(L_Menu_BaseUrl+'/Lists/{ListName}/5-2013');
context.load(folder);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success()
{
var numberCount = folder.get_itemCount();
alert(numberCount )
}
function failed()
{

alert("error");
}

Method 2: query all item in folder

function ViewAllFiles()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MenuTracking');
var query = '<View></View>';
var camlQuery = new SP.CamlQuery.createAllFoldersQuery();
camlQuery.set_viewXml(query);
camlQuery.set_folderServerRelativeUrl('Lists/{List Name}/5-2013');
allItems = list.getItems(camlQuery);
context.load(allItems);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success()
{
var numberCount = this.allItems.get_count();
alert(numberCount )
var ListEnumerator = this.allItems.getEnumerator();
 while (ListEnumerator.moveNext()) {
   var currentItem = ListEnumerator.get_current();
   var _contentType = currentItem.get_fileSystemObjectType();
   if(currentItem.get_fileSystemObjectType() == '1') {
        //FolderNames  += currentItem.get_item('Title')+ '\n';
   }
 }
}
function failed()
{
alert("error");
}



String Format for Double [C#]

Chữ số sau dấu thập phân
Ví dụ chuyển định dạng Double thành chuỗi với số cố định của dấu thập phân. Với mẫu định dạng "0.00" nếu  số lượng số sau dấu chấm: nếu ít hơn 2 các chữ số còn lại ở bên phải sẽ là zero. Nếu nó có nhiều nơi hơn 2, số lượng sẽ được thêm số zero để đủ thành 2 số sau dấu chấm

String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"
Khác với mẫu "0.00", mẫu "0.##" là có bao nhiêu hiện bấy nhiêu nhưng nhỏ hơn hoặc bằng số lượng quy định mà không tự thêm zero vào
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
Ví dụ bên dưới sẽ giúp hiểu rõ 2 kí hiệu "0" và "#"
String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // "

Digits before decimal point

String.Format("{0:0.0}", 0.0);                            // "0.0"
String.Format("{0:0.#}", 0.0);                            // "0"
String.Format("{0:#.0}", 0.0);                            // ".0"
String.Format("{0:#.#}", 0.0);                            // ""

 

 

OnClick và Href

Ví dụ 1: Rê chuột vào link sẽ chỉ là liên kết hiện tại trang đang đứng nhưng khi click vào link thì sẽ thực hiện js bất kì trong trừơng hợp này là alert().
Nhược điểm: không click chuột phải New tab hay new window dc, đường link khi rê chuột vào không thể hiện chính xác url ở thanh status nếu như sự kiện oclick là openwindow tới 1 trang khác
<a href="#" onclick="alert('ne')" >Link</a>
Kết quả:
Link

Ví dụ 2: Ở thanh trạng thái status chỉ xuất hiện dòng "javascript:alert('nè')" trong thẻ href.
Nhược điểm: không click chuột phải New tab hay new window dc.
<a href="javascript:alert('ne')"  >Link</a>
Kết quả:
Link

Ví dụ 3: Khắc phục 2 cách trên, thanh status thể hiện chính xác url khi click vào link nên click chuột phải new tab cũng dc. thêm sự kiện js xử lí khi click. event.ctrlKey là bool trả về khi nhấn phím Ctrl và window.opera trả về bool khi trình duyệt là opera.
Giải thích onclick: nếu ko nhấn phím Ctrl và trình duyệt ko phải là opera thì alert lên và vô hiệu hóa thuộc tính href bởi lệnh return false, nghĩa là click vào link xuất hiện alert nhưng ko nhảy tới trang web dc ghi trong thẻ href. Ngược lại các trường hợp khac thì nhảy tới url mô tả trong href.

<a href="http://www.blogger.com" onclick="if(!event.ctrlKey&&!window.opera){ alert("nè") ;return false;}else{return true;}" target="_blank" >Link</a>
Kết quả:
Link

CSS: hiểu về Position

 Positon co 4 thuộc tính:
  • static
  • absolute
  • relative
  • fixed
Static là thuộc tính mặc định
Static is the default position for any element on a Web page. If you don't define the position, it will be static and will display on the screen based on where it is in the HTML document and how it would display inside the normal flow of the document.
If you apply positioning rules like top or left to an element that has a static position, those rules will be ignored.
 Absolute:
This tells the browser that whatever is going to be positioned should be removed from the normal flow of the document and will be placed in an exact location on the page. It is also taken out of the normal flow of the document - it won't affect how the elements before it or after it in the HTML are positioned on the Web page. 
The four positioning properties are:
To use them, you need to think of them as offset properties. In other words, an element positioned right 2em is not moved right 2em. It's right side is offset from the right side of the window 2em. The same is true for the other three.
 Nếu dùng right với thuộc tính absolute 2em có nghĩa là khoảng cách chính xác tính từ cạnh bên phải của cửa sổ window tới bên phải của đối tượng chứ ko phải là dịch sang phải 2em
offset có thể dịch là sự dịch chuyển hoặc bù đắp, khỏang chừa trống


Relative:
Relative positioning uses the same four positioning properties as absolute positioning. But instead of basing the position of the element upon the browser view port, it starts from where the element would be if it were still in the normal flow.
 Giá trị relative dựa trên hướng đi bình thường dựa trên các thuộc tính cha nếu thuoc tính cha cũng theo hướng bình thường
Với right, left,top, bottom:

Từ  hình trên ta có thể thấy right khỏang cách bao nhiêu phụ thuộc vào vị trí ban đầu trước khi có giá trị right, khác với absolute

Absolute và Relative


Fixed:

Fixed positioning is a lot like absolute positioning. The position of the element is calculated in the same way as the absolute model - from the sides of the view port. But fixed elements are then fixed in that location, like a watermark. Everything else on the page will then scroll past that element.
Cũng giống như absolute nhưng fixed thì sẽ luôn cố định xuất hiện trên window nếu bạn scroll

Sự khác nhau absolute và relative
Vấn đề Position luôn làm đau đầu với người mới tìm hiểu về position là absolute và relative:
- Đầu tiên ta coi ví dụ sau về absolute http://www.yourhtmlsource.com/examples/positioning1.html
Source:
<html>
<body id="htmlsource">
<img src="../updates/progress/media/feature.gif" width="100" height="100" alt="Absolutely positioned image." border="1" style="position: absolute; top: 56px; right: 80px">

<img src="../updates/progress/media/feature.gif" width="100" height="100" alt="This image was placed here using css2 absolute positioning" border="1" style="position: absolute; top: 100px; left: 360px; ">

<p>See? That image to the right is <em>exactly</em> where I told it to be, in relation to this page. Oh, and I put in the border so you can see where the image edges go.</p>

<br><br><br><br><br><br>
<p>I can even obscure this text by placing an image on top of it like this. Hello? Hello?? Oh, good, I'm back. Do you see?</p>

<p>It's just a matter of trying out different values and getting the element exactly where you wanted it.</p>

</body>
</html>
Kết hợp với plugin firebug của FF để view source rõ thêm
- Giờ ta phân tích sự khác nhau cơ bản của 2 thuộc tính absolute và relative: đó là thẻ <HTML> và thẻ <BODY>
Ta sửa style hình 1 và 2 thành :
hình 1: position: relative; left: 0pt; top: 0pt;
hình 2: position: absolute; left: 0pt; top: 0pt;
Ta dc kết quả: hình 1 dc kẻ khung đỏ

Phân tích thẻ <body>:
Đưa chuột vào thẻ body dc tô nền màu xanh, còn màu vàng là margin của thẻ body (coi thuộc tính thẻ body).
ta thấy hình 1 với thuộc tính relative nằm bên trong thẻ body, trong khi hình 2 thuộc tính absolute thì dc nằm trong thẻ <html>
Với hình 2 trong thẻ html:

Kết luận:
- Thuộc tính absolute chịu ảnh hưởng đầu tiên từ thẻ html và thuộc tính relative chịu ảnh hưởng đầu tiên từ thẻ body
- Đối với thuộc tính relative: sẽ chịu ảnh hưởng trực tiếp thẻ cha của nó mà ko từ thẻ <body> nữa nếu thẻ cha của nó dc khai báo thuộc tính relative

Link đọc thêm và có ví dụ: http://www.barelyfitz.com/screencast/html-training/css/positioning/

Window size and scrolling

Chiều nay làm window scroll thì gặp vấn đề giữa IE và W3C, ta biết chuẩn IE khác biệt so với W3C, vậy nếu với dòng code sau:
document.body.scrollLeft
Sẽ chạy bình thường trong IE, nhưng vào trong html của mình có khai báo:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
Thì giá trị luôn trả về là 0. giải thích vì từ đây html của mình sẽ tuân thủ chuẩn W3C và
document.body.scrollLeft không có hỗ trợ trong W3C. 
Giải quyết với chuẩn W3C ta phải dùng :
document.documentElement.scrollLeft
Giải quyết cross browsers: nhận diện browser cụ thể để dùng hàm tương ứng
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
} 

Đọc thêm về size: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

Chèn Syntax Highlighter vào Blogger


 Hôm nay lang thang đưa bài về jquery plugin thấy editor của blogger thiếu thiếu gì đó, chèn code nó ko dc đẹp, đi tìm cái layout cho nó đẹp 1 tí, kết quả là Syntax Highlighter, cái thường thấy trên web mà ko biết là cái gì, giới thiệu mọi ng, 2 link dưới làm theo 1 trong 2 là có thể chèn code vào với layout đẹp liền à.
Syntax Highlighter phiên bản cũ : http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html
Phiên bản mới: http://www.bloggermint.com/2010/02/how-to-add-syntax-highlighter-for-bloggerblogspot/

2 trang html Escape:
http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/default.php
http://www.elliotswan.com/postable/

cập nhật 22/10/2010:
kiếm thêm dc vài bài viết hay trên mạng:
http://hontap.blogspot.com/2010/04/lam-ep-code-trong-blog-voi.html
Làm theme mới  Highlighter
 http://davidchambersdesign.com/coda-theme-for-syntaxhighlighter/