原作者:Bryan Forbes
原文链接:http://dojotoolkit.org/documentation/tutorials/1.6/datagrid/
译者:zhuxw ([email protected])
鉴于DataGrid对于表格数据的有效呈现,它早已成为许多应用的核心组件之一。在本教程中,我们将着眼于如何定义grid的布局结构,并讨论DataGrid所采用的滚屏机制。
适用Dojo版本:1.6 (其实绝大部分内容自1.2开始就支持了——译者注)
【示例】
可以看到,DataGrid能够轻易地承载这份包含了17452项纪录的数据列表。在本教程中,我们将使用一份较小的数据集作为例子:只显示名人堂球员的击球统计。好了,让我们开始吧!
在后面的教程中,我们还将不断修这个示例的布局结构。要定义一个DataGrid的模样,我们需要在构造函数里给structure(结构)属性传递一些对象和数组。我们将从最小的可定义单元开始——单元格——直到最大的组成部分——视图。我们暂时不涉及如何从服务器端获取数据,但下一篇教程会涵盖这部分内容。
grid = new dojox.grid.DataGrid({ store: store, query: { id: "*" }, structure: [ { name: "First Name", field: "first", width: "84px" }, { name: "Last Name", field: "last", width: "84px" }, { name: "Bats", field: "bats", width: "70px" }, { name: "Throws", field: "throws", width: "70px" }, { name: "G", field: "totalG", width: "60px" }, { name: "AB", field: "totalAB", width: "60px" }, { name: "Games as Batter", field: "totalGAB", width: "120px" }, { name: "R", field: "totalR", width: "60px" }, { name: "RBI", field: "totalRBI", width: "60px" }, { name: "BB", field: "totalBB", width: "60px" }, { name: "K", field: "totalK", width: "60px" }, { name: "H", field: "totalH", width: "60px" }, { name: "2B", field: "total2B", width: "60px" }, { name: "3B", field: "total3B", width: "60px" }, { name: "HR", field: "totalHR", width: "60px" } ] }, "grid");【示例】
<style> .firstName { font-style: italic; } .lastName { font-weight: bold; } </style> <script> grid = new dojox.grid.DataGrid({ store: store, query: { id: "*" }, structure: [ { name: "First Name", field: "first", width: "84px", classes: "firstName" }, { name: "Last Name", field: "last", width: "84px", cellClasses: "lastName" }, { name: "Bats", field: "bats", width: "70px", cellStyles: "text-align: right;" }, { name: "Throws", field: "throws", width: "70px" }, { name: "G", field: "totalG", width: "60px" }, { name: "AB", field: "totalAB", width: "60px" }, { name: "Games as Batter", field: "totalGAB", width: "120px", styles: "text-align: center;" }, { name: "R", field: "totalR", width: "60px" }, { name: "RBI", field: "totalRBI", width: "60px" }, { name: "BB", field: "totalBB", width: "60px" }, { name: "K", field: "totalK", width: "60px" }, { name: "H", field: "totalH", width: "60px" }, { name: "2B", field: "total2B", width: "60px" }, { name: "3B", field: "total3B", width: "60px" }, { name: "HR", field: "totalHR", width: "60px" } ] }, "grid"); </script>【示例】
单元格定义中使用两个新的属性:rowSpan定义了一个单元格占用多少子行,而colSpan则定义了一个单元格占用多少列。
grid = new dojox.grid.DataGrid({ store: store, query: { id: "*" }, structure: [ [ { name: "First Name", field: "first", width: "84px", rowSpan: 2 }, { name: "Last Name", field: "last", width: "84px", rowSpan: 2 }, { name: "Bats", field: "bats", width: "70px", rowSpan: 2 }, { name: "Throws", field: "throws", width: "70px", rowSpan: 2 }, { name: "G", field: "totalG", width: "60px" }, { name: "AB", field: "totalAB", width: "60px" }, { name: "R", field: "totalR", width: "60px" }, { name: "RBI", field: "totalRBI", width: "60px" }, { name: "BB", field: "totalBB", width: "60px" }, { name: "K", field: "totalK", width: "60px" } ],[ { name: "Games as Batter", field: "totalGAB", colSpan: 2 }, { name: "H", field: "totalH" }, { name: "2B", field: "total2B" }, { name: "3B", field: "total3B" }, { name: "HR", field: "totalHR" } ] ] }, "grid");【示例】
grid = new dojox.grid.DataGrid({ store: store, query: { id: "*" }, structure: [ { noscroll: true, cells: [ { name: "First Name", field: "first", width: "84px" }, { name: "Last Name", field: "last", width: "84px" } ] },{ cells: [ [ { name: "Bats", field: "bats", width: "70px", rowSpan: 2 }, { name: "Throws", field: "throws", width: "70px", rowSpan: 2 }, { name: "G", field: "totalG", width: "60px" }, { name: "AB", field: "totalAB", width: "60px" }, { name: "R", field: "totalR", width: "60px" }, { name: "RBI", field: "totalRBI", width: "60px" }, { name: "BB", field: "totalBB", width: "60px" }, { name: "K", field: "totalK", width: "60px" } ],[ { name: "Games as Batter", field: "totalGAB", colSpan: 2 }, { name: "H", field: "totalH" }, { name: "2B", field: "total2B" }, { name: "3B", field: "total3B" }, { name: "HR", field: "totalHR" } ] ] } ] }, "grid");【示例】
grid = new dojox.grid.DataGrid({ store: store, query: { id: "*" }, structure: [ { noscroll: true, defaultCell: { width: "84px" }, cells: [ { name: "First Name", field: "first" }, { name: "Last Name", field: "last" } ] },{ defaultCell: { width: "60px" }, cells: [ [ { name: "Bats", field: "bats", width: "70px", rowSpan: 2 }, { name: "Throws", field: "throws", width: "70px", rowSpan: 2 }, { name: "G", field: "totalG" }, { name: "AB", field: "totalAB" }, { name: "R", field: "totalR" }, { name: "RBI", field: "totalRBI" }, { name: "BB", field: "totalBB" }, { name: "K", field: "totalK" } ],[ { name: "Games as Batter", field: "totalGAB", colSpan: 2 }, { name: "H", field: "totalH" }, { name: "2B", field: "total2B" }, { name: "3B", field: "total3B" }, { name: "HR", field: "totalHR" } ] ] } ] }, "grid");【示例】