jQuery Datatables:将返回的JSON传递给变量

时间:2017-03-10 01:59:24

标签: javascript jquery json ajax datatables

我正在使用2个插件......

这是我的问题和代码......

$(document).ready(function() {
    var myjson;

//Initialize Datatable
    var newtable = $('#pdf-results').DataTable({
        "ajax": {
            "url": "http://www.example.com/home/Dummy_JSON_data.js",
            "dataSrc": function(json) {
             myjson: json; // This is the problem. I am not sure how to assign returned JSON to a variable ?
            }
        }
    });

// On button click, pass the returned JSON results to Defiant code below for searching and redraw Datatable.

    $("button").click(function() {
        var cname = $("#name").val();
        console.log('cname', cname);
        var cyear = $("#year").val();

        var rawXPath_cName = '//*[(contains(courseName, "' + cname + '") or contains(courseCode, "' + cname + '")) and contains(Year, "' + cyear + '")]';
        //console.log(rawXPath_cName);
        try {
            var reds = JSON.search(myjson, rawXPath_cName);
            var table_body = '';
            for (var i = 0; i < reds.length; i++) {
                table_body += '<tr>';
                table_body += '<td>' + reds[i].courseCode + '</td>';
                table_body += '<td>' + reds[i].courseName + '</td>';
                table_body += '<td>' + reds[i].Year + '</td>';
                table_body += '<td>' + reds[i].Trimester + '</td>';
                table_body += '<td><a href = ' + reds[i].pdfURL + '>Download map</a></td>';
                table_body += '</tr>';
            }
            $("tbody").empty();
            $("tbody").append(table_body);
            newtable.ajax.reload(); // Also, not sure if this is required or not. 
            //When the table redraws based on user search query, datatables doesn't display pagination correctly. It sometimes it shows 4-5 rows on page 1 and 4-5 rows on page 2, instead of showing upto 10 rows on page 1, which is the default behavior.

        } catch (e) {
            console.log('No results found');
        }
    });
});

我需要将Ajax调用返回的数据分配给变量,以便我可以在defiant.js代码中使用这些结果来搜索结果集。基本上,上面的代码 myjson:json; 是失败的。

2 个答案:

答案 0 :(得分:0)

我希望我能正确阅读API,但看起来你想使用ajax.json() - https://datatables.net/reference/api/ajax.json()

var newtable = $('#pdf-results').DataTable({
    "ajax": {
        "url": "http://www.example.com/home/Dummy_JSON_data.js"
    }
});

//new code
newtable.on('xhr', function(){
    var json = newtable.ajax.json();
    myjson = json.data; //bind to global variable
    alert(json.data);
});

答案 1 :(得分:0)

根据the docs,它应该只通过声明一个全局变量(在这种情况下为myjson),并在dataSrc函数中为其分配JSON数据:

var newtable = $('#pdf-results').DataTable({
        "ajax": {
            "url": "http://www.example.com/home/Dummy_JSON_data.js",
            "dataSrc": function(json) {
                 myjson = json; // '=', not ':'
                 return json;
            }
        }
    });

在您的代码中,您尝试使用:而不是=将json分配给变量。也许这就是它失败的原因?

另外,不要忘记在dataSrc函数中返回数据,因此DataTables可以使用它。

相关问题