DOJO:如何将选定的行从网格发送/删除到springMVC控制器

时间:2015-07-24 09:00:45

标签: spring-mvc dojo dgrid dstore

我正在使用dgrid / Grid将数据显示到网格中。我想从网格中添加/删除选定的行并更新数据库。我怎样才能做到这一点?任何帮助将不胜感激。

 require([ 'dojo/_base/declare', 'dstore/RequestMemory', 'dgrid/Grid',
            'dgrid/extensions/Pagination', 'dgrid/Selection' ],
            function(declare, RequestMemory, Grid, Pagination, Selection,
                    Dialog, Button) {
                var structure = [ {
                    label : "Value Date",
                    field : "valueDate"
                }, {
                    label : "Currency",
                    field : "currency"
                }];
 var grid = new (declare([ Grid, Pagination, Selection ]))({
                    collection : new RequestMemory({
                        target : 'getdata'
                    }),
                    columns : structure,
                    className : 'dgrid-autoheight',
                    loadingMessage : 'Loading data...',
                    noDataMessage : 'No results found.',
                }, 'grid');
               grid.startup();
            });

2 个答案:

答案 0 :(得分:0)

使用: -

require([
                "dojox.grid.EnhancedGrid",
                "dojox.grid.enhanced.plugins.Pagination",
                "dojo.data.ItemFileWriteStore",
                    "dojo/store/JsonRest",
                    'dojo/_base/array',
                    "dojo/store/Memory",
                    "dojo/store/Cache",
                    "dojox/grid/DataGrid",
                    "dojo/data/ObjectStore",
                    "dojo/request",
                    "dojo/query",
                    "dojo/domReady!",
                    "dojox/grid/_CheckBoxSelector"
                ], function(EnhancedGrid,Pagination,ItemFileWriteStore,JsonRest,array, Memory, Cache, DataGrid, ObjectStore,request, query){
                var myStore, grid;
                request.get("example.json", {
                    handleAs: "json"
                }).then(function(data){
                    var dataLength=data.object.length;
                    var arrayData=data.object;
                    console.log(data.object.length);
                        var data = {
                          identifier: 'id',
                          items: []
                        };
                        var data_list = arrayData;
                        var rows = dataLength;
                        for(var i=0, l=data_list.length; i<rows; i++){
                          data.items.push(dojo.mixin({ id: i+1 }, data_list[i%l]));
                        }
                        var store = new dojo.data.ItemFileWriteStore({data: data});

                        /*set up layout*/
                        var layout =[ {
                                label : "Value Date",
                                field : "valueDate"
                            }, {
                                label : "Currency",
                                field : "currency"
                            }];

                        /*create a new grid:*/
                        var grid = new dojox.grid.EnhancedGrid({
                            id: 'grid',
                            store: store,
                            structure: layout,
                            rowSelector: '20px',
                            plugins: {
                              pagination: {
                                  pageSizes: ["25", "50", "100", "All"],
                                  description: true,
                                  sizeSwitch: true,
                                  pageStepper: true,
                                  gotoButton: true,
                                          /*page step to be displayed*/
                                  maxPageStep: 4,
                                          /*position of the pagination bar*/
                                  position: "bottom"
                              }
                            }
                        }, document.createElement('div'));

                        /*append the new grid to the div*/
                        dojo.byId("gridDiv").appendChild(grid.domNode);

                        /*Call startup() to render the grid*/
                        grid.startup();

                        query("#delete_c").on("click", function(){
                        var select_row = grid.selection.getSelected();
                        var array_data=[];
                        array.forEach(select_row, function(selectedItem){
                            if(selectedItem !== null){
                            /* Delete the item from the data store: */
                            store.deleteItem(selectedItem);
                            array_data.push(selectedItem);
                            } /* end if */
                        });
                        console.log(array_data);
                         dojo.xhrDelete({
                            url :"delete_row",
                            postData: dojo.toJson(array_data),
                            handleAs: "json",
                            headers: { "Content-Type": "application/json"},
                            load: function(response, ioArgs){
                            console.log(response);
                            if(response.status=="SUCCESS"){
                                continueDialog.onCancel();
                            }
                            }
                         });
                    });

              });
        });

使用相同的删除功能进行更改后的添加和更新方法。

答案 1 :(得分:0)

您可以使用以下内容获取所选行数据:

.rootBorder {
    -fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.4), 10, 0.5, 0.0, 0.0);
}

获取所选行数据后,您可以使用Dojo XHR组件('dojo / _request / xhr')向服务器发送任何所需的请求。该请求将返回包含响应的Promise。之后,您只需刷新网格或向商店发出事件即可更新值。

对服务器的删除请求应该是这样的,假设你在服务器端经历了一个JSON:

function getSelectedRowsData(grid) {
    var rowsData = [];
    for (var id in grid.selection) {
        if (grid.selection[id]) {
            rowsData.push(grid.row(id).data);
            }
    }
    return rowsData;
}