gpt4 book ai didi

javascript - jQuery 数据表 : Pass returned JSON to a variable

转载 作者:行者123 更新时间:2023-11-28 11:01:39 27 4
gpt4 key购买 nike

我正在使用 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);
});

关于javascript - jQuery 数据表 : Pass returned JSON to a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708835/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com