清风阁☆四少 - 2007-2-4 22:33:00
In this post we are going to touch the key client side data components of Atlas – the DataTable, which is used to represent a tabular data structure. The DataTable object connects the actual data – the DataSource and the UI control – the ListView. It is the data field of a DataSource object, and can also be decorated by a DataView object by filtering and sorting the rows or doing the page navigations. Here Atlas also shares the ASP.NET/ADO.NET concepts: the Atlas DataSource is similar with ADO.NET SqlDataSource, the Atlas DataTable is similar with ADO.NET DataTable and the Atlas ListView is similar with ASP.NET ListView. Pretty easy to understand, isn't it? A DataTable contains a collection of DataColumn objects and a collection of DataRow objects, so let's begin with the DataColumn and DataRow.
Sys.Data.DataColumn
The DataColumn object is really simple, only following properties:
columnName: Column name string.
dataType: The type of data in this column.
defaultValue: The default value in this column.
isKey: Whether data in this column is the key of DataTable.
readOnly: Whether data in this column is read only.
Sys.Data.DataRow
Sys.Data.DataRow object is a little bit of complex, there’s following properties:
$isDirty: Whether this row is modified and not submitted to server yet.
$index: The index of current row in DataTable.
$selected: Whether this row is selected.
And following event:
propertyChanged: gets fired when one of the above properties is changed.
Sys.Data.DataTable
Now let start the DataTable, the DataTable object implements the Sys.Data.IData interface. Following properties:
columns: Current collection of DataColumn objects. You may add/update/delete a column when get the collection.
keyNames: The collection of key columns’ columnName property.
length: Length of row collection.
isDirty: Whether the DataRow collection is changed and not submitted to server yet.
And following methods:
add: Add a new row to current DataTable.
clear: Delete all rows in current DataTable.
remove: Remove an existing row in current DataTable.
createRow: Create a new row based on current DataTable’s schema.
getChanges: Get the changes of current DataTable. There are three parts in the result.
updated: The updated row collection.
inserted: The added row collection.
deleted: The delete row collection.
getColumn: Get a DataColumn object by column name.
getRow: Get a DataRow object by row index.
getItem: The same to getRow method.
Also events:
collectionChanged: Gets fired when the DataRow collection is modified.
propertyChanged: Gets fired when one of the properties above is changes.
清风阁☆四少 - 2007-2-4 22:35:00
Atlas provides Web.Data.DataTable control which is just like the behavior of ADO.NET DataTable object and the Atlas Framework can also make the conversion between the two types for you automatically. Here are some common operations on Atlas DataTable.
Get DataTable from server
Server side code in C#:
[WebMethod]
public DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
dt.Columns.Add(new DataColumn("LastName", typeof(string)));
dt.Columns.Add(new DataColumn("Email", typeof(string)));
dt.Rows.Add("Dflying", "Chen", "dflying@dflying.net");
dt.Rows.Add("Someone", "Else", "someone@someone.net ");
return dt;
}Client side code in JavaScript:
function getDataTable() {
MyWebService.GetDataTable(onComplete);
}
function onComplete(result) {
var dataTable = result;
}
Send DataTable to server
Client side code in JavaScript:
function sendDataTable() {
MyWebService.SendDataTable(myDataTable);
}Server side code in C#:
[WebMethod]
public void SendDataTable(DataTable someTable)
{
DataTable dt = someTable;
}Rows count of DataTable
var nRows = dataTable.length;Get data in a single cell of DataTable
var data = dataTable.getItem(rowIndex).getProperty(colName);Add a new row to DataTable
var oRow = { FirstName:'New', LastName:'Row', Email: 'new@new.com' }
dataTable.add(oRow);Update row
dataTable.getItem(rowIndex).setProperty(colName, value);Delete row
dataTable.remove(dataTable.getItem(rowIndex));DataTable changed
var hasChanges = dataTable.get_isDirty();Get updated/inserted/deleted rows
var updatedRows = dataTable.getChanges().updated;
var insertedRows = dataTable.getChanges().inserted;
var deletedRows = dataTable.getChanges().deleted;Get columns’ properties
var oCol = dataTable.getColumn(colName);
var colName = oCol.get_columnName();
var colDataType = oCol.get_dataType();
var colDefaultValue = oCol.get_defaultValue();Get DataTable from DataSet
DataSet is also an Atlas object, just like the ADO.NET object DataSet which may contain many DataTables.
var dataTable = dataSet[dataTableIndex];
LV侠者风范 - 2007-2-5 16:32:00
顶!~
清风阁☆四少 - 2007-2-27 23:23:00
顶~
© 2000 - 2026 Rising Corp. Ltd.