How to set a preset value within Editor (based on current data)?
How to set a preset value within Editor (based on current data)?
Hamjam31
Posts: 15Questions: 3Answers: 0
Hi there, I am trying to implement a function where when I pull up the editor form, it reads the current data, and if the data is blank, it will insert a default value. I have already implemented the "def" option however this only works for new entries while I am trying to work with existing entries. For context, I am using a Django REST API as the backend.
{% extends 'base.html' %}
{% block content %}
<h1 style="text-align: center; padding-top: 1em; padding-bottom: 1em;">Subject</h1>
<div class="row">
<div class="col-sm-12">
<table id="SubjectDataTable" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th scope="col">L Name</th>
<th scope="col">F Name</th>
<th scope="col">Dob</th>
<th scope="col">Sex</th>
<th scope="col">Race Preomb</th>
<th scope="col">Study</th>
<th scope="col">Mod Date</th>
<th scope="col">Mod Person</th>
<th scope="col">Ent Date</th>
<th scope="col">Ent Person</th>
<th scope="col">Note</th>
<th scope="col">Ss Num</th>
<th scope="col">Mrec Num</th>
<th scope="col">Research Status</th>
<th scope="col">Race</th>
<th scope="col">Ethnicity</th>
</tr>
</thead>
</table>
</div>
</div>
{% endblock content %}
{% block extra_js %}
<script>
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$(document).ready(function () {
editor = new $.fn.dataTable.Editor( {
ajax: "http://127.0.0.1:8000/test2/api/subject/editor/?format=datatables",
table: "#SubjectDataTable",
idSrc: "id",
fields: [ {
label: "L Name:",
name: "l_name",
}, {
label: "F Name:",
name: "f_name",
}, {
label: "Dob:",
name: "dob",
}, {
label: "Sex:",
name: "sex",
}, {
label: "Race Preomb:",
name: "race_preomb",
}, {
label: "Study:",
name: "study.id",
type: "select"
}, {
label: "Mod Date:",
name: "mod_date",
type: "datetime",
}, {
label: "Mod Person:",
name: "mod_person",
}, {
label: "Ent Date:",
name: "ent_date",
type: "datetime",
}, {
label: "Ent Person:",
name: "ent_person",
}, {
label: "Note:",
name: "note",
}, {
label: "Ss Num:",
name: "ss_num",
}, {
label: "Mrec Num:",
name: "mrec_num",
}, {
label: "Research Status:",
name: "research_status",
}, {
label: "Race:",
name: "race",
}, {
label: "Ethnicity:",
name: "ethnicity",
}]
});
editor.on('open', function (e, mode, action) {
if (mode == 'edit') {
var rowData = editor.edit(this.s.id);
if (!rowData.note) {
rowData.note = "Default Value";
editor.edit(this.s.id, rowData);
}}}
});
$("#SubjectDataTable").DataTable({
serverSide: true,
ajax: {
url: "http://127.0.0.1:8000/test2/api/subject/?format=datatables",
type: "GET",
},
dom: "Bfrtrip",
columns: [
{data: "l_name", },
{data: "f_name", },
{data: "dob",
render: function(data, type, row) {
return moment(data).format("YYYY-MM-DD")
}
},
{data: "sex", },
{data: "race_preomb", },
{data: "study.descr", },
{data: "mod_date", },
{data: "mod_person", },
{data: "ent_date", },
{data: "ent_person", },
{data: "note", },
{data: "ss_num", },
{data: "mrec_num", },
{data: "research_status", },
{data: "race", },
{data: "ethnicity", },
],
columnDefs: [
{ targets: [0], visible: true},
{ targets: '_all', visible: true},
],
select: {
style: "os",
},
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor },
{ extend: "collection",
text: "Export",
buttons: [
"excel", "csv",
]
}
]
});
});
</script>
{% endblock %}
Replies
You could use
initEdit
for that, something like:Please see this example demonstrating that. When the table loads, "Ashton Cox" doesn't have an allocated office. Editing his record automatically fills in "Boston",
Colin
Thank you very much! This is exactly what I needed!