簡體   English   中英

jQuery Datatables:將返回的JSON傳遞給變量

[英]jQuery Datatables: Pass returned JSON to a variable

我正在使用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;。 以上是失敗的。

我希望我能正確閱讀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);
});

根據該文檔 ,它應該通過聲明一個全局變量(簡單的工作myjson在這種情況下),以及JSON數據在分配給它dataSrc功能:

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可以使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM