我正在使用:jquery.dataTables.js来自:https://datatables.net
我试图让每个tr都有一个链接,但是有些原因这个不起作用,我尝试在我的控制台上运行chrome并工作,有人可以解释我为什么我不能在我的元素中插入这个链接?< / p>
它与json数据有关吗?
HTML:
<div class=" dashboard">
<div class="col-md-8 no-padding">
<div class="form-group col-md-4 no-padding">
<select class="form-control" id="sel1">
<option value="Filter by">Filter by country </option>
<option value="All">All</option>
<option value="China">China</option>
<option value="EUA">EUA</option>
<option value="Spain">Spain</option>
</select>
</div>
</div>
<br>
<br>
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>First name</th>
<th>Place</th>
</tr>
</thead>
</table>
jquery的:
$(document).ready(function() {
var dt = $('#example').dataTable();
dt.fnDestroy();
});
$(document).ready(function() {
var url = 'http://www.json-generator.com/api/json/get/crcCiZXZfm?indent=2';
var table = $('#example').DataTable({
ajax: url,
columns: [{
data: 'name'
}, {
data: 'place'
}]
});
$('#sel1').change(function() {
if (this.value === "All") {
table
.columns(1)
.search('')
.draw();
} else {
table
.columns(1)
.search(this.value)
.draw();
}
});
});
$(document).ready(function() {
$('#example tbody tr').attr('onclick', "document.location = 'edit.html'");
});
jquery我想插入:
$('#example tbody tr').attr('onclick', "document.location = 'edit.html'");
jsfiddle但不是上面的jquery: http://jsfiddle.net/f7debwj2/
答案 0 :(得分:2)
使用多个$(document).ready()函数不是一个好选择。您可以使用数据表的回调函数在创建后执行某些功能。
updated fiddle is: http://jsfiddle.net/dssoft32/f7debwj2/4/
答案 1 :(得分:1)
将列传递给调用实例化表时,使用列上的render
属性。以下是the documentation的链接和以下示例:
$(document).ready(function() {
var dt = $('#example').dataTable();
dt.fnDestroy();
});
$(document).ready(function() {
var url = 'http://www.json-generator.com/api/json/get/crcCiZXZfm?indent=2';
var table = $('#example').DataTable({
ajax: url,
columns: [{
data: 'name',
"render": function(data, type, full, meta) {
return '<a href="http://stackoverflow.com">' + data + '</a>';
}
}, {
data: 'place',
"render": function(data, type, full, meta) {
return '<a href="http://www.bbc.com/sport/football">' + data + '</a>';
}
}]
});
$('#sel1').change(function() {
if (this.value === "All") {
table
.columns(1)
.search('')
.draw();
} else {
table
.columns(1)
.search(this.value)
.draw();
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-2.2.4/dt-1.10.13/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-2.2.4/dt-1.10.13/datatables.min.js"></script>
<div class=" dashboard">
<div class="col-md-8 no-padding">
<div class="form-group col-md-4 no-padding">
<select class="form-control" id="sel1">
<option value="Filter by">Filter by country</option>
<option value="All">All</option>
<option value="China">China</option>
<option value="EUA">EUA</option>
<option value="Spain">Spain</option>
</select>
</div>
</div>
<br>
<br>
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>First name</th>
<th>Place</th>
</tr>
</thead>
</table>
&#13;