Spring @ResponseBody获取数据以填充dataTable Jquery DataTable

时间:2014-12-21 10:50:24

标签: javascript jquery spring jsp datatable

首先,请忘掉我的英语。

我是JQuery和DataTable的新手。

我遇到了结果的JQuery dataTable问题并填充了列表。 我在控制器中有一个对象返回到ResponseBody从数据库中检索数据。到目前为止还好。 当我将数据发送到jsp时,只是以json格式显示结果,但不分配dataTable。我做错了什么或者我缺少什么才能正确呈现结果。

控制器

@SuppressWarnings("rawtypes")
@RequestMapping(value = "/loadAlbaranes", method = RequestMethod.GET)
public @ResponseBody XxxView loadAlbaranes(
        HttpServletResponse response,
        @Valid
        @ModelAttribute ("view") XxxView view,
        BindingResult result, Locale locale) {

    if(getLogService().getLog(this.getClass()).isDebugEnabled())
        getLogService().getLog(this.getClass()).info("loadAlbaranes <-- ");


    ValidationResultDTO validations = null;
    if(result.hasErrors()){
        validations = new ValidationResultDTO();
        validations.setMessage("Errores de validacion");
        validations.setErrors(validationMessages(result, locale));

        view.setValidationResultDTO(validations);

    }else{
        ...

        List<Qry011DTO> albaranesList;
        DataTableDTO<Qry011DTO> albaranesResult = new DataTableDTO<Qry011DTO>();
        try {
            albaranesList = albaranRPGService.getAlbaranes(params);

            albaranesResult.setAaData(albaranesList);
            int count = albaranesList.size();
            albaranesResult.setiTotalDisplayRecords(count);
            albaranesResult.setiTotalRecords(count);

            view.setAlbaranList(albaranesResult);

        } catch (Exception e) {
            getLogService().getLog(this.getClass()).error("Error recuperando datos de albaranes [ ERROR: "+e.getMessage() +" ]");

            ValidationErrorDTO valErr = new ValidationErrorDTO();

            validations = new ValidationResultDTO();
            validations.setMessage("Error");
            valErr.setMessage("Error recuperando datos de albaranes \n[ ERROR: "+e.getMessage().toString() +" ]");
            List<ValidationErrorDTO> errList = new ArrayList<ValidationErrorDTO>();
            errList.add(valErr);

            validations.setErrors(errList);

            view.setValidationResultDTO(validations);
        }

    }

    if(getLogService().getLog(this.getClass()).isDebugEnabled())
        getLogService().getLog(this.getClass()).info("loadAlbaranes --> ");

    return view;
}

在XxxView中我有private DataTableDTO<?> albaranList;及其各自的getter和setter,DataTableDTO具有结构

public class DataTableDTO<T> implements Serializable {

    private static final long serialVersionUID = 1991375252597373703L;

    private List<T> aaData;

    private Integer iTotalRecords;
    private Integer iTotalDisplayRecords;

    public List<T> getAaData() {
        return aaData;
    }
    public void setAaData(List<T> aaData) {
        this.aaData = aaData;
    }
    public Integer getiTotalRecords() {
        return iTotalRecords;
    }
    public void setiTotalRecords(Integer iTotalRecords) {
        this.iTotalRecords = iTotalRecords;
    }
    public Integer getiTotalDisplayRecords() {
        return iTotalDisplayRecords;
    }
    public void setiTotalDisplayRecords(Integer iTotalDisplayRecords) {
        this.iTotalDisplayRecords = iTotalDisplayRecords;
    }

}

这是按钮搜索的JS功能

function loadAlbaranes(){
         if (validateForm()) {
                var valueSerie = $("#frmPeparaPedidoFltr #serieAlbaran").val();
                var valueAlbaran = $("#frmPeparaPedidoFltr #numAlbaran").val();

                oTable = $('#table').dataTable({
                    "sAjaxSource" : '/loadAlbaranes',
                    "bProcessing": true,
                    "bServerSide" : true,
                    "fnServerParams": function ( aoData ) {
                            aoData.push( {"name":'serieAlbaran' , "value" : valueSerie} );
                            aoData.push( {"name":'numAlbaran' , "value" : valueAlbaran} );
                    },


                    "fnServerData": function ( sSource, aaData, fnCallback ) {

                        console.log(aaData);

                    },

                    "bDestroy": true, //Para reinstanciar la tabla.
                    "sPaginationType" : "bootstrap",
                    "aoColumns": [
                                   { "mData": "ptoStock" , sDefaultContent: "", bSortable: true, sWidth: "10%"},
                                   { "mData": "fecCarga" , sDefaultContent: "", bSortable: true, sWidth: "15%"},
                                   { "mData": "ptoVenta" , sDefaultContent: "", bSortable: true, sWidth: "15%"},
                                   { "mData": "unidades" , sDefaultContent: "", bSortable: false, sWidth: "15%"},
                                   { "mData": "codProducto" , sDefaultContent: "", bSortable: true, sWidth: "15%"},
                                   { "mData": "desProducto" , sDefaultContent: "", bSortable: false, sWidth: "40%"}

                               ],


                    "bFilter": false, //Quito el global search de la tabla
                    "bInfo": true,
                    "oLanguage" : {
                        "sLengthMenu" : "<spring:message code='datatable.info.filas.pagina' arguments='_MENU_'/>",
                        "sInfo" : "<spring:message code='datatable.info.sInfo' arguments='_START_ ; _END_ ; _TOTAL_' argumentSeparator=";"/>",
                        "sInfoEmpty" : "<spring:message code='datatable.info.sInfoEmpty'/>",
                        "sInfoFiltered" : "(<spring:message code='datatable.info.filtradas' arguments='_MAX_'/>)",
                        "sSearch" : "",
                        "sZeroRecords" : "<spring:message code='datatable.info.norecords'/>"
                    }       
                });          
         }

    }

这是jsp中的答案,只有json的结果。否显示页面的其余部分。

{"validationResultDTO":null,"serieAlbaran":"AB","numAlbaran":"479114","albaranList":{"aaData":[{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"80555","desProducto":"SABORES VAR.J","unidades":18},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"80554","desProducto":"VIT.SAB.VAR E","unidades":90},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81994","desProducto":"VIT.SAB.VAR F","unidades":9},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"71059","desProducto":"VIT.C.FR.VA G","unidades":36},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81997","desProducto":"GRIEGO FR VAR","unidades":24},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81995","desProducto":"ACTV.0%.VAR B","unidades":108},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"12723","desProducto":"ACTV.FI.VAR D","unidades":36},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"71062","desProducto":"ACTV.SJ.VAR C","unidades":54},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"31305","desProducto":"DANAC.BEVARX6","unidades":48},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"92795","desProducto":"VIT.SAB.VAR C","unidades":63},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"71061","desProducto":"MPD VAR DX4","unidades":9},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"8013","desProducto":"CUAJADA VARX2","unidades":72},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"25569","desProducto":"DPS MX.VARX4B","unidades":18},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81684","desProducto":"PS.B.VAR50X6A","unidades":75},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81642","desProducto":"PS.B.VAR50X6C","unidades":45},{"ptoStock":"0170","fecCarga":20141028,"ptoVenta":"350064298","codProducto":"81644","desProducto":"BEDINO VARX4A","unidades":16}],"iTotalRecords":16,"iTotalDisplayRecords":16}}

我需要获取albaranList属性才能填充de dataTable,如果出现错误,请获取validationResultDTO。

我不了解Ajax和回调函数,以便用dataTable jQuery获取结果。

提前谢谢。

更新

你好,我为没有提出解决方案而道歉,我完全忘记了。 因为IE不起作用reloadAjax因此返回执行从列表中加载数据的方法,不是很好的解决方案,但它是数据有限的结果的替代方案。

                        if(isMIE){
                        loadNumTeoricosMensajes();
                    }else{
                        var oSettings = $('#numTeoricoMensajesTable').dataTable().fnSettings();

                        updateUrl = oSettings.sAjaxSource.substring(1, oSettings.sAjaxSource.indexOf('?'));
                        oSettings.sAjaxSouce = updateUrl + new Date().getTime();

                        oTableNumTeoricoMensajesTable.fnClearTable(this);
                        oTableNumTeoricoMensajesTable.fnSettings(oSettings);
                        oTableNumTeoricoMensajesTable.fnReloadAjax();
                    }

0 个答案:

没有答案