datagrid進階——subGrid講解,datagridsubgrid
subGrid,也就是datagrid中再套一層datagrid,比較適合用於顯示一些帶有層次的資料,比如要顯示一批訂單,訂單包含具體的貨物。這樣一個需求,就可以用到subGrid。
我們可以先看看官網上subGrid的樣子來增強一下視覺上的感覺。
如何來實現呢?若只是用來顯示一下資料,其實還是簡單的。
看看demo的代碼量也不過區區幾十行,跟上思路很容易就可以理解了。
$(function(){ $('#dg').datagrid({ view: detailview, detailFormatter:function(index,row){ return '<div style="padding:2px"><table class="ddv"></table></div>'; }, onExpandRow: function(index,row){ var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv'); ddv.datagrid({ url:'datagrid22_getdetail.php?itemid='+row.itemid, fitColumns:true, singleSelect:true, rownumbers:true, loadMsg:'', height:'auto', columns:[[ {field:'orderid',title:'Order ID',width:200}, {field:'quantity',title:'Quantity',width:100,align:'right'}, {field:'unitprice',title:'Unit Price',width:100,align:'right'} ]], onResize:function(){ $('#dg').datagrid('fixDetailRowHeight',index); }, onLoadSuccess:function(){ setTimeout(function(){ $('#dg').datagrid('fixDetailRowHeight',index); },0); } }); $('#dg').datagrid('fixDetailRowHeight',index); } }); });
1、使用這個subGrid的視圖,需要使用到官網提供的datagrid-detailview.js,其中幫我們定義好了detailView這種顯示視圖。
在使用過程中,我們只需要在主datagrid中將預設的view用detailView替換掉就可以了。
2、detailFormatter,也就是產生的detail中的初始代碼,這裡放入一個table,之後可以將其動態建立成datagrid。
3、當主datagrid剛剛初始化完成時,並不顯示subGrid,此時subGrid並沒有存在的必要,我們也不去建立它。
那什麼時候去建立它呢?當我們展開主datagrid中的某條資料的時候,也就是在onExpandRow事件中,才去建立一個它“管轄”的subGrid。
4、建立subGrid的方法和一般建立datagrid的方式差不多,但是後面多了一些fixDetailRowHeight的操作
fixDetailRowHeight這個方法主要是為了調整subGrid的高度用的,如若不加,可能會造成subGrid的高度會出現錯位的情況。
不過這裡setTimeout的時間為0,那和直接調用function又有什麼區別呢?
silverlight datagrid 顯示選擇excel檔案的內容
用ASPX頁面 就行了,不要用銀光
Private Sub excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
With Response
.Buffer = True
.ContentType = "application/vnd.ms-excel "
.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8 ")
End With
DataGrid_CXMONEY.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid_CXMONEY.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
end sub
GRID在放在ASPX頁面上,綁定資料來源
這樣匯出非常快,比自己遞迴填充XLS儲存格不知道快多少.
但有個缺陷就是只能看,做統計時會有問題.
因為其實這段代碼原理就是用XLS中顯示HMTL而已,跟真正的XLS原始碼有區別的
silverlight中的datagrid匯出excel
用ASPX頁面 就行了,不要用銀光
Private Sub excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
With Response
.Buffer = True
.ContentType = "application/vnd.ms-excel "
.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8 ")
End With
DataGrid_CXMONEY.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid_CXMONEY.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
end sub
GRID在放在ASPX頁面上,綁定資料來源
這樣匯出非常快,比自己遞迴填充XLS儲存格不知道快多少.
但有個缺陷就是只能看,做統計時會有問題.
因為其實這段代碼原理就是用XLS中顯示HMTL而已,跟真正的XLS原始碼有區別的