This is my first time using the Yii2 Framework, so I don’t have much knowledge of the features or what it offers.
Currently I integrate Ajax as I do with all projects with script and a table that updates the data according to the option you select.
The issue is that the project occupies GridView for the tables and I was asked that all the tables were the same, ie, that the table I did now is with GridView and do not know how to implement Ajax with the Gridview.
I leave you my HTML work:
HTML
<div class="course-index container">
<div class="row">
<div class="col-sm-4">
<div class="panel">
<div class="panel-heading">
Cursos
</div>
<div class="panel-body">
<div class="refresh-course">
<table class="table">
<tbody id="course-item">
<?php foreach ($courses as $key): ?>
<tr class="course-<?= $key['id'] ?> course-tr" id="<?= $key['id'] ?>">
<td class="name"><?= $key['name'] ?></td>
<td><span class="badge badge-info"><?= $key['amount'] ?></span></td>
<td><a title="Listar estudiantes" class="list-course" data-id="<?= $key['id'] ?>"><i class="fa fa-chevron-right"></i></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-sm-8">
<div class="panel">
<div class="panel-heading">
Estudiantes inscritos
</div>
<div class="panel-body">
<div class="refresh-students">
<table class="table table-striped table-bordered tablesorter" id="report-table">
<thead>
<th>Email</th>
<th>Fecha inicio</th>
<th>% Completados</th>
<th>Tiempo realizado</th>
</thead>
<tbody id="resultData">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
Script
<script type="text/javascript">
initializate();
function initializate(){
$('#report-table > tbody').empty();
$("#course-item tr[id]").click(function(){
var order = $(this).attr('id');
$.ajax({
url: '<?= Url::to(['default/load/']); ?>',
type: 'POST',
data : {id: order},
success:function(data){
$('#report-table > tbody').empty();
var html = '';
data.data.forEach(element => {
html += '<tr>' +
'<td>' + element.email + '</td>' +
'<td style="text-align: center;">' + (element.start_date == null ? '-' : new Date(element.start_date).toLocaleDateString("en-US") ) + '</td>' +
'<td style="text-align: center;">' + element.percent + '</td>' +
'<td>' + element.time + '</td>' +
'</tr>';
});
$('#resultData').html(html);
}
});
});
};
</script>