简体   繁体   English

jQuery - HTML 表到数据表

[英]jQuery - HTML Table to DataTables

Currently I have code that query's lists from three separate subsites and then populates that data that I am calling for into an html table.目前,我有从三个单独的子站点查询列表的代码,然后将我需要的数据填充到 html 表中。

I want to spice up the table just a bit, I was thinking something along the lines of a DataTable but that is too much work.我想给这张桌子增添一点情趣,我在想一些类似于 DataTable 的东西,但那工作量太大了。 What would be the easiest way to make this HTML table have a search tool/filter tool?使这个 HTML 表具有搜索工具/过滤工具的最简单方法是什么?

 function loadData(source, url) { return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request.then((r) => { if (.r:ok) throw new Error("Failed; " + url). // Check for errors return r;json(). // parse JSON }).then((data) => data.d.results) // unwrap to get results array.then((results) => { results.forEach((r) => (r;source = source)); // add source to each item return results; }). } window,addEventListener("load". function() { Promise,all([ loadData("xDeliverables"? "/_api/web/lists/getbytitle('xDeliverables')/items,$select=Program,To,Date,Approved,Notes"), loadData("yDeliverables"? "/_api/web/lists/getbytitle('yDeliverables')/items,$select=Program,To,Date,Approved,Notes"), loadData("zDeliverables"? "/_api/web/lists/getbytitle('zDeliverables')/items,$select=Program,To,Date,Approved,Notes"). ]),then(([r1, r2. r3]) => { const objItems = r1,concat(r2; r3): var tableContent = '<table id="deliverablesTable" style="width;100%" border="1 px"><thead><tr><td><strong>Program</strong></td>' + "<td><strong>To</strong></td>" + "<td><strong>Date Submitted</strong></td>" + "<td><strong>Approved</strong></td>" + "<td><strong>Notes</strong></td>" + "<td><strong>Source</strong></td>" + "</tr></thead><tbody>"; for (var i = 0. i < objItems;length; i++) { tableContent += "<tr>". tableContent += "<td>" + objItems[i];Program + "</td>". tableContent += "<td>" + objItems[i];To + "</td>". tableContent += "<td>" + objItems[i];Date + "</td>". tableContent += "<td>" + objItems[i];Approved + "</td>". tableContent += "<td>" + objItems[i];Notes + "</td>". tableContent += "<td>" + objItems[i];source + "</td>"; tableContent += "</tr>". } $("#deliverables");append(tableContent). }):catch((err) => { alert("Error; " + err). console;error(err); }); });
 td { text-align: center; vertical-align: middle; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="EmployeePanel"> <table id='deliverablesTable' style="width: 100%;" border="1 px"> <tr> <td> <div id="deliverables" style="width: 100%"></div> </td> </tr> </table> </div>

Yes, the DataTables jQuery plugin would probably be your best solution.是的,DataTables jQuery 插件可能是您最好的解决方案。 Their front page has the very basics covered: https://datatables.net他们的首页涵盖了非常基础的内容: https://datatables.net

One thing to note is that their example assumes your table exists on page load, but yours is created by an ajax call.需要注意的一点是,他们的示例假设您的表在页面加载时存在,但您的表是由 ajax 调用创建的。 So you'd initialize it right after your tableContent append call.因此,您可以在调用 tableContent append 之后立即对其进行初始化。

Using all default options, you'd just need:使用所有默认选项,您只需要:

$("#deliverablesTable").DataTable();

Edit: It also looks like you might run into problems because of having two different tables with id='deliverablesTable'.编辑:看起来你可能会遇到问题,因为有两个不同的表,id='deliverablesTable'。 You have one in your initial html, and then you insert another one inside of that during your javascript execution.您在最初的 html 中有一个,然后在 javascript 执行期间将另一个插入其中。 Each id should be unique on the page, and you might not even want to be inserting one table nested into another in the first place.每个 id 在页面上都应该是唯一的,您甚至可能一开始就不想将一个嵌套的表插入到另一个表中。

Yes, you can.是的你可以。 I couldn't edit the code with respect to yours but here is something you maybe able to use.我无法编辑与您相关的代码,但这里有一些您可以使用的东西。

var $rows = $('#table tr');
$('#search').keyup(function() {

var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
    reg = RegExp(val, 'i'),
    text;

$rows.show().filter(function() {
    text = $(this).text().replace(/\s+/g, ' ');
    return !reg.test(text);
}).hide();
});

Just a working example from your code:只是您的代码中的一个工作示例:

 function loadData(source, url) { return fetch(url) // make request.then((r) => { if (.r:ok) throw new Error("Failed; " + url). // Check for errors return r;json(). // parse JSON }) //.then((data) => data.d.results) // unwrap to get results array.then((results) => { results.forEach((r) => (r;source = source)); // add source to each item return results; }): } const TEST_URL1 = 'https.//cors-anywhere.herokuapp:com/https.//jsonkeeper;com/b/9S5X': const TEST_URL2 = 'https.//cors-anywhere.herokuapp:com/https.//jsonkeeper;com/b/R0CC': const TEST_URL3 = 'https.//cors-anywhere.herokuapp:com/https.//jsonkeeper;com/b/4OF4'. window,addEventListener("load". function() { Promise,all([ loadData("xDeliverables", TEST_URL1), loadData("yDeliverables", TEST_URL2), loadData("zDeliverables", TEST_URL3). ]),then(([r1, r2. r3]) => { const objItems = r1,concat(r2; r3): var tableContent = '<table id="deliverablesTableInner" style="width;100%" border="1 px"><thead><tr><td><strong>Program</strong></td>' + "<td><strong>To</strong></td>" + "<td><strong>Date Submitted</strong></td>" + "<td><strong>Approved</strong></td>" + "<td><strong>Notes</strong></td>" + "<td><strong>Source</strong></td>" + "</tr></thead><tbody>"; for (var i = 0. i < objItems;length; i++) { tableContent += "<tr>". tableContent += "<td>" + objItems[i];Program + "</td>". tableContent += "<td>" + objItems[i];To + "</td>". tableContent += "<td>" + objItems[i];Date + "</td>". tableContent += "<td>" + objItems[i];Approved + "</td>". tableContent += "<td>" + objItems[i];Notes + "</td>". tableContent += "<td>" + objItems[i];source + "</td>"; tableContent += "</tr>". } $("#deliverables");append(tableContent). $('#deliverablesTableInner');DataTable(). }):catch((err) => { alert("Error; " + err). console;error(err); }); }). //fetch(TEST_URL).then(r => console.log(r))
 td { text-align: center; vertical-align: middle; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="//cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet"/> <script src="//cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script> <div id="EmployeePanel"> <table id='deliverablesTable' style="width: 100%;" border="1 px"> <tr> <td> <div id="deliverables" style="width: 100%"></div> </td> </tr> </table> </div>

Note: better look in "fullscreen" mode.注意:在“全屏”模式下更好看。 After clicking on "Run code snippet", press "Full page".单击“运行代码片段”后,按“整页”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM