JQuery pagination not working with AJAX script - html

Im using the Jquery for pagination of my data tables and it is working great. However, when i add some AJAX script to load data without page refresh, the pagination function is not working anymore. Can anyone point out my mistake? my code is as below:
My table:
<table id="data-table" class="table table-striped border " style="width:100%">
<thead>
<tr>
<th>No</th>
<th>License Plate</th>
<th>Status</th>
<th>Time</th>
<th>Date</th>
</tr>
</thead>
<tbody>
#foreach($bg_records as $bg_record)
#if($bg_record -> status == 1)
<tr id="">
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $bg_record -> lp}}</td>
<td> PASS</td>
<td>{{ Carbon\Carbon::parse($bg_record->timestamp)->format('h:i:s')}}</td>
<td>{{ Carbon\Carbon::parse($bg_record->timestamp)->format('d-m-Y')}}</td>
</tr>
#else
<tr id="">
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $bg_record -> lp}}</td>
<td> FAILED</td>
<td>{{ Carbon\Carbon::parse($bg_record->timestamp)->format('h:i:s')}}</td>
<td>{{ Carbon\Carbon::parse($bg_record->timestamp)->format('d-m-Y')}}</td>
</tr>
#endif
#endforeach
</tbody>
</table>
My AJAX script to load the table without page refresh
<script type="text/javascript">
var delay = 1000;
var refreshId = setInterval(function () {
$('#data-table').load(' #data-table');
}, delay);
</script>
JQuery pagination script
<script>
$(document).ready(function () {
$('#data-table').DataTable();
});
</script>

Related

Group datatables

I am using the datatable plugin :
I need to group row on the data on column 2 , I am using the the below code ,
HTML :
<table class="table table-stripped table-bordered table-sm mytable ">
<thead>
<tr>
<th>{{ __('code') }}</th>
<th >{{ __('value1') }}</th>
<th >{{ __('description') }}</th>
<th >{{ __('quantity') }}</th>
<th >{{ __('amount') }}</th>
<th >{{ __('total') }}</th>
<th ></th>
</tr>
</thead>
<tbody class="provider_cpts_div">
<tr>
<td>33</td>
<td>xx</td>
<td>www</td>
<td>ww</td>
<td>ww</td>
<td>ww</td>
<td>ww</td>
</tr>
<tr>
<td>33</td>
<td>xx</td>
<td>www</td>
<td>ww</td>
<td>ww</td>
<td>ww</td>
<td>ww</td>
</tr>
</tbody>
</table>
Script :
var collapsedGroups = {};
var CptTable = $('.mytable').DataTable({
rowGroup: {
// Uses the 'row group' plugin
dataSrc: 1,
startRender: function(rows, group) {
var collapsed = !!collapsedGroups[group];
rows.nodes().each(function(r) {
r.style.display = 'none';
if (collapsed) {
r.style.display = '';
}
});
// Add category name to the <tr>. NOTE: Hardcoded colspan
return $('<tr/>')
.append('<td >' + group + ' (' + rows.count() + ')</td>')
.attr('data-name', group)
.toggleClass('collapsed', collapsed);
}
}
});
$('.mytable tbody').on('click', 'tr.group-start', function() {
var name = $(this).data('name');
collapsedGroups[name] = !collapsedGroups[name];
CptTable.draw(false);
});
It is not working , it seems I missed something
this based on the sample :
https://jsfiddle.net/lbriquet/ua4yLscx/
I am not sure what I missed
the missing was the'row group' plugin ,
I added the .js and .css
<script src="https://cdn.datatables.net/rowgroup/1.0.2/js/dataTables.rowGroup.min.js"></script>
<link href="https://cdn.datatables.net/rowgroup/1.0.2/css/rowGroup.dataTables.min.css" rel="stylesheet">
this has fix my problem ...

Export HTML DataTable to Excel - Display URL to Image in Excel

I'm trying to display an HTML DataTables URL to the actual image in an Excel column.
Right now, when I export the DataTable to Excel it populates the corresponding column with the URL itself, what I want to achieve is to actually populate the Excel column with the Image, not the Image URL.
Right now, I can see the image for the comment.screenshot variable into HTML, but in Excel, it is blank, while for the rest of comment.screenshot1,comment.screenshot2 and comment.screenshot3, only the URL is populated in Excel.
jQuery:
$("#tableComments").DataTable({
stripHtml: !1,
dom: "Bfrtip",
buttons: ["copyHtml5", "excelHtml5", "csvHtml5", "pdfHtml5"]
})
HTML:
<table id="tableComments" class="display table table-info table-striped">
<thead>
<tr>
<th>User</th>
<th>Version</th>
<th>Round</th>
<th>Comment</th>
<th>Screenshot #1</th>
<th>Screenshot #2</th>
<th>Screenshot #3</th>
<th>Screenshot #4</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for comment in comments %}
<tr>
<td>{{ comment.user.username }}</td>
<td>{{ comment.versionName }}</td>
<td>{{ comment.round }}</td>
<td>{{ comment.comment }}</td>
<td><img src="{{ comment.screenshot }}" /></td>
<td>{{ comment.screenshot1 }}</td>
<td>{{ comment.screenshot2 }}</td>
<td>{{ comment.screenshot3 }}</td>
<td>{{ comment.approved }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I highly appreciate your help!
Instead of Datatables export buttons you may use a different approach using encodeURIComponent:
The snippet:
$("#tableComments").DataTable({
stripHtml: !1,
dom: 'Bfrtip',
buttons: ['copyHtml5', 'csvHtml5', 'pdfHtml5']
})
$('button').on('click', function(e) {
window.open('data:application/vnd.ms-excel,' + encodeURIComponent( document.getElementById('tableComments').outerHTML));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js"></script>
<button>Export To Excel</button>
<table id="tableComments" class="display table table-info table-striped">
<thead>
<tr>
<th>User</th>
<th>Version</th>
<th>Round</th>
<th>Comment</th>
<th>Screenshot #1</th>
<th>Screenshot #2</th>
<th>Screenshot #3</th>
<th>Screenshot #4</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>username1</td>
<td>versionName</td>
<td>round</td>
<td>comment </td>
<td><img src="https://dummyimage.com/100x100/000/fff&text=1"/></td>
<td>screenshot1</td>
<td>screenshot2</td>
<td>screenshot3</td>
<td>approved</td>
</tr>
<tr>
<td>username2</td>
<td>versionName</td>
<td>round</td>
<td>comment </td>
<td><img src="https://dummyimage.com/100x100/000/fff&text=2"/></td>
<td>screenshot1</td>
<td>screenshot2</td>
<td>screenshot3</td>
<td>approved</td>
</tr>
<tr>
<td>username3</td>
<td>versionName</td>
<td>round</td>
<td>comment </td>
<td><img src="https://dummyimage.com/100x100/000/fff&text=3"/></td>
<td>screenshot1</td>
<td>screenshot2</td>
<td>screenshot3</td>
<td>approved</td>
</tr>
</tbody>
</table>

Adding a link to a specific row

I'm trying to add an image to a specific row. I have numerous of images that I'm trying to add to a table and this Google image keeps popping up in every row instead of just one. I have a snippet of the code where I'm adding the image and the parameters for each row. I was trying to add the image by doing {{link.img}} like the website, URL, and description but that didn't work either. Below is a snippet of the code that has a screenshot of what's printing out. I would like to get {{link.img}} working if possible so I can be consistent with all the other parameters, but if that's not feasible that's fine. Any assistance will be greatly appreciated. Thanks
image of table
LINKS = [
{
'id': uuid.uuid4().hex,
'img': "../assets/ipts.png",
},
{
'id': uuid.uuid4().hex,
'img': "../assets/express.png",
}
]
<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Website</th>
<th scope="col">URL</th>
<th scope="col">Description</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="(link, index) in links" :key="index">
<!--<td>{{ link.img }}</td>-->
<td>
<img v-bind:src="link.img" width="21" height="21">
<img src = "../assets/google-logo.png" width="21" height="21">
</td>
<!--<td>{{ link.website }} </td>-->
<td>{{ link.website }}</td>
<td>{{ link.URL }}</td>
<td>{{ link.description }}</td>
<td></td>
</tr>
</tbody>
</table>
Use the object data keys without qoute sign as follows :
'id'=> id and ........
links = [
{
id: uuid.uuid4().hex,
img: "../assets/ipts.png",
},
{
id: uuid.uuid4().hex,
img: "../assets/express.png",
}
]

How would I link a copy button to a cell in a table?

I have a copy button script:
function myFunction() {
var table = document.getElementById('myTable');
var copyText = table.rows[1].cells[0].innerHTML;
copyText.select();
document.execCommand("copy");
alert("Copied");
}
And my table:
<table id="myTable">
{% for resp in results %}
<tr>
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
<td><button onclick="myFunction()">Copy text</button></td>
</tr>
{% endfor %}
</table>
I want the button to copy the text inside td {{ resp }} /td
function myFunction(val, event) {
var inp = document.createElement('input');
document.body.appendChild(inp)
inp.value = val;
inp.select();
document.execCommand('copy', false);
inp.remove();
alert('copied');
}
<table id="myTable">
<tr>
<td>one</td>
<td><button onclick="myFunction('one')">Copy text</button></td>
</tr>
<tr>
<td>two</td>
<td><button onclick="myFunction('two')">Copy text</button></td>
</tr>
<tr>
<td>three</td>
<td><button onclick="myFunction('three')">Copy text</button></td>
</tr>
<tr>
<td>four</td>
<td><button onclick="myFunction('four')">Copy text</button></td>
</tr>
</table>
a quick solution will be passing the text (to be copied) with in the function as argu.
ctrl + v to see the result.

Fat-free framework: how to display a calendar using the Matrix class?

$matrix = \Matrix::instance();
$cal = $matrix->calendar('2014-06', 1);
I can not work out how to use data in $cal & get it to display in view
The calendar() method returns an array of weeks.
Here's a basic example:
index.php
$f3=require('lib/base.php');
$f3->route('GET /cal/#year/#month',function($f3,$params){
$f3->cal=Matrix::instance()->calendar($params['year'].'-'.$params['month'],1);
$f3->title=date('F Y',mktime(0,0,0,$params['month'],1,$params['year']));
echo Template::instance()->render('cal.html');
});
$f3->run();
cal.html
<h1>{{ #title }}</h1>
<table>
<thead>
<tr>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
</tr>
</thead>
<tbody>
<repeat group="#cal" value="#week">
<tr>
<loop from="$d=0" to="$d<7" step="$d++">
<td>{{ ##week[ #d ] }}</td>
</loop>
</tr>
</repeat>
</tbody>
</table>
Now GET /cal/2014/06 should return something like: