目录
1使用jQuery DataTables.net的ASP.NET8
1使用jQuery DataTables.net的ASP.NET8
我正在评估jQuery datatable.net组件[1]在ASP.NET8项目中的使用,并创建了几个原型(概念验证)应用程序,这些应用程序将在这些文章中展示。
1.1 本系列中的文章
本系列中的文章包括:
- 使用DataTables.net的ASP.NET8——第1部分——基础
- 使用DataTables.net的ASP.NET8——第2部分——操作按钮
- 使用DataTables.net的ASP.NET8——第3部分——状态保存
- 使用DataTables.net的ASP.NET8——第4部分——多语言
- 使用DataTables.net的ASP.NET8——第5部分——在AJAX中传递其他参数
- 使用DataTables.net的ASP.NET8——第6部分——在AJAX中返回其他参数
- 使用DataTables.net的ASP.NET8——第7部分——常规按钮
- 使用DataTables.net的ASP.NET8——第8部分——选择行
- 使用DataTables.net的ASP.NET8——第9部分——高级过滤器
2 最终结果
本文的目标是创建一个概念验证应用程序,用于演示组件状态保存 DataTables.net。让我们介绍一下这篇文章的结果。在此屏幕上,您会看到处于特定“state”的示例应用程序,即应用了过滤器“mark”,并按“Family name”降序排序。

用户经常要求,当他们离开页面然后返回时,他们处于与离开时相同的状态。如本文所示,您可以在此应用程序中导航离开并返回,并且DataTables组件将处于与您离开时相同的状态。
本文的主题是展示如何实现它。这很简单,这是组件中包含的一个功能,并且state保存在客户端的浏览器存储中。
3 客户端DataTables.net组件
在这里,我将只展示使用DataTables组件的ASP.NET视图是什么样子的。
<!-- Employees.cshtml -->
<partial name="_LoadingDatatablesJsAndCss" />
@{
<div class="text-center">
<h3 class="display-4">Employees table</h3>
</div>
<!-- Here is our table HTML element defined. JavaScript library Datatables
will do all the magic to turn it into interactive component -->
<table id="EmployeesTable01" class="table table-striped table-bordered ">
</table>
}
<script>
// Datatables script initialization=========================================
// we used defer attribute on jQuery so it might not be available at this point
// so we go for vanilla JS event
document.addEventListener("DOMContentLoaded", InitializeDatatable);
function InitializeDatatable() {
$("#EmployeesTable01").dataTable({
//processing-Feature control the processing indicator.
processing: true,
//paging-Enable or disable table pagination.
paging: true,
//info-Feature control table information display field
info: true,
//ordering-Feature control ordering (sorting) abilities in DataTables.
ordering: true,
//searching-Feature control search (filtering) abilities
searching: true,
//search.return-Enable / disable DataTables' search on return.
search: {
return: true
},
//autoWidth-Feature control DataTables' smart column width handling.
autoWidth: true,
//lengthMenu-Change the options in the page length select list.
lengthMenu: [10, 15, 25, 50, 100],
//pageLength-Change the initial page length (number of rows per page)
pageLength: 10,
//order-Initial order (sort) to apply to the table.
order: [[1, 'asc']],
//serverSide-Feature control DataTables' server-side processing mode.
serverSide: true,
//stateSave-State saving - restore table state on page reload.
stateSave: true,
//stateDuration-Saved state validity duration.
//-1 sessionStorage will be used, while for 0 or greater localStorage will be used.
stateDuration: -1,
//Load data for the table's content from an Ajax source.
ajax: {
"url": "@Url.Action("EmployeesDT", "Home")",
"type": "POST",
"datatype": "json"
},
//Set column specific initialisation properties
columns: [
//name-Set a descriptive name for a column
//data-Set the data source for the column from the rows data object / array
//title-Set the column title
//orderable-Enable or disable ordering on this column
//searchable-Enable or disable search on the data in this column
//type-Set the column type - used for filtering and sorting string processing
//visible-Enable or disable the display of this column.
//width-Column width assignment.
//render-Render (process) the data for use in the table.
//className-Class to assign to each cell in the column.
{ //0
name: 'id',
data: 'id',
title: "Employee Id",
orderable: true,
searchable: false,
type: 'num',
visible: true
},
{
//1
name: 'givenName',
data: "givenName",
title: "Given Name",
orderable: true,
searchable: true,
type: 'string',
visible: true
},
{
//2
name: 'familyName',
data: "familyName",
title: "Family Name",
orderable: true,
searchable: true,
type: 'string',
visible: true
},
{
//3
name: 'town',
data: "town",
title: "Town",
orderable: true,
searchable: true,
type: 'string',
visible: true
},
{
//4
name: 'country',
data: "country",
title: "Country",
orderable: true,
searchable: true,
type: 'string',
visible: true
},
{
//5
name: 'email',
data: "email",
title: "Email",
orderable: true,
searchable: true,
type: 'string',
visible: true,
width: "150px",
className: 'text-center '
},
{
//6
name: 'phoneNo',
data: "phoneNo",
title: "Phone Number",
orderable: false,
searchable: true,
type: 'string',
visible: true
},
{
//7
name: 'actions',
data: "actions",
title: "Actions",
orderable: false,
searchable: false,
type: 'string',
visible: true,
render: renderActions
},
{
//8
name: 'urlForEdit',
data: "urlForEdit",
title: "urlForEdit",
orderable: false,
searchable: false,
type: 'string',
visible: false
}
]
});
function renderActions(data, type, row, meta) {
//for Edit button we get Url from the table data
let html1 =
'<a class="btn btn-info" href="' +
row.urlForEdit + '">Edit</a>';
//for Info button we create Url in JavaScript
let infoUrl = "@Url.Action("EmployeeInfo", "Home")" +
"?EmployeeId=" + row.id;
let html2 =
'<a class="btn btn-info" style="margin-left: 10px" href="' +
infoUrl + '">Info</a>';
return html1 + html2;
}
}
</script>
注意DataTables组件配置中的“stateSave”和“stateDuration”属性。他们只需要激活即可使用该功能。
- 属性stateSave——状态保存——在页面重新加载时恢复表状态。
- 属性stateDuration——保存的状态有效期。如果设置为-1,则将使用sessionStorage,而对于0或更大,将使用localStorage。
有关这些属性的更多信息,请参阅手册[1]。这里的应用程序只是ASP.NET环境的概念证明。
4 保存状态如何运作
从内部观察事物的运作方式总是很有趣。使用Chrome DevTools,我们可以窥探浏览器的会话存储。

您可以看到,DataTables.net组件已为自己为该特定URL创建了一个键,因为可能有多个页面包含该组件DataTables.net并且每个页面都需要保留自己的状态。你可以看到状态保存在一个大的JSON中,并且有我们的搜索短语“mark”和“order by column 2, descending”设置。
5 总结
可以下载完整的示例代码项目。
6 参考资料
[1] DataTables | Javascript table library
https://www.codeproject.com/Articles/5385308/ASP-NET8-using-DataTables-net-Part3-State-saving
244




被折叠的 条评论
为什么被折叠?



