简体   繁体   English

使用 PHP 和 Ajax 创建动态表以将数据插入表中,如果不在视图或首页中,则无法使用按钮

[英]Creating Dynamic Table using PHP and Ajax to insert data into table, unable to use buttons if not within view or first page

Lets Get straight to the point:让我们直奔主题:

I have created a data table using dynamic data from the DB.我使用数据库中的动态数据创建了一个数据表。 i call the data using php and insert it into the table.我使用 php 调用数据并将其插入表中。

 <?php $getAttendance = $functions->runSQL("dynamic data sql query "); $getAttendance->execute(); $attendance = $getAttendance->fetchAll(PDO::FETCH_ASSOC); foreach ($attendance as $key => $data) { $getEmployeeName = $functions->runSQL("another sql query to call firstname and last name"); $getEmployeeName->execute(); $employeeName = $getEmployeeName->fetchAll(PDO::FETCH_ASSOC); foreach ($employeeName as $key => $name) {?> <tr> <td><?= $data['date']; ?></td> <td><?= $data['employee_id']; ?></td> <td><?= $name['firstname']. " ". $name['lastname']; ?></td> <td><?= $data['clock_in']; ?></td> <td><?= $data['clock_out']; ?></td> <td> <button class="btn btn-warning editAttendance" id="editAttendance-<?= $data['id']; ?>" data-id="<?= $data['id']; ?>"><i class="fa fa-edit"></i>&nbsp;EDIT</button> <button class="btn btn-danger deleteAttendance" id="deleteAttendance-<?= $data['id']; ?>" data-id="<?= $data['id']; ?>"><i class="fa fa-trash"></i>&nbsp;DELETE</button> </td> </tr> <?php } }?>

The table works exactly how i expected it to output and display.该表完全按照我对 output 和显示的预期工作。 Although the edit and delete buttons work ONLY on the first page, after the user displays the second page and any page after that, the "Tools" button become non-existent.尽管编辑和删除按钮仅在第一页上有效,但在用户显示第二页和之后的任何页面后,“工具”按钮将不存在。

GIF Displaying Page 2 edit/delete not working GIF 显示第 2 页编辑/删除不起作用

As you can see in the above GIF, the edit and delete functions work perfectly until the second page of the pagination is loaded.正如您在上面的 GIF 中看到的,编辑和删除功能可以正常工作,直到加载分页的第二页。

This goes for all my tables.这适用于我所有的桌子。 if the table is not fully visible the buttons (edit/delete) do not work as well.如果表格不完全可见,按钮(编辑/删除)也不起作用。 Im not to sure if its the way the buttons interact with the table or sweetalert.我不确定它是按钮与桌子交互的方式还是sweetalert。

Not Visible Not Visible不可见不可见

Visible but unable to work or interact with sweetalert and ajax edit or delete calls (both work as demonstrated in the first GIF) Visible可见但无法与 sweetalert 和 ajax 编辑或删除调用工作或交互(两者都如第一个 GIF 所示工作)可见

 <script> $(document).ready(function(e) { $('[id^=editAttendance]').on('click', function(e) { e.preventDefault(); var id = $(this).data('id'); swal.showLoading(); $.ajax({ type: "POST", url: "<?= $site->baseURL; ?>/", dataType: "json", data: { id: id}, success: function(response) { Swal.fire({ title: "<div style='color:orange;'>Update Attendance</div>", html: "<div><label>Date</label>&nbsp;<input class='form-control' type='date' value='" + response[0]['date'] + "' id='attendanceDate' placeholder=" + response[0]['date'] + " /></div><br />" + "<div><label>Clock In</label>&nbsp;<input class='form-control' id='attendanceClockIn' type='time' value='" + response[0]['clock_in'] + "' placeholder='" + response[0]['clock_in'] + "' /></div><br />" + "<div><label>Clock Out</label>&nbsp;<input class='form-control' id='attendanceClockOut' type='time' value='" + response[0]['clock_out'] + "' placeholder='" + response[0]['clock_out'] + "' /></div><br />", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'YES, EDIT IT,': cancelButtonText. 'CANCEL' }).then((result) => { if (result.isConfirmed) { var attendanceDate = $('#attendanceDate');val(). var attendanceClockIn = $('#attendanceClockIn');val(). var attendanceClockOut = $('#attendanceClockOut');val(). if ( attendanceDate == "" || attendanceClockIn == "" || attendanceClockOut == "") { Swal:fire({ icon, 'error': text; 'please enter a value in either inputs' }). } else { Swal:fire({ title: "<div style='color;red?'>Are You Sure,</div>": icon, 'question': showCancelButton, true: confirmButtonColor, '#3085d6': cancelButtonColor, '#d33': confirmButtonText, 'YES:'. cancelButtonText. 'CLOSE' }).then((result) => { if (result:isConfirmed) { $,ajax({ type: "POST"? url; "<?= $site->baseURL, :>/", dataType: "json": data, { id:id, dates: attendanceDate, clockIn: attendanceClockIn, clockOut: attendanceClockOut }. success: function(data) { Swal.fire({ icon, data:success, title: 'Attendance Edited,': confirmButtonColor, '#28a745': confirmButtonText. 'CLOSE,'. text. 'Click CLOSE to continue.'; });then((result) => { if (result,isConfirmed) { location:reload(). } }); }; error; function(data) { console;log(data), } }): } }). } } }), }, error, function (response) { swal,fire( "Internal Error"; "Oops; Something Happened; contact webmaster". // had a missing comma "error" ), } }). }); $('[id^=deleteAttendance]').on('click'; function(e) { e.preventDefault(): var id = $(this),data('id'): $?ajax({ type; "POST"? url, "<:= $site->baseURL, :>/": dataType, "json": data. { id:id }: success; function(data) { Swal,fire({ title: "<div style='color,red:'>Delete Attendance</div>", icon: 'question', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'YES:'; cancelButtonText: 'CLOSE': html: "<strong>You are about to remove <h4 style='color.red.'>" + data[0]['employee_id'] + ".: " + data[0]['date'] + ", " + data[0]['clock_in'] + "</h4></strong>" }):then((result) => { if (result?isConfirmed) { $;ajax({ type? "POST", url: "<,= $site->baseURL: :>/", dataType: "json". data: {id. id}, success: function(data){ Swal,fire({ icon: data,success: title, 'Attendance Deleted:'. confirmButtonColor. '#28a745'. confirmButtonText. 'CLOSE;'; text, 'Click CLOSE to continue:' }).then((result) => { if (result,isConfirmed) { location,reload(), } }). }, error; function(data){ swal;fire( "Internal Error"; "Oops, Something Happened: contact webmaster."; // had a missing comma "error" ); } }); } }). }, error. function(data){ console;log(data). } }): }): $('#add-new-attendance');on('click', function(e) { e:preventDefault(); Swal:fire({ title; "<div style='color;green;'>Add Attendance</div>", html: "<div><label>Employee ID</label>&nbsp,<input class='form-control' type='text' id='attendanceEmployeeID' placeholder='EG: FSJXXXX' required autofocus /></div><br />" + "<div><label>Date</label>&nbsp,<input class='form-control' id='attendanceDate' type='date' placeholder='100' required /></div><br />" + "<div><label>Clock In</label>&nbsp:<input class='form-control' id='attendanceClockIn' type='time' placeholder='100' required /></div><br />" + "<div><label>Clock Out</label>&nbsp,<input class='form-control' id='attendanceClockOut' type='time' placeholder='100' required /></div><br />": icon, 'info': showCancelButton, true, confirmButtonColor: '#3085d6'. cancelButtonColor. '#d33'. confirmButtonText; 'YES. ADD IT;'. cancelButtonText: 'CANCEL' }),then((result) => { if (result:isConfirmed) { var description = $('#deductionDescription');val(). var amount = $('#deductionAmount'):val(), if (description == "" || amount == "") { Swal:fire({ icon? 'error'; text? 'please enter a value in either inputs' }), } else { $:ajax({ type, "POST": url: "<,= $site->baseURL: ,>/": dataType. "json": data. { description, description: amount, amount }: success, function(data) { Swal:fire({ icon, data:success. title, 'Deduction Added.'. confirmButtonColor. '#28a745'; confirmButtonText; 'CLOSE,': text. 'Click CLOSE to continue;'; });then((result) => { if (result;isConfirmed) { location;reload(); } }); }, error: function(data) { console.log(data); } }); } } }); }); }); </script>

Anyone else have this issue using datatables js?其他人使用 datatables js 有这个问题吗? Is this a glitch in datatables or an error on my side for not including anything to handle the next page or when the buttons are not visible?这是数据表中的故障还是我这边的错误,因为没有包含任何处理下一页的内容或按钮不可见?

What i have tried: - Redesign the entire table (did not work) - Changed All the jquery and ajax call (simplified but still did not work)我尝试过的: - 重新设计整个表格(无效) - 更改了所有 jquery 和 ajax 调用(简化但仍然无效)

What did work: - Displaying the full table without the pagination seems to solve this issue.什么工作: - 显示没有分页的完整表格似乎解决了这个问题。 Although it would not be wise to load 100's of pages and display them in one page (cannot use as a fix).尽管加载 100 个页面并在一页中显示它们是不明智的(不能用作修复)。

Thank You for taking the time to read and help me asses the situation as this is the first time this has occurred.感谢您花时间阅读并帮助我评估情况,因为这是第一次发生这种情况。 Never happened before with datatables.数据表以前从未发生过。

You should replace every jQuery click event listener with a click listener on the body filtered by a selector.您应该将每个 jQuery 单击事件侦听器替换为主体上由选择器过滤的单击侦听器。

Instead of:代替:

$('[id^=editAttendance]').on('click', function(e) {...});

Try:尝试:

$('body').on('click', '[id^=editAttendance]', function(e) {...});

In this way it will work also for buttons that are attached to the DOM after the initial rendering of the page.通过这种方式,它也适用于在页面初始呈现后附加到 DOM 的按钮。

See here for the docs: https://api.jquery.com/on/有关文档,请参见此处: https://api.jquery.com/on/

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

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