使用DataTables.net的ASP.NET8——第7部分——常规按钮

目录

1使用jQuery DataTables.net的ASP.ET8

1.1 本系列中的文章

2 最终结果

3 客户端DataTables.net组件

4 总结

5 参考资料


1使用jQuery DataTables.netASP.ET8

我正在评估jQuery datatable.net组件[1]ASP.NET8项目中的使用,并创建了几个原型(概念验证)应用程序,这些应用程序将在这些文章中展示。

1.1 本系列中的文章

本系列中的文章包括: 

2 最终结果

本文的目标是创建一个概念验证应用程序,该应用程序使用常规的预定义按钮演示DataTables.net组件。让我们介绍一下这篇文章的结果。

关键是,组件DataTables.net允许您通过JavaScript配置激活一些预定义的按钮,而无需编写任何代码。

典型功能是将表格数据导出到复制到剪贴板、CSV文件、ExcelPDF和打印

然后,您可以选择在屏幕不够大时应该显示哪些表格列

然后,我们将展示如何控制仅将可见的表格列导出到 ExcelPDFPrint

然后,我们将展示如何创建按钮来重置搜索字段

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 initialization 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
                },
                {
                    //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,
                    className: 'FixedVisibility'
                }
            ],

            layout: {
                top1Start: {
                    buttons:
                        ['copy', 'csv', 'excel', 'pdf', 'print',
                            {
                                extend: 'colvis',
                                text: 'Column Visibility',
                                columns: ':not(.FixedVisibility)',
                            },
                            {
                                //this version will export just columns that are visible
                                extend: 'pdfHtml5',
                                text: 'PDF2',
                                orientation: 'landscape',
                                pageSize: 'A4',
                                exportOptions: {
                                    columns: ':visible'
                                }
                            },
                            {
                                //this version will export just columns that are visible
                                extend: 'print',
                                text: 'Print2',
                                exportOptions: {
                                    columns: ':visible'
                                }
                            },
                            {
                                //this version will export just columns that are visible
                                extend: 'excel',
                                text: 'Excel2',
                                exportOptions: {
                                    columns: ':visible'
                                }
                            },
                            {
                                text: 'Reset Search',
                                action: ResetSearch
                            }
                        ]
                }
            }
        });

        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;
        }

        function ResetSearch(e, dt, node, config) {
            dt.search('').draw();
        };
    }
</script>

请注意我们用于定义此示例中所有按钮的layout属性。

有关这些属性的更多信息,请参阅手册[1]。这里的应用程序只是ASP.NET环境的概念证明。

4 总结

可以下载完整的示例代码项目。

5 参考资料

[1] https://datatables.net/

https://www.codeproject.com/Articles/5385828/ASP-NET8-using-DataTables-net-Part7-Buttons-regula

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值