WebGrid支持客户端进行数据的添加、修改、删除和更新,下面就分别介绍如何使用JavaScript对WebGrid进行客户端数据处理。
1.客户端为表格插入行
function DoAdd()
{
// retrieves WebGrid's object
var grid = ISGetObject("WebGrid1");
var newRow = grid.RootTable.NewRow(); // create new row object
var cells = newRow.GetCells(); // get WebGridCell collection
// populate new row object
cells.GetNamedItem("CustomerID").SetText("MARTI", true);
cells.GetNamedItem("CompanyName").SetText("Intersoft Solutions", true);
cells.GetNamedItem("ContactName").SetText("Martin Anderson", true);
cells.GetNamedItem("Country").SetText("Indonesia", true);
newRow.Update(); // insert new record
}
2. 客户端更新表格数据
function DoUpdate()
{
// retrieves WebGrid's object
var grid = ISGetObject("WebGrid1");
var selectedRow = grid.GetSelectedObject().ToRowObject(); // get selected row object
var cells = selectedRow.GetCells(); // get WebGridCell collection
// update the selected row's cells value
cells.GetNamedItem("CustomerID").SetText("JAMES", true);
cells.GetNamedItem("CompanyName").SetText("Brian Corporation", true);
cells.GetNamedItem("ContactName").SetText("James Brian", true);
cells.GetNamedItem("Country").SetText("Germany", true);
selectedRow.Update(); // update the selected row object
}
3. 客户端删除表格行
function DeleteRow()
{
// retrieves WebGrid's object
var grid = ISGetObject("WebGrid1");
var selectedObject = grid.GetSelectedObject(); // return object of type SelectedObject
if (selectedObject != null)
{
var selectedRow = selectedObject.ToRowObject(); // return WebGridRow object
selectedRow.Delete(); // delete the selected row
}
else
{
alert("Please select a row first.");
}
}