Using dojo DataGrid as a DOM object(更利于大家查资料,就转一下)

Using dojo DataGrid as a DOM object

Meir Winston | Jun 29, 2009 | Comments (4)

Dojo DataGrid (dojo 1.3) Using DOM Object
Since I started using the dojo DataGrid I realized that there are some basic methods that are missing or hidden from the API. It is not so obvious how to achieve functionalities like setItems or getFilteredItems.

Prepare the web page:
Assuming the dojo library resides in the same directory as your web page, I will reference the necessary items inside theheader tag of the web page.

Import CSS stylesheets (in web page header)
<style>
    @import"dojo-release-1.3.1/dijit/themes/tundra/tundra.css";
    @import"dojo-release-1.3.1/dojo/resources/dojo.css";
    @import"dojo-release-1.3.1/dojox/grid/resources/Grid.css";
    @import"dojo-release-1.3.1/dojox/grid/resources/tundraGrid.css";
</style>

Include dojo.js (in web page header)
<script type="text/javascript"src="dojo-release-1.3.1/dojo/dojo.js"djConfig="parseOnLoad:true, isDebug:true"></script>

Include data grid files (in web page header)
<scripttype="text/javascript">
    dojo.require("dojox.grid.DataGrid");
    dojo.require("dojo.data.ItemFileWriteStore");
</style>

Creating the DataGrid
Inside the body tag I will create a container to host my DataGrid widget:
<divid="container"></div>
, and than the DataGrid in few steps:

  1. Create items
  2. Create a grid data object with the items
  3. Create a store containing the grid data
  4. Create a layout for the grid
  5. Create a configuration object for the DataGrid
  6. Create the DataGrid object
  7. Invoke the startup() function.

<script type="text/javascript">
var items = [
    {partNumber: "A1JFHT",serialNumber:"T3 LLB",description:"T3 LLB for lab"},
    {partNumber: "A2JFHT",serialNumber:"T3 ASD",description:"T3 ASD for lab"},
    {partNumber: "A3JFHT",serialNumber:"T23 LLB",description:"T23 LLB for lab"}
];

var gridData = {
    identifier: "partNumber",
    label: "partNumber",items: items
};

By setting the identifier to be partNumber, the datagrid will treat it as a unique ID and we must not have duplicates of ID's, otherwise the data will not show up in the grid.

var fileWriteStore = new dojo.data.ItemFileWriteStore({data: gridData}); //^^
var layout = [{cells:[[
    {field: "partNumber", name: "Part Number", width: "100px"},
    {field: "serialNumber", name: "Serial Number", width: "100px"},
    {field: "serialNumber", name: "Description", width: "230px"}
]]}];
var cfg = {
    id: "mytable",
    jsId: "mytable",
    widgetId: "mytable",
    store: fileWriteStore,
    clientSort: true,
    rowsPerPage: 20,
    rowSelector: '20px',
    structure: layout,
    style: "width: 500px; height: 200px; font-size: 14px;"
};
var widget =new dojox.grid.DataGrid(cfg, "container");
widget.startup();
</script/span>>

Get All Items
To get all items in the DataGrid it is not enough just to get the array of items from the store.
e.g.
function getAllItems(){
   return widget.store._arrayOfTopLevelItems;
}

The problem with this method might be visible when you have a large amount of rows in the DataGrid, then you will receive only the rows that are visible on the browser or a little bit more (loaded items only), the rest will be null references in the array. The DataGrid is smart enough to fetch onto the cash only the data that is visible to the user which sometimes can be a problem, but if you scroll slowly all the way down to the bottom, all rows will be cashed in the memory such that a call to getAllItems() will return all rows. Filtering the grid rows using the setQuery method or the filter method will cause the same problem when trying the get all items after applying a filter.
To handle this problem I explicitly fetched all rows prior to returning all items. After stumbling upon the dojo library I managed to extract the following code to load data grid items:

function loadItems(){
    widget.store.fetch({
        start: 0,
        count: widget.rowCount,
        query: widget.query,
        sort: widget.getSortProps(),
        queryOptions: widget.queryOptions,
        isRender: widget.isRender,
        onBegin: dojo.hitch(_widget, "_onFetchBegin"),
        onComplete: dojo.hitch(_widget,"_onFetchComplete"),
        onError: dojo.hitch(_widget, "_onFetchError")
    });
}

By setting the count to be the number of records in the DataGrid I'm forcing the widget to fetch all records.
The function below shows how to get all items ignoring filters.

function getItems(){
    var items = [];
    var count = _widget.rowCount;
    loadItems();
    for(var i = 0 ; i < count ; i++){
        var item = widget.getItem(i);
        items.push(item);
    }
    return items;
}

The method getDisplayedItems will return only the rows that are presented in the browser.

function getDisplayedItems(){
    var items = [];
    var count = _widget.rowCount;
    for(var i = 0 ; i < count ; i++){
        var item = widget.getItem(i);
        if(item !=null){
            items.push(item);
        }
    }
    return items;
}

Get Selected Items
function getSelectedItems(){
    return widget.selection.getSelected();
}

Filter Rows
This example shows how to filter the rows in the DataGrid using the setQuery method:
function filter(query){//query example: {fieldName: 'regularExpression' }
    widget.setQuery(query,{ignoreCase:true});
}

Connecting Dojo Events
The argument f is a function to be invoked on an event.

function connectOnMouseDown(f){
    return dojo.connect(widget, "onMouseDown", f);
}
function connectOnHeaderCellClick(f){
    return dojo.connect(widget, "onHeaderCellClick", f);
}
function connetcOnStyleRow(f){
    return dojo.connect(widget, "onStyleRow", f);
}
function connectOnRowSelected(f){
    return dojo.connect(widget, "onRowClick", f);
}

Set Data
To set the data I'm creating a new Store and invoke the setStore method

function setData(arr){
    var gridData = {label: "partNumber",items: arr};
    var fileWriteStore = new dojo.data.ItemFileWriteStore({data: gridData});
    widget.setStore(fileWriteStore);
}

Replace Items
In order to replace an Item I am removing the obsolete item and create a new one, this method might cause ordering problem, the previous row would not reside at the same index as the new one. Hence, a good alternative is to update each one of the row fields usingwidget.store.setValue(item,fieldName,newValue);
Remember the "partNumber" is the identifier of the grid

function replaceItem(item,newItem){
    widget.store.deleteItem(item);
    widget.store.save();
    newItem["partNumber"] = item["partNumber"];
    widget.store.newItem(newItem);
    widget.store.save();
}

Update Items
Here I am iterating over one's item fields and update each with the corresponding newItem's value
function updateItem(item,newItem){
    for(var k in newItem){
        if(k != "partNumber"){
            if(newItem[k].constructor == Object){
               //ignore object types
            }
            elseif(newItem[k].constructor == Array){
               //ignore array types
            }
            else{
                widget.store.setValue(item,k,newItem[k]);
            }
        }
    }
}



深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
要在 Dojo Datagrid 中实现数据删除功能,可以按照以下步骤进行: 1. 在 Datagrid 中添加一个列,用于显示删除按钮。 2. 在该列中定义一个自定义单元格模板,用于显示删除按钮。 3. 在该模板中添加一个点击事件处理程序,以便在单击删除按钮时触发删除操作。 4. 在删除操作处理程序中获取要删除的数据行,并将其从数据存储中删除。 以下是一个简单的示例代码,演示如何在 Dojo Datagrid 中实现数据删除功能: ```javascript require(["dojo/_base/lang", "dojo/_base/declare", "dojo/data/ItemFileWriteStore", "dojox/grid/DataGrid", "dojo/domReady!"], function(lang, declare, ItemFileWriteStore, DataGrid){ var data = { identifier: "id", items: [ { id: 1, name: "John Doe", age: 32 }, { id: 2, name: "Jane Smith", age: 25 }, { id: 3, name: "Bob Johnson", age: 45 } ] }; var store = new ItemFileWriteStore({data: data}); var grid = new DataGrid({ structure: [ { name: "Name", field: "name", width: "200px" }, { name: "Age", field: "age", width: "100px" }, { name: "Delete", field: "id", width: "100px", formatter: function(id){ return "<button data-dojo-type='dijit/form/Button'>Delete</button>"; }, cellType: dojox.grid.cells.Cell, editable: false, onClick: function(evt){ var row = grid.getItem(evt.rowIndex); store.deleteItem(row); store.save(); } } ], store: store, rowSelector: "20px" }, "grid"); grid.startup(); }); ``` 在这个示例中,我们添加了一个名为“Delete”的列,用于显示删除按钮。我们定义了一个自定义单元格模板,用于显示一个具有“Delete”标签的按钮。我们还添加了一个事件处理程序,以便在单击按钮时触发删除操作。在该处理程序中,我们获取要删除的数据行,并将其从数据存储中删除。最后,我们使用 ItemFileWriteStore 作为数据存储,并将其与 Datagrid 组件一起使用。 注意:该示例代码仅供参考,具体实现需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值