Asp.net mvc4到datatables js

时间:2014-07-30 20:05:43

标签: c# jquery asp.net asp.net-mvc-4 datatables-1.10

问题是Datatables JS处于“处理”状态并且Chrome调试抛出:

  

未捕获的TypeError:无法读取未定义的属性“长度”

“提供”数据表js的问题是(数据是一些linq查询):

var jsonData = Json(data);

return jsonData;

回复是(文字回复):

    [{
    "TotalCredito": 1649112940.000000,
    "TotalClientes": 1040,
    "Balance7": 188974066.000000,
    "Balance37": 25152742.000000,
    "Balance67": 53268069.000000,
    "Mes": 1
}, {
    "TotalCredito": 1910576150.000000,
    "TotalClientes": 941,
    "Balance7": 332301396.000000,
    "Balance37": 84407873.000000,
    "Balance67": -7053061.000000,
    "Mes": 2
}, {
    "TotalCredito": 1852843443.000000,
    "TotalClientes": 809,
    "Balance7": 300589569.000000,
    "Balance37": 87170595.000000,
    "Balance67": 41900708.000000,
    "Mes": 3
}, {
    "TotalCredito": 1736522626.000000,
    "TotalClientes": 747,
    "Balance7": 235758479.000000,
    "Balance37": 107699635.000000,
    "Balance67": 60831390.000000,
    "Mes": 4
}, {
    "TotalCredito": 1611546395.000000,
    "TotalClientes": 702,
    "Balance7": 201209547.000000,
    "Balance37": 59028449.000000,
    "Balance67": 64171607.000000,
    "Mes": 5
}, {
    "TotalCredito": 1306131513.000000,
    "TotalClientes": 697,
    "Balance7": 552835099.000000,
    "Balance37": 67349028.000000,
    "Balance67": 50490441.000000,
    "Mes": 6
}]

视图中的脚本函数是:

<script>
$(document).ready(function () {
    var datatable = $('#informe').dataTable({
        "language": { "url": "http://cdn.datatables.net/plug-ins/28e7751dbec/i18n/Spanish.json" },
        "bFilter": false,
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "somesupercoolurl",
            "type": "POST",
            "dataType": "json"
        },
        "columns": [
            { "data": "Balance7" },
            { "data": "Balance37" },
            { "data": "Balance67" },
            { "data": "Mes" },
            { "data": "TotalClientes" },
            { "data": "TotalCredito" }
        ],            
    });
});

最后,表格是:

<table id="informe">
        <thead>
            <tr>
                <th>Balance7</th>
                <th>Balance37</th>
                <th>Balance67</th>
                <th>Mes</th>
                <th>TotalClientes</th>
                <th>TotalCredito</th>
            </tr>
        </thead>        
    </table>

我觉得很奇怪,虽然信息格式正确,但无法处理。

1 个答案:

答案 0 :(得分:2)

最后,我解决了这个问题

在看到很多例子之后,我注意到在将json对象解析为datatables js之前,必须将这3个变量包含在json中;在控制器中:

var totalDatos = data.Count();
var jsonData = Json(new {
    iTotalDisplayRecords = totalDatos,
    iTotalRecords = totalDatos,
    aaData = data
});
return jsonData;

使用此'功能',json对象就像这样

{"iTotalDisplayRecords":6,"iTotalRecords":6,"aaData":[{"TotalCredito":1649112940.000000,"TotalClientes":1040,"Balance7":188974066.000000,"Balance37":25152742.000000,"Balance67":53268069.000000,"Mes":1},{"TotalCredito":1910576150.000000,"TotalClientes":941,"Balance7":332301396.000000,"Balance37":84407873.000000,"Balance67":-7053061.000000,"Mes":2},{"TotalCredito":1852843443.000000,"TotalClientes":809,"Balance7":300589569.000000,"Balance37":87170595.000000,"Balance67":41900708.000000,"Mes":3},{"TotalCredito":1736522626.000000,"TotalClientes":747,"Balance7":235758479.000000,"Balance37":107699635.000000,"Balance67":60831390.000000,"Mes":4},{"TotalCredito":1611546395.000000,"TotalClientes":702,"Balance7":201209547.000000,"Balance37":59028449.000000,"Balance67":64171607.000000,"Mes":5},{"TotalCredito":1306131513.000000,"TotalClientes":697,"Balance7":552835099.000000,"Balance37":67349028.000000,"Balance67":50490441.000000,"Mes":6}]}

视图中的表:

<table id="informe">
    <thead>
        <tr>
            <th>Mes</th> 
            <th>TotalCredito</th>                
            <th>TotalClientes</th>
            <th>Balance7</th>
            <th>Balance37</th>
            <th>Balance67</th>                               
        </tr>
    </thead>           
</table>

脚本是:

<script>
$(document).ready(function () {

    var arrayDatos = {
        'canal': $(" #ListaCanales ").val(),
        'anio': $(" #ListaAnios ").val(),
        'vendedorsigla': $(" #ListaVendedores ").val()
    };

    var datatable = $('#informe').dataTable({
        "language": { "url": "http://cdn.datatables.net/plug-ins/28e7751dbec/i18n/Spanish.json" },
        "bFilter": false,
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "mensualajax",
            "type": "POST",
            "dataType": "json",
            "data": arrayDatos
        },
        "columns": [                
            { "data": "Mes", "bSortable": false },
            { "data": "TotalCredito" },
            { "data": "TotalClientes" },
            { "data": "Balance7" },
            { "data": "Balance37" },
            { "data": "Balance67" }               

        ],            
    });
    $(" #FiltrarResultados ").click(function () {

        var arrayDatos = {
            'canal': $(" #ListaCanales ").val(),
            'anio': $(" #ListaAnios ").val(),
            'vendedorsigla': $(" #ListaVendedores ").val()
        };

        datatable.fnClearTable();
        $('#informe').dataTable({
            "bDestroy": true,               
            "language": { "url": "http://cdn.datatables.net/plug-ins/28e7751dbec/i18n/Spanish.json" },
            "bFilter": false,
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "mensualajax",
                "type": "POST",
                "dataType": "json",
                "data": arrayDatos
            },
            "columns": [
                { "data": "Mes", "bSortable": false },
                { "data": "TotalCredito" },
                { "data": "TotalClientes" },
                { "data": "Balance7" },
                { "data": "Balance37" },
                { "data": "Balance67" }

            ],
        });
    });
});

是重要的评论,我使用'click'函数重新加载whit ajax数据表,'click'函数几乎等于另一个,但我在数据表构造函数中聚合"bDestroy": true,来重新加载数据表(它不是很优雅,但工作)。

最后,我的新superduper控制器使用DatatablesJs来渲染,捕获和更新数据

        //repository with the query
        var repositorio = new Repositorios.InformeMensualController();

        //capture ajax
        string canal = String.Join("", Request.Form.GetValues("canal"));

        string auxAnio = String.Join("", Request.Form.GetValues("anio"));
        int anio = Convert.ToInt32(auxAnio);

        string auxVendedorCodigo = String.Join("", Request.Form.GetValues("vendedorsigla"));
        int vendedorCodigo = Convert.ToInt32(auxVendedorCodigo);

        //set up data
        var data = repositorio.CargaDatos(canal, anio, vendedorCodigo);

        //Transformación a JSON y Datatables JS.
        var totalDatos = data.Count();
        var jsonData = Json(new {
            iTotalDisplayRecords = totalDatos,
            iTotalRecords = totalDatos,
            aaData = data});

        return jsonData;

我希望这对某人有用

问候! :)