<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaChen on JavaJavaScript</title>
	<atom:link href="http://www.javachen.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javachen.com</link>
	<description>Just some sharing about Java open source and life</description>
	<lastBuildDate>Mon, 06 Feb 2012 12:16:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Export DhtmlxGrid to PDF in Java</title>
		<link>http://www.javachen.com/2011/08/export-dhtmlxgrid-to-pdf-in-java/</link>
		<comments>http://www.javachen.com/2011/08/export-dhtmlxgrid-to-pdf-in-java/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 13:00:56 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dhtmlx]]></category>
		<category><![CDATA[DhtmlxGrid]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=2384</guid>
		<description><![CDATA[将DhtmlxGrid数据导出到pdf这是很常见的需求，dhtmlx官网提供了php和java版本的例子，你可以去官网查看这篇文章《Grid-to-Excel, Grid-to-PDF Available for Java》，你可以从以下地址下载导出程序源码： Export to Excel Export to PDF 当然，还有一个示例工程： .zip archive with an example XML2PDF和XML2Excel工程内代码很相似，XML2PDF内部使用了PDFjet.jar导出PDF，而XML2Excel使用JXL导出Excel。 需要说明的是，还需要引入dhtmlxgrid_export.js文件，该文件是导出grid的js源码，主要用于将表格数据，包括表头、样式等，序列化为xml字符串，然后模拟一个Form表单提交数据。 将上面三个工程导入到一个工程然后打开sample.html页面，效果如下： 点击Get as PDF按钮，你会发现会打开一个新的窗口，然后页面什么都没有，而eclipse控制台报空指针异常。异常的主要原因在于下段代码：。 ?View Code JAVA1 2 3 4 5 6 7 8 9 10 11 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance &#40;&#41;; DocumentBuilder db = dbf.newDocumentBuilder&#40;&#41;; Document dom = null; try &#123; dom = db.parse&#40;new InputSource&#40;new [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="DhtmlxGrid Quick Start Guide" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/19/18117829.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">DhtmlxGrid Quick Start Guide</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Drag a item to dhtmlxGrid and add a column" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-a-item-to-dhtmlxgrid-and-add-a-column%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/25/18833249.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Drag a item to dhtmlxGrid and add a column</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Struts 2.0中的Action" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F11%2Fstruts-2-0-action%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10383497.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts 2.0中的Action</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring Bean Lifecycle Managerment" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F04%2Fspring-bean-lifecycle-managerment%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10381835.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring Bean Lifecycle Managerment</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Unobtrusive Javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Funobtrusive-javascript%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10383161.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Unobtrusive Javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>将DhtmlxGrid数据导出到pdf这是很常见的需求，dhtmlx官网提供了php和java版本的例子，你可以去官网查看这篇文章《<a href="http://www.dhtmlx.com/blog/?p=855">Grid-to-Excel, Grid-to-PDF Available for Java</a>》，你可以从以下地址下载导出程序源码：<br />
<a href="http://www.dhtmlx.com/x/download/regular/export/XML2Excel.war">Export to Excel</a><br />
<a href="http://www.dhtmlx.com/x/download/regular/export/XML2PDF.war">Export to PDF</a><br />
当然，还有一个示例工程：<a href="http://www.dhtmlx.com/x/download/regular/export/javaexport_sample.zip"> .zip archive with an example</a></p>
<p>XML2PDF和XML2Excel工程内代码很相似，XML2PDF内部使用了PDFjet.jar导出PDF，而XML2Excel使用JXL导出Excel。<br />
需要说明的是，还需要引入dhtmlxgrid_export.js文件，该文件是导出grid的js源码，主要用于将表格数据，包括表头、样式等，序列化为xml字符串，然后模拟一个Form表单提交数据。</p>
<p>将上面三个工程导入到一个工程然后打开sample.html页面，效果如下：</p>
<div class="pic">
<a href="http://www.javachen.com/wp-content/uploads/2011/08/export-dhtmlxgrid-to-pdf.png" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.javachen.com/wp-content/uploads/2011/08/export-dhtmlxgrid-to-pdf-300x166.png" alt="" title="export dhtmlxgrid to pdf" width="300" height="166" class="aligncenter size-medium wp-image-2385" /></a>
</div>
<p><span id="more-2384"></span><br />
点击Get as PDF按钮，你会发现会打开一个新的窗口，然后页面什么都没有，而eclipse控制台报空指针异常。异常的主要原因在于下段代码：。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2384code4'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23844"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2384code4"><pre class="java" style="font-family:monospace;">DocumentBuilderFactory dbf <span style="color: #339933;">=</span> DocumentBuilderFactory.<span style="color: #006633;">newInstance</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
DocumentBuilder db <span style="color: #339933;">=</span> dbf.<span style="color: #006633;">newDocumentBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> dom <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
     dom <span style="color: #339933;">=</span> db.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> InputSource<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringReader</span></a><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>SAXException se<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     se.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> ioe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
     ioe.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
root <span style="color: #339933;">=</span> dom.<span style="color: #006633;">getDocumentElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>上面的代码，DocumentBuilder解析xml字符串后dom对象内并没有数据。<br />
为了能够看到DhtmlxGrid导出pdf的效果，决定将上面的代码用dom4j改写，于是有了下面的代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2384code5'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23845"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
</pre></td><td class="code" id="p2384code5"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFXMLParser <span style="color: #009900;">&#123;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> root<span style="color: #339933;">;</span>
	PDFColumn<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> columns<span style="color: #339933;">;</span>
	PDFRow<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> rows<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> widths<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aboolean+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Boolean</span></a> header <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aboolean+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Boolean</span></a> footer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> profile <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;gray&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> orientation <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setXML<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		SAXReader saxReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SAXReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> document <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			document <span style="color: #339933;">=</span> saxReader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abytearrayinputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ByteArrayInputStream</span></a><span style="color: #009900;">&#40;</span>xml.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DocumentException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		root <span style="color: #339933;">=</span> document.<span style="color: #006633;">getRootElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>root.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>root.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			header <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> footer_string <span style="color: #339933;">=</span> root.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;footer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>footer_string <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>footer_string.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			footer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> profile_string <span style="color: #339933;">=</span> root.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;profile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>profile_string <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			profile <span style="color: #339933;">=</span> profile_string<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> orientation_string <span style="color: #339933;">=</span> root.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;orientation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>orientation_string <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>orientation_string.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;landscape&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				orientation <span style="color: #339933;">=</span> A4.<span style="color: #006633;">LANDSCAPE</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				orientation <span style="color: #339933;">=</span> A4.<span style="color: #006633;">PORTRAIT</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			orientation <span style="color: #339933;">=</span> Letter.<span style="color: #006633;">PORTRAIT</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> PDFColumn<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getColumnsInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PDFColumn<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> colLine <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a> n1 <span style="color: #339933;">=</span> root.<span style="color: #006633;">element</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;head&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">elements</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;columns&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n1 <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>n1.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			columns <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFColumn<span style="color: #009900;">&#91;</span>n1.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n1.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> cols <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#41;</span> n1.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a> n2 <span style="color: #339933;">=</span> cols.<span style="color: #006633;">elements</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;column&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n2 <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>n2.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					colLine <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFColumn<span style="color: #009900;">&#91;</span>n2.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n2.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> col_xml <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#41;</span> n2.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						PDFColumn col <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFColumn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						col.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>col_xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						colLine<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> col<span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
				columns<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> colLine<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		createWidthsArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		optimizeColumns<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> columns<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> PDFRow<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getGridContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a> nodes <span style="color: #339933;">=</span> root.<span style="color: #006633;">elements</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;row&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>nodes <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			rows <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFRow<span style="color: #009900;">&#91;</span>nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				rows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDFRow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				rows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#41;</span> nodes.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rows<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #339933;">*****</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>还需要修改PDFRow类的parse方法和PDFColumn的parse方法。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2384code6'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23846"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p2384code6"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFRow <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cells<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> parse<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">List</span></a> nodes <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#41;</span> parent<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">elements</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cell&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>nodes <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			cells <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span>nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> nodes.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				cells<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a><span style="color: #009900;">&#41;</span> nodes.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTextTrim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getCells<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> cells<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFColumn <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> parse<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Element</span></a> parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		colName <span style="color: #339933;">=</span> parent.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> width_string <span style="color: #339933;">=</span> parent.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;width&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>width_string<span style="color: #339933;">!=</span>null<span style="color: #339933;">&amp;&amp;</span>width_string.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			width <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>width_string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		type <span style="color: #339933;">=</span> parent.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;type&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		align <span style="color: #339933;">=</span> parent.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;align&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> colspan_string <span style="color: #339933;">=</span> parent.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;colspan&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>colspan_string<span style="color: #339933;">!=</span>null<span style="color: #339933;">&amp;&amp;</span>colspan_string.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			colspan <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>colspan_string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> rowspan_string <span style="color: #339933;">=</span> parent.<span style="color: #006633;">attributeValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rowspan&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rowspan_string<span style="color: #339933;">!=</span>null<span style="color: #339933;">&amp;&amp;</span>rowspan_string.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			rowspan<span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>rowspan_string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这样xml字符串就能正常解析了，然后使用pdfjet.jar包就可以导出pdf了，最后的效果如下：</p>
<div class="pic">
<a href="http://www.javachen.com/wp-content/uploads/2011/08/export-dhtmlx-to-pdf-pdf.png" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.javachen.com/wp-content/uploads/2011/08/export-dhtmlx-to-pdf-pdf-300x134.png" alt="" title="export dhtmlx to pdf -pdf" width="300" height="134" class="aligncenter size-medium wp-image-2386" /></a>
</div>
<h2>结论：</h2>
<div class="note">
1.导出pdf和导出Excel代码差不多，这里不做说明。<br />
2.使用上面的工具，可以将dhtmlxgrid的数据导出到pdf，并且导出的pdf还保持了grid表格的样式（包括颜色、多表头、表头合并、复选框等等），这点很不错。<br />
3.导出的pdf为多页显示，每页有表头<br />
4.导出后的pdf页面可以直接打印，当然如果在代码上做点处理，可以直接将pdf保存为一个文件，让用户下载。
</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="生日快乐！" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F08%2Fhappy_birthday_23%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/11/30221357.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">生日快乐！</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="OpenStack架构预览" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fopenstack-architecture-overview%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/07/16567889.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">OpenStack架构预览</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Unobtrusive Javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Funobtrusive-javascript%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10383161.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Unobtrusive Javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="模仿QQ空间的警告提示栏" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fie-warn-like-qq-zone%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10382516.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">模仿QQ空间的警告提示栏</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="DhtmlxGrid Quick Start Guide" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/19/18117829.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">DhtmlxGrid Quick Start Guide</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>By the time  your rss reader get this post here is <strong> 1 </strong>comments ,Welcome you come to leave your opinion !]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/08/export-dhtmlxgrid-to-pdf-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript模态窗口和非模态窗口</title>
		<link>http://www.javachen.com/2011/08/javascript-modelessdialog-and-modaldialog/</link>
		<comments>http://www.javachen.com/2011/08/javascript-modelessdialog-and-modaldialog/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 06:58:26 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ModalDialog]]></category>
		<category><![CDATA[ModelessDialog]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=2373</guid>
		<description><![CDATA[JavaScript中弹出的窗口有模态窗口和非模态窗口。模态窗口就是打开一个子窗口，如果这个子窗口不关闭，就不能操作它的父窗口，原来程序暂停执行，直到这个模态窗口关闭后才回到原来程序继续。非模态的就是直接显示出来，然后原来的程序继续执行下面的语句，而且其他窗口也呈可用状态。 模态窗口独占了用户的输入，当一个模态窗口打开时，用户只能与该窗口进行交互，而其他用户界面对象收不到输入信息。 通常浏览器中windwo.open或超链接弹出的新窗口就是非模式窗口,而模式窗口是类似alert那种必须关闭才能响应其他事件的窗口。 常见的方法 window.showModalDialog(“http://www.javachen.com”,”newwin”,”dialogHeight: 200px; dialogWidth: 150px; dialogTop: 458px; dialogLeft: 166px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;”); window.showModelessDialog() //用来创建一个显示HTML内容的非模态对话框。 window.returnValue=”javachen”; //设置模态窗口返回值 var str=window.showModalDialog(“http://www.javachen.com”); //获得模态窗口返回值 window.close(); //关闭窗口 window.focus(); //窗口聚焦 使用方法： 参数说明： sURL&#8211;必选参数，类型：字符串。用来指定对话框要显示的文档的URL。 vArguments&#8211;可选参数，类型：变体。用来向对话框传递参数。传递的参数类型不限，包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。 sFeatures&#8211;可选参数，类型：字符串。用来描述对话框的外观等信息，可以使用以下的一个或几个，用分号“;”隔开。 1.dialogHeight :对话框高度，不小于100px，IE4中dialogHeight 和 dialogWidth 默认的单位是em，而IE5中是px，为方便其见，在定义modal方式的对话框时，用px做单位。 2.dialogWidth: 对话框宽度。 3.dialogLeft: 离屏幕左的距离。 4.dialogTop: 离屏幕上的距离。 5.center: {yes &#124; no &#124; 1 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript 弹出子窗口" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F10%2Fjavascript_open%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript 弹出子窗口</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript数据类型" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F02%2Fjavascript-basic-data-type%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript数据类型</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript中的闭包" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjavascript-inner-function%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript中的闭包</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript 中的 call与apply" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F02%2Fjavascript-call-and-apply%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript 中的 call与apply</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript验证正则表达式大全" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F08%2Fjavascript_validate_all%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript验证正则表达式大全</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>JavaScript中弹出的窗口有模态窗口和非模态窗口。模态窗口就是打开一个子窗口，如果这个子窗口不关闭，就不能操作它的父窗口，原来程序暂停执行，直到这个模态窗口关闭后才回到原来程序继续。非模态的就是直接显示出来，然后原来的程序继续执行下面的语句，而且其他窗口也呈可用状态。 模态窗口独占了用户的输入，当一个模态窗口打开时，用户只能与该窗口进行交互，而其他用户界面对象收不到输入信息。 通常浏览器中windwo.open或超链接弹出的新窗口就是非模式窗口,而模式窗口是类似alert那种必须关闭才能响应其他事件的窗口。</p>
<h2>常见的方法</h2>
<div class="info">
window.showModalDialog(“http://www.javachen.com”,”newwin”,”dialogHeight: 200px; dialogWidth: 150px; dialogTop: 458px; dialogLeft: 166px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;”); window.showModelessDialog() //用来创建一个显示HTML内容的非模态对话框。<br />
window.returnValue=”javachen”; //设置模态窗口返回值<br />
var str=window.showModalDialog(“http://www.javachen.com”); //获得模态窗口返回值<br />
window.close(); //关闭窗口<br />
window.focus(); //窗口聚焦
</div>
<h2>使用方法：</h2>
<p>参数说明： sURL&#8211;必选参数，类型：字符串。用来指定对话框要显示的文档的URL。 vArguments&#8211;可选参数，类型：变体。用来向对话框传递参数。传递的参数类型不限，包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。<br />
<span id="more-2373"></span><br />
sFeatures&#8211;可选参数，类型：字符串。用来描述对话框的外观等信息，可以使用以下的一个或几个，用分号“;”隔开。</p>
<p>1.dialogHeight :对话框高度，不小于100px，IE4中dialogHeight 和 dialogWidth 默认的单位是em，而IE5中是px，为方便其见，在定义modal方式的对话框时，用px做单位。</p>
<p>2.dialogWidth: 对话框宽度。</p>
<p>3.dialogLeft: 离屏幕左的距离。</p>
<p>4.dialogTop: 离屏幕上的距离。</p>
<p>5.center: {yes | no | 1 | 0 }：窗口是否居中，默认yes，但仍可以指定高度和宽度。</p>
<p>6.help: {yes | no | 1 | 0 }：是否显示帮助按钮，默认yes。</p>
<p>7.resizable: {yes | no | 1 | 0 } ［IE5＋］：是否可被改变大小。默认no。</p>
<p>8.status: {yes | no | 1 | 0 } ［IE5+］：是否显示状态栏。默认为yes[ Modeless]或no[Modal]。</p>
<p>9.scroll:{ yes | no | 1 | 0 | on | off }：指明对话框是否显示滚动条。默认为yes。</p>
<p>下面几个属性是用在HTA中的，在一般的网页中一般不使用。</p>
<p>10.dialogHide:{ yes | no | 1 | 0 | on | off }：在打印或者打印预览时对话框是否隐藏。默认为no。</p>
<p>11.edge:{ sunken | raised }：指明对话框的边框样式。默认为raised。</p>
<p>12.unadorned:{ yes | no | 1 | 0 | on | off }：默认为no。</p>
<p>上面的方法中需要设置太多的常数，我们可以对其进行封装，如window.open方法：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2373code12'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p2373code12"><pre class="javascript" style="font-family:monospace;">       window.doOpenWinAction = function(sUrl, _nWidth, _nHeight, _oArgs, _nTop,
		_nLeft) {
	var nWidth = _nWidth || 200;
	var nHeight = _nHeight || 200;
&nbsp;
	var nLeft = _nLeft || (window.screen.availWidth - nWidth) / 2;
	var nTop = _nTop || (window.screen.availHeight - nHeight) / 2;
&nbsp;
	// 2.Construct parameters for dialog
	var sFeatures = &quot;height=&quot;+ nHeight+ &quot;, width=&quot;	+ nWidth+ &quot;,&quot;+ &quot;top=&quot;
             + nTop+ &quot;,left=&quot;+ nLeft+ &quot;,fullscreen =no,&quot;
             +&quot;menubar =no,toolbar =no,scrollbars=yes,location =no,titlebar=no,resizable=yes&quot;;
&nbsp;
	var oWin = window.open(sUrl, '_blank', sFeatures, false);
	if (oWin == null) {
		alert(&quot;您的IE插件已经将窗口拦截，无法打开新窗口！\n&quot;
		+ &quot;请将拦截去掉--&amp;gt;点击退出--&amp;gt;关闭IE，然后重新打开IE登录即可！&quot;    
                +&quot;\n&quot; + &quot;URL：&quot; + _sUrl);
	} else {
		oWin.opener = window;
		oWin.focus();
	}
};</pre></td></tr></table></div>

<p>而对模态窗口和非模态窗口的方法也可以封装如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2373code13'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code" id="p2373code13"><pre class="javascript" style="font-family:monospace;">window.doModalDialogAction = function(sDialogUrl, _nWidth, _nHeight, _oArgs,
		_nTop, _nLeft) {
	// 1.verify parameters
	var nWidth = _nWidth || 200;
	var nHeight = _nHeight || 200;
&nbsp;
	var nLeft = _nLeft || (window.screen.availWidth - nWidth) / 2;
	var nTop = _nTop || (window.screen.availHeight - nHeight) / 2;
&nbsp;
	// 2.Construct parameters for dialog
	var sFeatures = &quot;dialogHeight: &quot; + nHeight + &quot;px; dialogWidth: &quot; + nWidth
		+ &quot;px; &quot; + &quot;dialogTop: &quot; + nTop + &quot;; dialogLeft: &quot; + nLeft + &quot;; &quot;
		+ &quot;center: Yes; scroll:Yes;help: No; resizable: No; status: No;&quot;;
	// 3.display Dialog
	var arArgs = [];
	arArgs[0] = window.location.href;
	arArgs[1] = _oArgs;
	try {
		var sResult = window.showModalDialog(sDialogUrl, arArgs, sFeatures);	
		return sResult;
	} catch (e) {
		alert(&quot;您的IE插件已经将对话框拦截！\n&quot; + 
          &quot;请将拦截去掉--&amp;gt;点击退出--&amp;gt;关闭IE，然后重新打开IE登录即可！\n&quot;);
		return true;
	}
&nbsp;
};
&nbsp;
window.doModelessDialogAction = function(sDialogUrl, _nWidth, _nHeight, _oArgs,
		_nTop, _nLeft) {
	// 1.verify parameters
	var nWidth = _nWidth || 200;
	var nHeight = _nHeight || 200;
&nbsp;
	var nLeft = _nLeft || (window.screen.availWidth - nWidth) / 2;
	var nTop = _nTop || (window.screen.availHeight - nHeight) / 2;
&nbsp;
	// 2.Construct parameters for dialog
	var sFeatures = &quot;dialogHeight: &quot; + nHeight + &quot;px; dialogWidth: &quot; + nWidth
			+ &quot;px; &quot; + &quot;dialogTop: &quot; + nTop + &quot;; dialogLeft: &quot; + nLeft + &quot;; &quot;
			+ &quot;center: Yes; scroll:Yes;help: No; resizable: No; status: No;&quot;;
	// 3.display Dialog
	var arArgs = new Array();
	arArgs[0] = window.location.href;
	arArgs[1] = _oArgs;
	try {
		var sResult = window.showModelessDialog(sDialogUrl, arArgs, sFeatures);
		return sResult;
	} catch (e) {
		alert(&quot;您的IE插件已经将对话框拦截！\n&quot; + 
          &quot;请将拦截去掉--&amp;gt;点击退出--&amp;gt;关闭IE，然后重新打开IE登录即可！\n&quot;);
		return true;
	}
&nbsp;
};</pre></td></tr></table></div>

<p>模态窗口一般是用于弹出窗口，让用户在模态窗口中选中某一数据之后关闭模态窗口将用户选择的结果返回到当前页面，如下图。下图单击浏览按钮将弹出窗口，用户当前弹出窗口中数据列表的某一行记录，就会关闭窗口将该行记录的id和name设置到当前窗口的隐藏域（id）和输入框上（name）。</p>
<div class="pic"><a href="http://www.javachen.com/wp-content/uploads/2011/08/modeldialog.png" class="highslide-image" onclick="return hs.expand(this);"><img class="aligncenter size-medium wp-image-2374" title="modeldialog" src="http://www.javachen.com/wp-content/uploads/2011/08/modeldialog-300x160.png" alt="" width="300" height="160" /></a></div>
<p>为了实现上面的效果，需要在浏览按钮上添加事件：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2373code14'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237314"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p2373code14"><pre class="javascript" style="font-family:monospace;">function showSelectIdNameDialog(id, name, url, width, height, callback) {
	var array = callback ? toArray(id, name, callback) : toArray(id, name);
	var args = buildDialogArgs(onDialogSelectIdAndName, array,callback);
	doModalDialogAction2(url, width, height, args);
	return false;
}</pre></td></tr></table></div>

<div class="note">
常数id指定当前隐藏域的id；<br />
name指定返回结果需要显示到的输入框name；<br />
url为访问地址；<br />
callback为回调函数，在关闭窗口后执行
</div>
<p>设置好id和name的值，在调用上面的方法之后，但用户选中某一行数据之后，只需要在打开的页面设置好返回值，其他的就不用我们做了。showSelectIdNameDialog方法的内部往常数里添加列onDialogSelectIdAndName这个回调函数，而这个函数就是用于将窗口返回值设置到指定id和name的input域上。</p>
<p>在弹出窗口的页面，需要返回结果：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2373code15'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237315"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2373code15"><pre class="javascript" style="font-family:monospace;">grid.attachEvent(&quot;onRowSelect&quot;, function(id, ind) {
	dialogReturn(id, grid.cells(id, 1).getValue());//这句话是关键
	return true;
});</pre></td></tr></table></div>

<p>当然，我们还可以自定义回调函数：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2373code16'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p237316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p2373code16"><pre class="javascript" style="font-family:monospace;">/**
 * 弹出选择窗口
 * @param url
 * @param width
 * @param height
 * @param callback
 * @returns {Boolean}
 */
function showSelectDialog(url, width, height, callback) {
	var args = buildDialogArgs(onDialogSelectCallback,callback);
	doModalDialogAction2(url, width, height, args);
	return false;
}
&nbsp;
/**
 * 弹出普通的窗口
 * @param url
 * @param width
 * @param height
 * @param callback
 * @returns {Boolean}
 */
function showNormalDialogCallback(url, width, height, callback) {
	var args = buildDialogArgs(callback,{});
	doModalDialogAction2(url, width, height, args);
	return false;
}
&nbsp;
function showModalDialogCallbackExt(url, width, height, callback, param) {
	var args = buildDialogArgs(callback, param);
	doModalDialogAction2(url, width, height, args);
	return false;
}</pre></td></tr></table></div>

<h2>说明：</h2>
<p>本文中的源码和例子可以通过email(在关于页面可以找到地址)向我索取。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript 弹出子窗口" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F10%2Fjavascript_open%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript 弹出子窗口</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript数据类型" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F02%2Fjavascript-basic-data-type%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript数据类型</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript中的闭包" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjavascript-inner-function%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript中的闭包</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript 中的 call与apply" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F02%2Fjavascript-call-and-apply%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript 中的 call与apply</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript验证正则表达式大全" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F08%2Fjavascript_validate_all%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fjavascript-modelessdialog-and-modaldialog%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript验证正则表达式大全</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/08/javascript-modelessdialog-and-modaldialog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自定义dhtmlxGrid表头菜单</title>
		<link>http://www.javachen.com/2011/07/custom-dhtmlxgrid-header-menu/</link>
		<comments>http://www.javachen.com/2011/07/custom-dhtmlxgrid-header-menu/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 08:12:22 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dhtmlx]]></category>
		<category><![CDATA[DhtmlxGrid]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=2286</guid>
		<description><![CDATA[dhtmlxGrid可以定义表头菜单以及表格右键菜单，表格右键菜单可以自定义，但是表头菜单只能使用其提供的菜单。dhtmlxGrid默认的表头菜单可以决定表格中每一列是否在表格中显示，并没有提供更多的扩展，如果我想自定义表头菜单，该怎么做呢？本文就是基于自定义表格菜单，说说我的实现方式。 以下是dhtmlxGrid的表头菜单效果： 其功能过于单一，以下是表格右键菜单效果： 如果能够像表格菜单一样自定义表头菜单，那会是一件非常有意义的事情，因为dhtmlxGrid菜单都是一些针对行和单元格的操作，没有提过针对列的操作，比如我可能需要在某一列上实现该列的显示与隐藏、排序、改变列属性以及在该列右边添加一新的列，等等。 如何实现表头菜单的自定义呢？可不可将表格右键菜单移到表头上去呢？ 首先，来看看context menu的实现方式，下面代码来自dhtmlxGrid Samples中的Context menu例子源码： ?View Code JAVASCRIPT1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 function onButtonClick(menuitemId, type) { var data = mygrid.contextID.split(&#34;_&#34;); //rowId_colInd; mygrid.setRowTextStyle(data[0], &#34;color:&#34; + menuitemId.split(&#34;_&#34;)[1]); return true; } menu = new dhtmlXMenuObject(); menu.setIconsPath(&#34;../common/images/&#34;); [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="DhtmlxGrid Quick Start Guide" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/19/18117829.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">DhtmlxGrid Quick Start Guide</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Export DhtmlxGrid to PDF in Java" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/11/22222962.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Export DhtmlxGrid to PDF in Java</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Drag a item to dhtmlxGrid and add a column" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-a-item-to-dhtmlxgrid-and-add-a-column%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/25/18833249.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Drag a item to dhtmlxGrid and add a column</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Drag an item to dhtmlxGrid and add a column" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/25/18833249.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Drag an item to dhtmlxGrid and add a column</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring Bean Lifecycle Managerment" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F04%2Fspring-bean-lifecycle-managerment%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10381835.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring Bean Lifecycle Managerment</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>dhtmlxGrid可以定义表头菜单以及表格右键菜单，表格右键菜单可以自定义，但是表头菜单只能使用其提供的菜单。dhtmlxGrid默认的表头菜单可以决定表格中每一列是否在表格中显示，并没有提供更多的扩展，如果我想自定义表头菜单，该怎么做呢？本文就是基于自定义表格菜单，说说我的实现方式。<br />
以下是dhtmlxGrid的表头菜单效果：</p>
<div class="pic"><a href="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-head-menu.jpg" class="highslide-image" onclick="return hs.expand(this);"><img class="aligncenter size-medium wp-image-2287" title="dhtmlxgrid-head-menu" src="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-head-menu-300x174.jpg" alt="" width="300" height="174" /></a></div>
<p>其功能过于单一，以下是表格右键菜单效果：</p>
<div class="pic"><a href="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-context-menu.jpg" class="highslide-image" onclick="return hs.expand(this);"><img class="aligncenter size-medium wp-image-2288" title="dhtmlxgrid-context-menu" src="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-context-menu-300x126.jpg" alt="" width="300" height="126" /></a></div>
<p>如果能够像表格菜单一样自定义表头菜单，那会是一件非常有意义的事情，因为dhtmlxGrid菜单都是一些针对行和单元格的操作，没有提过针对列的操作，比如我可能需要在某一列上实现该列的显示与隐藏、排序、改变列属性以及在该列右边添加一新的列，等等。<br />
如何实现表头菜单的自定义呢？可不可将表格右键菜单移到表头上去呢？<span id="more-2286"></span><br />
首先，来看看context menu的实现方式，下面代码来自dhtmlxGrid Samples中的Context menu例子源码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code24'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228624"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p2286code24"><pre class="javascript" style="font-family:monospace;">function onButtonClick(menuitemId, type) {
    var data = mygrid.contextID.split(&quot;_&quot;);
    //rowId_colInd;
    mygrid.setRowTextStyle(data[0], &quot;color:&quot; + menuitemId.split(&quot;_&quot;)[1]);
    return true;
}
menu = new dhtmlXMenuObject();
menu.setIconsPath(&quot;../common/images/&quot;);
menu.renderAsContextMenu();
menu.attachEvent(&quot;onClick&quot;, onButtonClick);
menu.loadXML(&quot;../common/_context.xml&quot;);
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setImagePath(&quot;../../codebase/imgs/&quot;);
mygrid.setHeader(&quot;Author,Title&quot;);
mygrid.setInitWidths(&quot;250,250&quot;);
mygrid.enableAutoWidth(true);
mygrid.setColAlign(&quot;left,left&quot;);
mygrid.setColTypes(&quot;ro,link&quot;);
mygrid.setColSorting(&quot;str,str&quot;);
mygrid.enableContextMenu(menu);
mygrid.init();
mygrid.setSkin(&quot;dhx_skyblue&quot;);
mygrid.loadXML(&quot;../common/grid_links.xml&quot;);</pre></td></tr></table></div>

<p>上面代码创建了一个menu并将其作为context menu附件到grid上面去，下面为最关键的两行行代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code25'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228625"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2286code25"><pre class="javascript" style="font-family:monospace;">menu.renderAsContextMenu();
mygrid.enableContextMenu(menu);</pre></td></tr></table></div>

<p>上面对于context menu提供的方法太少，这时候可以看看dhtmlxMenu api，看看有没有设置context menu生效位置的方法（指定context menu在哪片区域有效）。在dhtmlxMenu API Methods里没有找到需要的方法，这时候在官网论坛搜搜，也许可以找到点什么。<br />
在论坛里可以找到一个例子，大致代码如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code26'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228626"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p2286code26"><pre class="javascript" style="font-family:monospace;">function onButtonClick(menuitemId, type) {
    var data = mygrid.contextID.split(&quot;_&quot;);
    //rowId_colInd;
    mygrid.setRowTextStyle(data[0], &quot;color:&quot; + menuitemId.split(&quot;_&quot;)[1]);
    return true;
}
menu = new dhtmlXMenuObject();
menu.setIconsPath(&quot;../common/images/&quot;);
menu.attachEvent(&quot;onClick&quot;, onButtonClick);
menu.loadXML(&quot;../common/_context.xml&quot;);
&nbsp;
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setImagePath(&quot;../../codebase/imgs/&quot;);
mygrid.setHeader(&quot;Author,Title&quot;);
mygrid.setInitWidths(&quot;250,250&quot;);
mygrid.enableAutoWidth(true);
mygrid.setColAlign(&quot;left,left&quot;);
mygrid.setColTypes(&quot;ro,link&quot;);
mygrid.setColSorting(&quot;str,str&quot;);
//mygrid.enableContextMenu(menu); //使其失效
mygrid.init();
mygrid.setSkin(&quot;dhx_skyblue&quot;);
mygrid.loadXML(&quot;../common/grid_links.xml&quot;);
&nbsp;
mygrid.hdr.id = &quot;header_id&quot;;
var header_row = mygrid.hdr.rows[1];
for ( var i = 0; i &amp;lt; header_row.cells.length; i++) {
   header_row.cells[i].id = &quot;context_zone_&quot; + i;
}
menu.addContextZone(&quot;header_id&quot;);</pre></td></tr></table></div>

<p>上面最关键的代码在最后几行，给dhtmlxGrid表头设置了一个id，然后调用menu的addContextZone()方法指定centext的有效区域。视乎这就是我们所需要的，但是你执行以上代码你会发现onButtonClick方法里mygrid.contextID会报错，原因是mygrid没有contextID属性（在context menu中通过该属性可以获知鼠标焦点在哪一行，但是现在在表头上强加了该menu，所以并不存在该属性了）。<br />
剩下的问题是需要解决，菜单单击事件了。我们可以在表头的contextmenu事件处罚的时候获取鼠标焦点，并将自定义的菜单在该位置显示，该方法如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code27'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228627"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p2286code27"><pre class="javascript" style="font-family:monospace;">dhtmlxEvent(mygrid.hdr, &quot;contextmenu&quot;, function(ev) {
	ev = ev || event;
	var el = ev.target || ev.srcElement;
	var zel = el;
	while (zel.tagName != &quot;TABLE&quot;)
		zel = zel.parentNode;
	var grid = zel.grid;
	if (!grid)
		return;
	grid.setActive();
&nbsp;
	el = grid.getFirstParentOfType(el, &quot;TD&quot;)
&nbsp;
	if ((grid) &amp;amp;&amp;amp; (!grid._colInMove)) {
		grid.resized = null;
		if ((!grid._mCols) || (grid._mCols[el._cellIndex] == &quot;true&quot;))
			colId = el._cellIndex + 1;//获得表头右键菜单焦点所在列索引
	}
&nbsp;
	function mouseCoords(ev) {
		if (ev.pageX || ev.pageY) {
			return {
				x : ev.pageX,
				y : ev.pageY
			};
		}
		var d = _isIE &amp;amp;&amp;amp; document.compatMode != &quot;BackCompat&quot; ? 
                    document.documentElement: document.body;
		return {
			x : ev.clientX + d.scrollLeft - d.clientLeft,
			y : ev.clientY + d.scrollTop - d.clientTop
		};
	}
&nbsp;
	var coords = mouseCoords(ev);
	menu.addContextZone(&quot;header_id&quot;);
	menu.showContextMenu(coords.x, coords.y);//强制显示
	return true;
});</pre></td></tr></table></div>

<p>在上面的代码里，我们获得表头右键菜单焦点所在列索引，将其值赋给colId，然后在菜单单击事件的时候添加一新的列并将colId重置：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code28'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228628"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2286code28"><pre class="javascript" style="font-family:monospace;">function onButtonClick(menuitemId, type, e) {
	mygrid.insertColumn(colId, &quot;12&quot;, &quot;ed&quot;, 80);
	colId = 0;
	return true;
}</pre></td></tr></table></div>

<p>然后，需要禁止掉表格数据区域的菜单显示：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code29'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228629"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2286code29"><pre class="javascript" style="font-family:monospace;">mygrid.attachEvent(&quot;onBeforeContextMenu&quot;, function(rid, cid, e) {
	return false;//禁止数据区域菜单
});</pre></td></tr></table></div>

<p>最后的最后，最后的代码如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2286code30'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228630"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code" id="p2286code30"><pre class="javascript" style="font-family:monospace;">	var mygrid, colId;
&nbsp;
	function onButtonClick(menuitemId, type, e) {
		mygrid.insertColumn(colId, &quot;12&quot;, &quot;ed&quot;, 80);
		colId = 0;
		return true;
	}
&nbsp;
	menu = new dhtmlXMenuObject();
	menu.setIconsPath(&quot;../common/images/&quot;);
	menu.renderAsContextMenu();
	menu.attachEvent(&quot;onClick&quot;, onButtonClick);
	menu.loadXML(&quot;../common/_context.xml&quot;);
	menu.attachEvent(&quot;onBeforeContextMenu&quot;, function(zoneId, e) {
		var hdr = document.getElementById(zoneId)
		return true;
	});
&nbsp;
	mygrid = new dhtmlXGridObject('gridbox');
	mygrid.setImagePath(&quot;../codebase/imgs/&quot;);
	mygrid.setHeader(&quot;Sales,Book Title,Author,Price,In Store,Shipping,Bestseller,
              Date of Publication&quot;);
	mygrid.setInitWidths(&quot;50,150,100,80,80,80,80,200&quot;);
	mygrid.setColAlign(&quot;right,left,left,right,center,left,center,center&quot;);
	mygrid.setColTypes(&quot;dyn,edtxt,ed,price,ch,co,ra,ro&quot;);
&nbsp;
	mygrid.init();
	mygrid.setSkin(&quot;dhx_skyblue&quot;);
	//mygrid.enableHeaderMenu();
	mygrid.enableColumnMove(true);
	mygrid.enableContextMenu(menu);
	dhtmlxEvent(mygrid.hdr, &quot;contextmenu&quot;, function(ev) {
		ev = ev || event;
		var el = ev.target || ev.srcElement;
		var zel = el;
		while (zel.tagName != &quot;TABLE&quot;)
			zel = zel.parentNode;
		var grid = zel.grid;
		if (!grid)
			return;
		grid.setActive();
&nbsp;
		el = grid.getFirstParentOfType(el, &quot;TD&quot;)
&nbsp;
		if ((grid) &amp;#038;&amp; (!grid._colInMove)) {
			grid.resized = null;
			if ((!grid._mCols) || (grid._mCols[el._cellIndex] == &quot;true&quot;))
                                //获得表头右键菜单焦点所在列索引
				colId = el._cellIndex + 1;
		}
&nbsp;
		function mouseCoords(ev) {
			if (ev.pageX || ev.pageY) {
				return {
					x : ev.pageX,
					y : ev.pageY
				};
			}
			var d = _isIE &amp;#038;&amp; document.compatMode != &quot;BackCompat&quot; ? 
                             document.documentElement: document.body;
			return {
				x : ev.clientX + d.scrollLeft - d.clientLeft,
				y : ev.clientY + d.scrollTop - d.clientTop
			};
		}
&nbsp;
		var coords = mouseCoords(ev);
		menu.addContextZone(&quot;header_id&quot;);
		menu.showContextMenu(coords.x, coords.y);//强制显示
		return true;
	});
&nbsp;
	mygrid.attachEvent(&quot;onBeforeContextMenu&quot;, function(rid, cid, e) {
		return false;//禁止数据区域菜单
	});
&nbsp;
	mygrid.loadXML(&quot;../common/grid_ml_16_rows_columns_manipulations.xml&quot;);
&nbsp;
	mygrid.hdr.id = &quot;header_id&quot;;
	var header_row = mygrid.hdr.rows[1];
	for ( var i = 0; i &lt; header_row.cells.length; i++) {
		header_row.cells[i].id = &quot;context_zone_&quot; + i;
	}</pre></td></tr></table></div>

<p>效果图如下;</p>
<div class="pic">
<a href="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-custom-head-menu.jpg" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.javachen.com/wp-content/uploads/2011/07/dhtmlxgrid-custom-head-menu-300x154.jpg" alt="" title="dhtmlxgrid-custom-head-menu" width="300" height="154" class="aligncenter size-medium wp-image-2291" /></a></div>
</pre>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="DhtmlxGrid Quick Start Guide" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/19/18117829.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">DhtmlxGrid Quick Start Guide</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Export DhtmlxGrid to PDF in Java" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/11/22222962.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Export DhtmlxGrid to PDF in Java</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Drag a item to dhtmlxGrid and add a column" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-a-item-to-dhtmlxgrid-and-add-a-column%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/25/18833249.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Drag a item to dhtmlxGrid and add a column</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Drag an item to dhtmlxGrid and add a column" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/25/18833249.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Drag an item to dhtmlxGrid and add a column</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Butterfly Scheduling Introduction" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F05%2Fbutterfly-scheduling-introduction%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fcustom-dhtmlxgrid-header-menu%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10376939.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Butterfly Scheduling Introduction</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/07/custom-dhtmlxgrid-header-menu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drag an item to dhtmlxGrid and add a column</title>
		<link>http://www.javachen.com/2011/07/drag-an-item-to-dhtmlxgrid-and-add-a-column/</link>
		<comments>http://www.javachen.com/2011/07/drag-an-item-to-dhtmlxgrid-and-add-a-column/#comments</comments>
		<pubDate>Sun, 24 Jul 2011 13:27:28 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DhtmlxGrid]]></category>
		<category><![CDATA[dhtmlxTree]]></category>
		<category><![CDATA[Drag]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=2236</guid>
		<description><![CDATA[dhtmlxGrid支持tree和grid、grid之间、grid内部进行拖拽，如在grid内部进行拖拽，可以增加一行；在grid之间拖拽，第一个grid的记录删除，第二个grid增加一行记录。如果我想在拖拽之后不是添加一行而是一列，该怎么做呢？ 现在有个需求，就是左边有个tree，右边有个grid，将左边tree的一个节点拖到右边grid的表头并动态增加一列。这个怎么做呢？ 如果你想快点看到最后的实现方法，你可以直接跳到本文的最后参看源码。 首先看看dhtmlxTree 关于Drag-n-Drop的例子，其中有这样一个例子Custom Drag Out。 上面的例子，右边定义了一个输入框，其id为“sInput”，代码如下： function maf() { return false; } tree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0); tree.setSkin('dhx_skyblue'); tree.setImagePath("../../codebase/imgs/csh_yellowbooks/"); tree.enableDragAndDrop(true); tree.setDragHandler(maf); tree.enableSmartXMLParsing(true); tree.loadXML("../common/tree_05_drag_n_drop.xml"); function s_control() { this._drag = function(sourceHtmlObject, dhtmlObject, targetHtmlObject) { targetHtmlObject.style.backgroundColor = ""; targetHtmlObject.value = sourceHtmlObject.parentObject.label; } this._dragIn = function(htmlObject, shtmlObject) { htmlObject.style.backgroundColor = "#fffacd"; return htmlObject; } this._dragOut [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Export DhtmlxGrid to PDF in Java" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F08%2Fexport-dhtmlxgrid-to-pdf-in-java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/11/22222962.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Export DhtmlxGrid to PDF in Java</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="毕业纪念册完结了" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F06%2Fgraduation_memory_photo%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10384461.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">毕业纪念册完结了</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JFreeChart 生成饼状图" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F04%2Fjfreechart-create_piechart%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10381940.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JFreeChart 生成饼状图</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="接触云服务环境Eucalyptus" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F06%2Ftouch-cloud-environmenteucalyptus%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/19/12682139.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">接触云服务环境Eucalyptus</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Hibernate one-to-one association" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F05%2Fhibernat_-one-to-one_association%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/19/10385005.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Hibernate one-to-one association</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>dhtmlxGrid支持tree和grid、grid之间、grid内部进行拖拽，如在grid内部进行拖拽，可以增加一行；在grid之间拖拽，第一个grid的记录删除，第二个grid增加一行记录。如果我想在拖拽之后不是添加一行而是一列，该怎么做呢？<br />
现在有个需求，就是左边有个tree，右边有个grid，将左边tree的一个节点拖到右边grid的表头并动态增加一列。这个怎么做呢？<br />
如果你想快点看到最后的实现方法，你可以直接跳到本文的最后参看源码。<br />
首先看看dhtmlxTree 关于<a href="http://www.dhtmlx.com/docs/products/dhtmlxGrid/samples/05_drag_n_drop/">Drag-n-Drop</a>的例子，其中有这样一个例子<a href="http://www.dhtmlx.com/docs/products/dhtmlxTree/samples/05_drag_n_drop/08_pro_drag_out.html">Custom Drag Out</a>。</p>
<div class="pic">
<a href="http://www.javachen.com/wp-content/uploads/2011/07/QQ截图20110724205806.png" class="highslide-image" onclick="return hs.expand(this);"><img class="aligncenter size-medium wp-image-2237" title="QQ截图20110724205806" src="http://www.javachen.com/wp-content/uploads/2011/07/QQ截图20110724205806-300x148.png" alt="" width="300" height="148" /></a>
</div>
<p><span id="more-2236"></span><br />
上面的例子，右边定义了一个输入框，其id为“sInput”，代码如下：</p>
<pre escaped="true" lang="javascript" line="1">function maf() {
    return false;
}
tree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);

tree.setSkin('dhx_skyblue');
tree.setImagePath("../../codebase/imgs/csh_yellowbooks/");
tree.enableDragAndDrop(true);
tree.setDragHandler(maf);
tree.enableSmartXMLParsing(true);
tree.loadXML("../common/tree_05_drag_n_drop.xml");

function s_control() {
    this._drag = function(sourceHtmlObject, dhtmlObject, targetHtmlObject) {
        targetHtmlObject.style.backgroundColor = "";
        targetHtmlObject.value = sourceHtmlObject.parentObject.label;
    }
    this._dragIn = function(htmlObject, shtmlObject) {
        htmlObject.style.backgroundColor = "#fffacd";
        return htmlObject;
    }
    this._dragOut = function(htmlObject) {
        htmlObject.style.backgroundColor = "";
        return this;
    }
}
var sinput = document.getElementById('sInput');
tree.dragger.addDragLanding(sinput, new s_control);</pre>
<p>为了使tree支持拖拽功能，必须添加以下代码：</p>
<pre escaped="true" lang="javascript" line="1">tree.enableDragAndDrop(true);</pre>
<p>为了实现自定义拖拽的输出，添加了以下代码：</p>
<pre escaped="true" lang="javascript" line="1">tree.dragger.addDragLanding(sinput, new s_control);</pre>
<p>从上面的字母意思可以看出，是在tree的拖拽对象dragger对象上添加一个拖拽着地对象，第一个常数是指拖拽到哪一个区域，第二个常数定义拖拽的三个方法：</p>
<pre escaped="true" lang="javascript" line="1">    this._drag = function(sourceHtmlObject, dhtmlObject, targetHtmlObject) {
        targetHtmlObject.style.backgroundColor = "";
        targetHtmlObject.value = sourceHtmlObject.parentObject.label;
    }
    this._dragIn = function(htmlObject, shtmlObject) {
        htmlObject.style.backgroundColor = "#fffacd";
        return htmlObject;
    }
    this._dragOut = function(htmlObject) {
        htmlObject.style.backgroundColor = "";
        return this;
    }</pre>
<p>参照上面的思路，我们可以在grid的表头上面定义一个id，然后通过该id获得表头的dom对象，更好的一个方法是通过mygrid.hdr（看看源码就知道列）能过获得grid的表头对象，然后调用下面的方法，定义tree拖拽到grid的表头：</p>
<pre escaped="true" lang="javascript" line="1">tree.dragger.addDragLanding(mygrid.hdr, new s_control);</pre>
<p>但是这个时候，你将tree的一个节点拖到grid的表头，grid不会有任何反应，故需要改写s_control对象的方法，这里主要是改写一个方法：</p>
<pre escaped="true" lang="javascript" line="1">var insertId;
this._drag = function(sourceHtmlObject, dhtmlObject,
		targetHtmlObject, e) {
	var zel = e;
	while (zel.tagName != "TABLE")
		zel = zel.parentNode;
	var grid = zel.grid;
	if (!grid)
		return;
	grid.setActive();
	if (!grid._mCol || e.button == 2)
		return;
	e = grid.getFirstParentOfType(e, "TD")

	if ((grid) &amp;&amp; (!grid._colInMove)) {
		grid.resized = null;
		if ((!grid._mCols) || (grid._mCols[e._cellIndex] == "true"))
			insertId = e._cellIndex + 1;
	}

	mygrid.insertColumn(insertId, "12", "ed", 80);
}</pre>
<p>该方法主要做的事情是计算拖拽落脚时候鼠标焦点所在的列，然后在其右边添加一新的列。</p>
<div class="pic">
<a href="http://www.javachen.com/wp-content/uploads/2011/07/QQ截图20110724211631.png" class="highslide-image" onclick="return hs.expand(this);"><img class="aligncenter size-medium wp-image-2238" title="QQ截图20110724211631" src="http://www.javachen.com/wp-content/uploads/2011/07/QQ截图20110724211631-300x91.png" alt="" width="300" height="91" /></a>
</div>
<p>本例最后的代码：</p>
<pre escaped="true" lang="javascript" line="1">
<script>
	var mygrid;
	function maf() {
		return false;
	}

	tree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);
	tree.setSkin('dhx_skyblue');
	tree.setImagePath("../../dhtmlxTree/codebase/imgs/csh_yellowbooks/");
	tree.enableDragAndDrop(true);
	//tree.setDragHandler(maf);
	tree.enableSmartXMLParsing(true);
	tree.loadXML("../../dhtmlxTree/samples/common/tree_05_drag_n_drop.xml")
	tree.openAllItems(0);

	function s_control() {
		var insertId;
		this._drag = function(sourceHtmlObject, dhtmlObject,
				targetHtmlObject, e) {
			var zel = e;
			while (zel.tagName != "TABLE")
				zel = zel.parentNode;
			var grid = zel.grid;
			if (!grid)
				return;
			grid.setActive();
			if (!grid._mCol || e.button == 2)
				return;
			e = grid.getFirstParentOfType(e, "TD")

			if ((grid) &#038;&#038; (!grid._colInMove)) {
				grid.resized = null;
				if ((!grid._mCols) || (grid._mCols[e._cellIndex] == "true"))
					insertId = e._cellIndex + 1;
			}

			mygrid.insertColumn(insertId, "12", "ed", 80);
		}
	}
	mygrid = new dhtmlXGridObject('gridbox');
	mygrid.setImagePath("../codebase/imgs/");
	mygrid.setHeader("Sales,Book Title,Author,Price,In Store,Shipping,Bestseller,
              Date of Publication");
	mygrid.setInitWidths("50,150,100,80,80,80,80,200");
	mygrid.setColAlign("right,left,left,right,center,left,center,center");
	mygrid.setColTypes("dyn,edtxt,ed,price,ch,co,ra,ro");
	mygrid.enableDragAndDrop("temporary_disabled", true);
	mygrid.init();
	mygrid.setSkin("dhx_skyblue");
	mygrid.enableHeaderMenu();
	mygrid.enableColumnMove(true);
	mygrid.setColumnHidden(2, true);
	mygrid.attachEvent("onHeaderClick", function(ind, obj) {
	});
	mygrid.loadXML("../common/grid_ml_16_rows_columns_manipulations.xml");
	tree.dragger.addDragLanding(mygrid.hdr, new s_control);
</script>
</pre>
<p>本文实现的是将tree拖拽到grid，其实其他的一些支持拖拽的组件也可以做，并不局限于tree组件，甚至还见过有人实现jquery的dtree拖拽到dhtmlxGrid增加一行记录。</p>
<div class="info">
<h2>参考文章</h2>
<p>Custom Drag Out：<br />
 <a href="http://www.dhtmlx.com/docs/products/dhtmlxTree/samples/05_drag_n_drop/08_pro_drag_out.html" target="_blank">http://www.dhtmlx.com/docs/products/dhtmlxTree/samples/05_drag_n_drop/08_pro_drag_out.html</a><br />
dhtmlxGrid doc：<a href="http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:toc" target="_blank">http://docs.dhtmlx.com/doku.php?id=dhtmlxgrid:toc</a><br />
dhtmlxTree doc：<a href="http://docs.dhtmlx.com/doku.php?id=dhtmlxtree:toc" target="_blank">http://docs.dhtmlx.com/doku.php?id=dhtmlxtree:toc</a>
</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="这一个月我在做什么" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F05%2Fwhat_am_i_doing_this_month%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10381574.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">这一个月我在做什么</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Spring Bean Lifecycle Managerment" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F04%2Fspring-bean-lifecycle-managerment%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10381835.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Spring Bean Lifecycle Managerment</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="使用kettle数据迁移添加主键和索引" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2012%2F01%2Fadd-primary-keys-and-indexes-when-migrating-datas-whith-kettle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/01/05/13482495.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用kettle数据迁移添加主键和索引</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="DhtmlxGrid Quick Start Guide" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdrag-an-item-to-dhtmlxgrid-and-add-a-column%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/19/18117829.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">DhtmlxGrid Quick Start Guide</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/07/drag-an-item-to-dhtmlxgrid-and-add-a-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DhtmlxGrid Quick Start Guide</title>
		<link>http://www.javachen.com/2011/07/dhtmlxgrid-quick-start-guide/</link>
		<comments>http://www.javachen.com/2011/07/dhtmlxgrid-quick-start-guide/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 13:28:59 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DhtmlxGrid]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=2217</guid>
		<description><![CDATA[说明:本文来源于http://dhtmlx.com/docs/products/dhtmlxGrid/，本人对其进行翻译整理成下文，贴出此文，紧供分享。 dhtmlxGrid是一个拥有强大的数据绑定、优秀的大数据展示性能并支持ajax的JavaScript表格控件。该组件易于使用并通过富客户端的API提供了很大的扩展性。dhtmlxGrid支持不同的数据源（XML, JSON, CSV, JavaScript 数组和HTML表格），如果需要的话，还可以从自定义的xml中加载数据。 跨浏览器 使用JavaScript API进行控制 Ajax支持 简单的JavaScript 或者XML 配置 与HTML集成 内建过滤、排序、查询、分组功能 表格 footer/header自动计算 行内编辑 准备使用大数据集解决方案：分页，动态加载，智能渲染 序列化为XML或CSV 从 XML或CSV加载 列锁定 剪贴板支持 简单的客户端到服务器端配置 (使用 dhtmlxConnector, 可用于 PHP, Java, .NET, ColdFusion) 支持子表格 列拖拽和移动 行或列拖拽 dhtmlxTree PRO Edition支持拖拽 可以创建一个编辑器或是列格式化 (使用 eXcell – 继承自 cell 对象) 组合框，日历以及更多的预定义eXcells Cell支持数学方程式 不同的键盘映射 简单的CSS风格或是预定义的皮肤 对于rows/entire grid不可见的数据块 (用户数据) 客户端排序(string, integer, date, [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="OpenNebula的架构" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fopennebula-architecture%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/19/14457146.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">OpenNebula的架构</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="jQuery中的正则表达式" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fregex-in-jquery%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10379741.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">jQuery中的正则表达式</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Begin to study struts2" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F11%2Fbegin-to-study-struts2%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/05/31/10383520.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Begin to study struts2</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="OpenStack架构预览" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fopenstack-architecture-overview%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/07/16567889.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">OpenStack架构预览</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="房奴生活" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F10%2Ffinding_house_life%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/22/10383918.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">房奴生活</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>说明:</strong></span>本文来源于<a href="http://dhtmlx.com/docs/products/dhtmlxGrid/">http://dhtmlx.com/docs/products/dhtmlxGrid/</a>，本人对其进行翻译整理成下文，贴出此文，紧供分享。</p>
<p>dhtmlxGrid是一个拥有强大的数据绑定、优秀的大数据展示性能并支持ajax的JavaScript表格控件。该组件易于使用并通过富客户端的API提供了很大的扩展性。dhtmlxGrid支持不同的数据源（XML, JSON, CSV, JavaScript 数组和HTML表格），如果需要的话，还可以从自定义的xml中加载数据。</p>
<ul>
<li>跨浏览器</li>
<li>使用JavaScript API进行控制</li>
<li>Ajax支持</li>
<li>简单的JavaScript 或者XML 配置</li>
<li>与HTML集成</li>
<li>内建过滤、排序、查询、分组功能</li>
<li>表格 footer/header自动计算</li>
<li>行内编辑</li>
<li>准备使用大数据集解决方案：分页，动态加载，智能渲染</li>
<li>序列化为XML或CSV</li>
<li>从 XML或CSV加载</li>
<li>列锁定</li>
<li>剪贴板支持</li>
<li>简单的客户端到服务器端配置 (使用 dhtmlxConnector, 可用于 PHP, Java, .NET, ColdFusion)</li>
<li>支持子表格</li>
<li>列拖拽和移动</li>
<li>行或列拖拽</li>
<li>dhtmlxTree PRO Edition支持拖拽</li>
<li>可以创建一个编辑器或是列格式化 (使用 eXcell – 继承自 cell 对象)</li>
<li>组合框，日历以及更多的预定义eXcells</li>
<li>Cell支持数学方程式</li>
<li>不同的键盘映射</li>
<li>简单的CSS风格或是预定义的皮肤</li>
<li>对于rows/entire grid不可见的数据块 (用户数据)</li>
<li>客户端排序(string, integer, date, custom)</li>
<li>服务器端排序</li>
<li>广泛的事件处理</li>
</ul>
<h2><span id="more-2217"></span><br />
Step 1 – 引入文件</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code44'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221744"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p2217code44"><pre class="html" style="font-family:monospace;">&lt;rel =&quot;STYLESHEET&quot; type=&quot;text/css&quot; href=&quot;codebase/dhtmlxgrid.css&quot;&gt;
&lt;script src=&quot;codebase/dhtmlxcommon.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;codebase/dhtmlxgrid.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;codebase/dhtmlxgridcell.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    //we'll write script commands here
&lt;/script&gt;
&lt;/rel&gt;</pre></td></tr></table></div>

<h2>Step 2 – 放置gird</h2>
<p>有两种方式在一个页面放置grid，这里减少最常用的方法：创建一个div并给id熟悉设置一个惟一值。例如：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code45'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221745"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2217code45"><pre class="html" style="font-family:monospace;">  &lt;div id=&quot;mygrid_container&quot; style=&quot;width:600px;height:150px;&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>下面初始化参数，首先定一个mygrid变量，然后定一个doInitGrid方法，方法内部进行mygrid初始化工作：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code46'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221746"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2217code46"><pre class="javascript" style="font-family:monospace;"> var mygrid;
 function doInitGrid(){
 }</pre></td></tr></table></div>

<p>doInitGrid方法会包括以下代码：<br />
o 使用dhtmlXGridObject构造方法创建一个基于我们之前创建的DIV的grid对象；<br />
o 设置grid图片路径。这个路径包括grid外观需要的所有图片。在大多数情况下该路径为“codebase/imgs/”. 该路径最后面的一个“/”很重要。 随便说一下，这个路径和你处理表格数据所使用的图片没有关系；<br />
o 使用setHeader 方法定义表头；<br />
o 使用setInitWidths (单位为像素) 或setInitWidthsP (单位为百分比)定义列宽。 使用*代表让列自动使用所有表格宽度；<br />
o 定义一个列的水平对其方式。 Numeric values is better to align right;<br />
o 使用setSkin方法设置皮肤；<br />
o 最好使用这些设置通过init方法初始化grid。更多的参数之后再讨论。目前，doInitGrid方法如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code47'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221747"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2217code47"><pre class="javascript" style="font-family:monospace;">mygrid = new dhtmlXGridObject('mygrid_container');
mygrid.setImagePath(&quot;codebase/imgs/&quot;); //指定图片路径
mygrid.setHeader(&quot;Model,Qty,Price&quot;); //设置表头显示
grid.setInitWidths(&quot;*,150,150&quot;); //设置列的初始宽度
grid.setColAlign(&quot;left,right,right&quot;); //设置列的水平对其方式
mygrid.setSkin(&quot;light&quot;); //设置皮肤
grid.init(); //显示调用初始化方法，必须的</pre></td></tr></table></div>

<p>现在需要做的是运行该方法，可以将该方法加入body的onload方法里或是使用jquery的方法。下面使用body的onload方法：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code48'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221748"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2217code48"><pre class="html" style="font-family:monospace;">&lt;body onload=&quot;doInitGrid();&quot;&gt;&lt;/body&gt;</pre></td></tr></table></div>

<p>这样在该页面初始化之后会显示如下：</p>
<div class="pic"><img src="http://docs.dhtmlx.com/lib/exe/fetch.php?cache=&amp;media=dhtmlxgrid:step_2_last.png" alt="" /></div>
<p>说明：除了调用set方法之外，还可以如下风格定义：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code49'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221749"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p2217code49"><pre class="javascript" style="font-family:monospace;">mygrid = new dhtmlXGridObject({
		parent:&quot;a_grid&quot;,
		image_path:&quot;codebase/imgs&quot;,
		columns:[
			{ label: &quot;Sales&quot;,           width:50, 	type:&quot;ed&quot; },
			{ label:[&quot;Book title&quot;,
				 &quot;#text_filter&quot;],   width:150, 	type:&quot;ed&quot; },
			{ label:[&quot;Author&quot;,
				 &quot;#select_filter&quot;], width:150, 	type:&quot;ed&quot; },
			{ label: &quot;Price&quot;,       width:50, 	type:&quot;ed&quot; },
			{ label:&quot;In store&quot; , 	width:80, 	type:&quot;ch&quot; },
			{ label:&quot;Shipping&quot; , 	width:50, 	type:&quot;ed&quot; },
			{ label:&quot;Bestseller&quot; , 	width:50, 	type:&quot;ed&quot; },
			{ label:&quot;Date&quot; , 	width:50, 	type:&quot;ed&quot; }
		],
&nbsp;
		xml:&quot;data.xml&quot;
	});</pre></td></tr></table></div>

<h2>Step 3 – 填充数据</h2>
<p>你已经知道了dhtmlxGrid可以加载xml或cvs或json数据，这里主要演示dhtmlxGrid加载json数据。<br />
在上面的例子中每行有三列，故我们的json数据如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code50'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221750"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p2217code50"><pre class="javascript" style="font-family:monospace;">{
rows:[
{
        id: &quot;a&quot;,
        data: [Model 1, 100, 399]
},
{
id: &quot;b&quot;,
        data: [Model 2, 50, 649]
},
{
    id: &quot;c&quot;,
       data: [ Model 3, 70, 499]
}
]
}</pre></td></tr></table></div>

<p>将上面存于data.json文件，然后在doInitGrid方法里调用以下方法：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code51'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221751"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2217code51"><pre class="javascript" style="font-family:monospace;">mygrid.load (&quot;data.json&quot;，,&quot;json&quot;);</pre></td></tr></table></div>

<p>这时候页面展示如下：</p>
<div class="pic"><img src="http://docs.dhtmlx.com/lib/exe/fetch.php?cache=&amp;media=dhtmlxgrid:step_3.png" alt="" /></div>
<h2>Step 4 – 客户端排序</h2>
<p>为了能够实习表格的客户端排序，必须调用grid的setColSorting（sortTypesStr）方法。sortTypesStr是一个类型列表，以逗号分隔。该类型值有以下四种：<br />
o str – 作为字符串排序<br />
o int &#8211; 以Integer值排序 (通常可以是任何数字);<br />
o date – 以日期排序<br />
o custom sorting –自定义的更加复杂的排序方式(for example to sort days of week).</p>
<p>接下来我们对上面的例子进行排序。上例中每行有三列，第一列为字符串，后两列为数字，故可以调用以下方法进行排序。注意，该方法应该在init方法之前执行。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code52'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221752"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2217code52"><pre class="javascript" style="font-family:monospace;">mygrid.setColSorting(&quot;str,int,int&quot;);</pre></td></tr></table></div>

<p>这时候单击最后一列表头，结果如下：</p>
<div class="pic"><img src="http://docs.dhtmlx.com/lib/exe/fetch.php?media=dhtmlxgrid:step_4.png" alt="" /></div>
<h2>Step 5 – 单元格格式化和编辑</h2>
<p>Grid中使用单元格的编辑器（或是eXcells – 继承自 Cells, Cell 或 Columns types）来定义值的格式和编辑方式。你可以根据你的需要创建eXcells。</p>
<p>设定单元格的类型非常容易，其可以用一行代码定义。这里有一些常见的编辑器，如简单的编辑器代码为“ed”，多行编辑“txt”，只读单元格“ro”，复选框“ch”，价格的格式化“price”。<br />
默认情况下所有的列是“ro”，也可以使用以下方法类设置编辑类型：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code53'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221753"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2217code53"><pre class="javascript" style="font-family:monospace;">mygrid.setColTypes(&quot;ed,ed,price&quot;);</pre></td></tr></table></div>

<p>Excells格式化有以下几种：</p>
<div class="note"> link：超链接<br />
 img：图片<br />
 price：价格<br />
 dyn：动态行</div>
<p>Excell 复杂编辑器有以下几种：</p>
<div class="note"> cp：colorpicker<br />
 calck：允许调用grid.setNumberFormat的计算器<br />
 dhxCalendar：日历，日期格式可以通过grid.setDateFormat设置<br />
 dhxCalendarA：日历，日期格式可以通过grid.setDateFormat设置，单元格可以编辑<br />
 calendar：YUI Calendar<br />
 clist：多选组件</div>
<p>使用其他组件作为单元格编辑器</p>
<div class="note"> grid：使用dhtmlxgrid<br />
 stree ：使用dhtmlxtree<br />
 context：使用dhtmlxmenu<br />
 combo：使用dhtmlxCombo</div>
<p>Excells特别用途</p>
<div class="note">sub_row：允许单元格作为一个可展开的子单元格，就想查看明细一样。<br />
两个扩展<br />
o sub_row_ajax – 单元格数据被认为是ajax请求的url<br />
o sub_row_grid – 允许创建一个子表作为一个子行的内容</div>
<p>现在你可以双击或是F2进入编辑模式，你可以用tab键在单元格之间导航。</p>
<div class="pic"><img src="http://docs.dhtmlx.com/lib/exe/fetch.php?media=dhtmlxgrid:step_5.png" alt="" /></div>
<h2>Step 6 – 行操作方法</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code54'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221754"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2217code54"><pre class="javascript" style="font-family:monospace;">    function addRow(){
        var newId = (new Date()).valueOf()
        mygrid.addRow(newId,&quot;&quot;,mygrid.getRowsNum())
        mygrid.selectRow(mygrid.getRowIndex(newId),false,false,true);
    }
    function removeRow(){
        var selId = mygrid.getSelectedId()
        mygrid.deleteRow(selId);
    }</pre></td></tr></table></div>

<p>代码中addRow() 方法的一些说明：</p>
<div class="note">o 创建一个惟一值 (number of millisecond since 1970) 来作为row的标识；<br />
o 在最后一行后面添加一新行，该行有新的id，值为空；<br />
o 选中最近创建的行 (by index), 不掉用 On-Select事件，不掉用选中行之前事件并且聚焦到选中行(如果垂直滚动条存在，则滚动对应位置)。</div>
<p>代码中removeRow() 的一些说明（一行行的）:</p>
<div class="note">o 得到选中行id；<br />
o 删除指定行id的行</div>
<h2>Step 7 – 事件</h2>
<p>添加事件调用attachEvent 方法，如下行选中事件：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code55'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221755"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2217code55"><pre class="javascript" style="font-family:monospace;">    function doOnRowSelected(rowID,celInd){
        alert(&quot;Selected row ID is &quot;+rowID+&quot;\nUser clicked cell with index &quot;+celInd);
    }
    mygrid.attachEvent(&quot;onRowSelect&quot;,doOnRowSelected);</pre></td></tr></table></div>

<h2>Step 8 – Code</h2>
<p>最后的代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2217code56'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221756"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code" id="p2217code56"><pre class="javascript" style="font-family:monospace;"> &lt;title&gt;dhtmlxGrid Sample Page&lt;/title&gt;
     &lt;link rel=&quot;STYLESHEET&quot; type=&quot;text/css&quot; href=&quot;codebase/dhtmlxgrid.css&quot;/&gt;
     &lt;script src=&quot;codebase/dhtmlxcommon.js&quot;&gt;&lt;/script&gt;
     &lt;script src=&quot;codebase/dhtmlxgrid.js&quot;&gt;&lt;/script&gt;
     &lt;script src=&quot;codebase/dhtmlxgridcell.js&quot;&gt;&lt;/script&gt;
&nbsp;
     &lt;script&gt;
      var mygrid;
      function doInitGrid(){
       mygrid = new dhtmlXGridObject('mygrid_container');
       mygrid.setImagePath(&quot;codebase/imgs/&quot;);
       mygrid.setHeader(&quot;Model,Qty,Price&quot;);
       mygrid.setInitWidths(&quot;*,150,150&quot;);
       mygrid.setColAlign(&quot;left,right,right&quot;)
       mygrid.setSkin(&quot;light&quot;);
       mygrid.setColSorting(&quot;str,int,int&quot;);
       mygrid.setColTypes(&quot;ed,ed,price&quot;);
       mygrid.attachEvent(&quot;onRowSelect&quot;,doOnRowSelected);
       mygrid.init();
       mygrid.load (&quot;data.json&quot;,&quot;json&quot;);
      }
&nbsp;
      function addRow(){
       var newId = (new Date()).valueOf()
       mygrid.addRow(newId,&quot;&quot;,mygrid.getRowsNum())
       mygrid.selectRow(mygrid.getRowIndex(newId),false,false,true);
      }
      function removeRow(){
       var selId = mygrid.getSelectedId()
       mygrid.deleteRow(selId);
      }
&nbsp;
      function doOnRowSelected(rowID,celInd){
       alert(&quot;Selected row ID is &quot;+rowID+&quot;\nUser clicked cell with index &quot;+celInd);
      }
&nbsp;
     &lt;/script&gt;
&nbsp;
     &lt;body onload=&quot;doInitGrid()&quot;&gt;
      &lt;div id=&quot;mygrid_container&quot; style=&quot;width:600px;height:150px;&quot;&gt;&lt;/div&gt;
     &lt;/body&gt;</pre></td></tr></table></div>

<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="使用kettle数据迁移添加主键和索引" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2012%2F01%2Fadd-primary-keys-and-indexes-when-migrating-datas-whith-kettle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/01/05/13482495.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用kettle数据迁移添加主键和索引</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="渐行渐远" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F07%2Ffurther-and-further%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://www.javachen.com/wp-content/uploads/2010/07/2010worldc-300x197.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">渐行渐远</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="在eclipse中构建Pentaho BI Server工程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F09%2Fbuild-pentaho-bi-server-source-code-in-eclipse%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lihaifeng555/EntryImages/20090911/%25E6%2596%25B0%25E5%259B%25BE%25E7%2589%2587.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">在eclipse中构建Pentaho BI Server工程</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="房奴生活" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F10%2Ffinding_house_life%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/22/10383918.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">房奴生活</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Mondrian and OLAP" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F12%2Fmondrian-and-olap%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F07%2Fdhtmlxgrid-quick-start-guide%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/12/07/12038557.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Mondrian and OLAP</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>By the time  your rss reader get this post here is <strong> 2 </strong>comments ,Welcome you come to leave your opinion !]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/07/dhtmlxgrid-quick-start-guide/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.javachen.com/category/javascript/feed/ ) in 13.36947 seconds, on Feb 7th, 2012 at 8:59 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 7th, 2012 at 9:59 pm UTC -->
