【发布时间】:2019-10-07 23:08:49
【问题描述】:
我从我的数据库中的查询加载了一个数据表。 (+/- 10000 条记录) 这个想法是用户应该能够选择多个记录以供以后处理
首先我想为选择添加一个带有复选框的列,然后当用户完成所有选择后,应用程序会跟踪所有选定的行,然后在页面上的某个位置使用“下一步按钮”进入下一步,但是经过 12 小时的尝试,我无法做到。
然后我想通过在每一行中添加一个按钮来使其更简单,这样每次用户单击此按钮时,应用程序都会将选定的 id 保存在会话变量中。
<div class="panel-body">
<table id="userTable" class="table display compact order-column">
<thead>
<tr>
<th>Select</th>
<th>Name</th>
<th>City</th>
<th>Phone</th>
<th>Zipcode</th>
</tr>
</thead>
</table>
@section Scripts {
@Scripts.Render("~/bundles/datatable")
<script type="text/javascript">
$(document).ready(function () {
var ids;
var mytable = $('#userTable').DataTable({
"sDom": 'ltipr',
"bServerSide": true,
"ajax": {
"beforeSend": AjaxBegin,
"type": "POST",
"url": '/LocationModifier/UserHistory',
"contentType": 'application/json; charset=utf-8',
'data': function (data) { return data = JSON.stringify(data); },
'complete': AjaxComplete
},
"bProcessing": false,
"orderMulti": false,
"scrollX": true,
"deferRender": true,
"searchDelay": 7000,
"fixedHeader": {
"header": true,
"footer": true
},
"columnDefs": [
{ "defaultContent": "-", "targets": "_all" },
{ "className": "text-center custom-middle-align", "targets": [0, 1, 2, 3, 4, ] },
],
"colReorder": true,
"lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
"columns": [
{
"title": "Select",
"data": "ID",
"searchable": false,
"sortable": false,
"render": function (data, type, full, meta) {
return '<a href="@Url.Action("AddToCache", "LocationModifier")?id=' + data + '&source=0" class="editUser"><span class="glyphicon glyphicon-pencil btn-sm btn-info"></span></a>';
}
},
{ "data": "Name", "orderable": false },
{ "data": "City", "orderable": true },
{ "data": "Phone", "orderable": true },
{ "data": "Zipcode", "orderable": false },
],
"order": []
});
});
</script>
}
public ActionResult AddToCache(int id)
{
GetRecordAndAddeToCache(id);
// what should i return here, the page should not be refreshed????
}
【问题讨论】:
-
复选框方法还不错。当您生成行时,它们具有 id,当单击复选框时,将其添加到 JavaScript 的 id 数组中。进行接受 List
id 的操作,并在提交时获取您的 id... -
@st_stefanov 那是我之前的目标,但我无法做到。尝试了几个小时后,我的同事告诉我,我必须使用不是免费的 Datatable Edit,所以我放弃了这个想法。但是,如果您在某处有示例,我将不胜感激,我对前端脚本的经验很少
-
我正在为你做一个例子。我还将展示一种 MVC 方法。给我一些时间。
-
我认为这个非常接近您的需要:gunaatita.com/Blog/… 或这个:tutorialspanel.com/…
-
@st_stefanov 感谢您的帮助。这些示例的问题是我将失去 jquery 数据表的好处,所以我将不得不实现我的 slef 分页、过滤排序......等等
标签: javascript c# jquery asp.net-mvc datatables