I have a Django app that uses jQuery DataTables for a list view of items. Now I am trying to add a hyperlink to the first element of the table which is the primary key for the model I am displaying. The intent is for the link to take you to a detail view for the model instance. I have what I think should work but it is giving me "undefined" for each item in the datatable and throws an error when you click on it. I think it just isn't picking up the ID of the model instance.
I've tried any variation of the render function that I can think of but I'm new to javascript so I'm a bit lost. I am pretty confident that my app will work once I get this sorted out.
Here is my html:
{% extends "app/layout.html" %}
{% block content %}
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Part Requests</title>
</head>
<body>
<h1>Part Orders</h1>
<a class="btn btn-info" style="float:right" href="{% url 'New_Order' %}"> New Part Order </a>
<br /><br />
<div class="table-responsive">
<table id="PartOrders" class="table table-hover">
<thead>
<tr>
<th>Order #</th>
<th>Priority</th>
<th>Part Number</th>
<th>Part Description</th>
<th>Quantity</th>
<th>Order Date</th>
<th>Unit Name</th>
<th>UIC</th>
<th>End Item</th>
<th>Reason For Order</th>
</tr>
</thead>
</table>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
<script>
$(document).ready(function() {
$.get('/api/Order',
function(data) {
$('#PartOrders').DataTable( {
"data": data,
"columns": [
{ 'data': 'id', 'name': 'id',
"render": function(data, type, full) {
return '<a href="/order/'+ full.path + '">' + data.id +' </a>';
}
},
{ 'data': 'Priority', 'name': 'Priority' },
{ 'data': 'PartNumber', 'name': 'Part Number' },
{ 'data': 'PartDescription', 'name': 'Nomenclature' },
{ 'data': 'Quantity', 'name': 'Quantity' },
{ 'data': 'OrderDate', 'name': 'Order Date' },
{ 'data': 'UnitName', 'name': 'Unit Name' },
{ 'data': 'UIC', 'name': 'UIC' },
{ 'data': 'EndItem', 'name': 'Aircraft' },
{ 'data': 'ReasonForOrder', 'name': 'Reason For Order' }
]
});
});
});
</script>
</body>
</html>
{% endblock %}
{% block js %}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
{% endblock js %}
and here is my models in case that helps:
class PartOrder(models.Model):
id = models.AutoField(primary_key=True)
Priority = models.CharField(max_length=20, choices=(('AOG','AOG'),('Rush','Rush'),('Planned','Planned')))
PartNumber = models.CharField(max_length=35)
PartDescription = models.CharField(max_length=200)
SerialNumber = models.CharField(max_length=35)
UnitOfMeasure = models.CharField(max_length=35)
Quantity = models.PositiveIntegerField(validators=[MinValueValidator(1)])
OrderDate = models.DateTimeField(auto_now=True)
UnitName = models.CharField(max_length=35)
UIC = models.CharField(max_length=6)
ShippingAddress = models.CharField(max_length=200)
TimeSinceNew = models.CharField(max_length=35)
EndItem = models.CharField(max_length=20, choices=(('UH-72A','UH-72A'),('44301-10-7 Hoist','44301-10-7 Hoist')))
AircraftSN = models.CharField(max_length=7)
AircrafTime = models.DecimalField(max_digits=5, decimal_places=2)
Engine1SN = models.CharField(max_length=12)
Engine1_TSN = models.DecimalField(max_digits=5, decimal_places=2)
Engine2SN = models.CharField(max_length=12)
Engine2_TSN = models.DecimalField(max_digits=5, decimal_places=2)
ReasonForOrder = models.CharField(max_length=200)
RequestedBy = models.CharField(max_length=35)
class Meta:
ordering = ['Priority']
I think, you should refer to the first argument of render
callback:
render: function (data, type, full) => `<a href="/order/${data.path}">${data.id}</a>`
ajax
rather than wrapping your code with your own $.get()
success callback - it would give you more flexibility and better integration with DataTables API; <head>
section of the page; <table id="PartOrders" class="table table-hover"></table>
DataTables will populate that for you (including header, you simply need to change columns.name
option for columns.title
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.