Fix issues when adding a record, and sort and/or search is involved.

Previously this caused the newly added record to either appear at the
bottom, or not appear at all. Now it will always be added at the top,
and whatever search present is cleared.
This commit is contained in:
Joachim Tingvold 2016-08-19 17:56:28 +00:00
parent 4ff755bc20
commit 480a00bb87

View File

@ -71,38 +71,28 @@
{% if domain.type != 'Slave' %} {% if domain.type != 'Slave' %}
<td width="6%"> <td width="6%">
{% if record.is_allowed() %} {% if record.is_allowed() %}
<button type="button" class="btn btn-flat btn-warning button_edit" id="{{ (record.name,domain.name)|display_record_name }}"> <button type="button" class="btn btn-flat btn-warning button_edit" id="{{ (record.name,domain.name)|display_record_name }}">Edit&nbsp;<i class="fa fa-edit"></i></button>
Edit&nbsp;<i class="fa fa-edit"></i>
</button>
{% else %} {% else %}
<button type="button" class="btn btn-flat btn-warning""> <button type="button" class="btn btn-flat btn-warning"">&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;</button>
&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;
</button>
{% endif %} {% endif %}
</td> </td>
<td width="6%"> <td width="6%">
{% if record.is_allowed() %} {% if record.is_allowed() %}
<button type="button" class="btn btn-flat btn-danger button_delete" id="{{ (record.name,domain.name)|display_record_name }}"> <button type="button" class="btn btn-flat btn-danger button_delete" id="{{ (record.name,domain.name)|display_record_name }}">Delete&nbsp;<i class="fa fa-trash"></i></button>
Delete&nbsp;<i class="fa fa-trash"></i>
</button>
{% else %} {% else %}
<button type="button" class="btn btn-flat btn-warning""> <button type="button" class="btn btn-flat btn-warning"">&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;</button>
&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;
</button>
{% endif %} {% endif %}
{% else %} {% else %}
<td width="6%"> <td width="6%">
<button type="button" class="btn btn-flat btn-warning""> <button type="button" class="btn btn-flat btn-warning"">&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;</button>
&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;
</button>
</td> </td>
<td width="6%"> <td width="6%">
<button type="button" class="btn btn-flat btn-warning""> <button type="button" class="btn btn-flat btn-warning"">&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;</button>
&nbsp;&nbsp;<i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;
</button>
</td> </td>
{% endif %} {% endif %}
</td> </td>
<!-- hidden column that we can sort on -->
<td>1</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -145,8 +135,18 @@
}, },
"retrieve" : true, "retrieve" : true,
"columnDefs": [ "columnDefs": [
{ type: 'natural', targets: [0, 4] } {
] type: 'natural',
targets: [0, 4]
},
{
// hidden column so that we can add new records on top
// regardless of whatever sorting is done
visible: false,
targets: [ 7 ]
}
],
"orderFixed": [[7, 'asc']]
}); });
// handle delete button // handle delete button
@ -214,15 +214,17 @@
// handle add record button // handle add record button
$(document.body).on("click", ".button_add_record", function (e) { $(document.body).on("click", ".button_add_record", function (e) {
if (nNew || nEditing) { if (nNew || nEditing) {
// TODO: replace this alert with modal var modal = $("#modal_error");
alert("Previous record not saved. Please save it before adding more record.") modal.find('.modal-body p').text("Previous record not saved. Please save it before adding more record.");
modal.modal('show');
return; return;
} }
var table = $("#tbl_records").DataTable(); // clear search first
$("#tbl_records").DataTable().search('').columns().search('').draw();
var aiNew = table.row.add(['', 'A', 'Active', 3600, '', '', '']).draw();
var nRow = aiNew.index(); // add new row
editRow(table, nRow); var nRow = jQuery('#tbl_records').dataTable().fnAddData(['', 'A', 'Active', 3600, '', '', '', '0']);
editRow($("#tbl_records").DataTable(), nRow);
nEditing = nRow; nEditing = nRow;
nNew = true; nNew = true;
}); });