gpt4 book ai didi

jquery - DataTables如何用超链接填充列?

转载 作者:行者123 更新时间:2023-12-01 04:39:49 27 4
gpt4 key购买 nike

我正在使用 DataTables 服务器端处理,并希望在某些表格的第一个单元格中添加超链接。

我可以添加一列并使表格正确呈现,但我不知道如何将超链接添加到单元格中。

我不希望每个 table 上都有这个,现在只适合其中两个,但这可能会发生变化。

我对所有表使用相同的初始化,但所有表不具有相同的列。可能有 3 到 65 列,具体取决于我要渲染的表,因此我不能只将列添加到初始化中。这是我现在初始化的方式:

$(document).ready(function ()
{
// Setup - add a text input to each footer cell
$('#DataTable tfoot th').each(function ()
{
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});

var table = $('#DataTable').DataTable({
"lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
"dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
"buttons": [{
extend: 'collection',
text: 'Selection',
buttons: ['selectAll', 'selectNone']
}, {
extend: 'collection',
text: 'Export',
buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
},

{
text: 'Export All to Excel',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('draw', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };

$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
}).draw();
}
}]
}
],
"fixedHeader": {
header: true,
footer: true
},
"select": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": "./ServerSide.php",
"type": "POST"
},
//Added this to the initialization
columnDefs: [
{
targets: 0,
render: function (data, type, row, meta)
{
if (type === 'display')
{
data = '<a href="FormToEdit.php?everything=' + encodeURIComponent(row) + '">' + data + '</a>';
}
return data;
}
}],
//It adds the hyperlink to all tables and not just the ones that I want
initComplete: function ()
{
var api = this.api();

// Apply the search
api.columns().every(function ()
{
var that = this;

$('input', this.footer()).on('keyup change', function ()
{
if (that.search() !== this.value)
{
that
.search(this.value)
.draw();
}
});
});
}
});
});

以下是我创建表格的方法:

<?php
$hsql = "select Headings from TableHeadings where TableName = '$TableName' order by Id";

$getHeadings = $conn->query($hsql);
$rHeadings = $getHeadings->fetchALL(PDO::FETCH_ASSOC);
$CountHeadings = count($rHeadings);
$tsqlHeadings = '';
for ($row = 0; $row < $CountHeadings; $row++)
{
$headings[$row] = $rHeadings[$row]["Headings"];
}
?>
<table id="DataTable" class="display nowrap" style="width: 100%; border: 1px">
<thead>
<tr>
<?php
foreach($headings as $heading)
{?>
<th class="cell"><?php echo $heading; ?></th><?php
}?>
</tr>
</thead>
<tfoot>
<tr>
<?php
foreach($headings as $heading)
{?>
<th class="cell"><?php echo $heading; ?></th><?php
}?>
</tr>
</tfoot>
</table>

编辑

我已经用我添加的内容编辑了上面的初始化脚本。现在的问题是,它将此超链接添加到所有表,而不仅仅是具有 编辑 列的表。如果第一个列标题是编辑,我该如何进一步修改它以使其仅出现在表格上?

最佳答案

我决定以不同的方式解决这个问题。

可编辑的表格将被赋予不同的ID,然后使用上述代码进行初始化。其余部分将被初始化,而无需在第一列中创建超链接的部分。所以现在我有这个来创建我的表:

<table id="<?php if($Edit == 1){echo "DataTableEdit";}elseif($Edit == 0){echo "DataTable";}?>" class="display nowrap" style="width: 100%; border: 1px">

因此,如果他们有 $Edit = 1,他们将获得不同的初始化。新脚本现在如下所示:

<script type="text/javascript" class="init">
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
"text": "Export All Test",
action: function (e, dt, node, config)
{
alert('Export All Test');
}
};

$(document).ready(function ()
{
// Setup - add a text input to each footer cell
$('#DataTableEdit tfoot th').each(function ()
{
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});

var table = $('#DataTableEdit').DataTable({
"lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
"dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
"buttons": [{
extend: 'collection',
text: 'Selection',
buttons: ['selectAll', 'selectNone']
}, {
extend: 'collection',
text: 'Export',
buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
},

{
text: 'Export All to Excel',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('draw', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };

$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
}).draw();
}
}]
}
],
"fixedHeader": {
header: true,
footer: true
},
"select": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": "./ServerSide.php",
"type": "POST"
},
columnDefs: [
{
targets: 0,
render: function (data, type, row, meta)
{
if (type === 'display')
{
data = '<a href="FormToEdit.php?everything=\'' + encodeURIComponent(row) + '\'">' + data + '</a>';
}
return data;
}
}],
initComplete: function ()
{
var api = this.api();

// Apply the search
api.columns().every(function ()
{
var that = this;

$('input', this.footer()).on('keyup change', function ()
{
if (that.search() !== this.value)
{
that
.search(this.value)
.draw();
}
});
});
}
});
});
</script>
<script type="text/javascript" class="init">
$.fn.dataTable.ext.buttons.export =
{
className: 'buttons-alert',
"text": "Export All Test",
action: function (e, dt, node, config)
{
alert('Export All Test');
}
};

$(document).ready(function ()
{
// Setup - add a text input to each footer cell
$('#DataTable tfoot th').each(function ()
{
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});

var table = $('#DataTable').DataTable({
"lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
"dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
"buttons": [{
extend: 'collection',
text: 'Selection',
buttons: ['selectAll', 'selectNone']
}, {
extend: 'collection',
text: 'Export',
buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
text: 'Export Current Page',
exportOptions: {
modifier: {
page: 'current'
}
},
customize: function (xlsx)
{
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row:first c', sheet).attr('s', '7');
}
},

{
text: 'Export All to Excel',
action: function (e, dt, button, config)
{
dt.one('preXhr', function (e, s, data)
{
data.length = -1;
}).one('draw', function (e, settings, json, xhr)
{
var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
var addOptions = { exportOptions: { 'columns': ':all'} };

$.extend(true, excelButtonConfig, addOptions);
excelButtonConfig.action(e, dt, button, excelButtonConfig);
}).draw();
}
}]
}
],
"fixedHeader": {
header: true,
footer: true
},
"select": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": "./ServerSide.php",
"type": "POST"
},
initComplete: function ()
{
var api = this.api();

// Apply the search
api.columns().every(function ()
{
var that = this;

$('input', this.footer()).on('keyup change', function ()
{
if (that.search() !== this.value)
{
that
.search(this.value)
.draw();
}
});
});
}
});
});
</script>

这允许我拥有可编辑的表格和不可编辑的表格。我可以将 $Edit 添加到任何需要编辑的表中。

关于jquery - DataTables如何用超链接填充列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com