简体   繁体   English

如何在数据表mvc c#中显示pageLength过滤器和打印选项

[英]how to show pageLength filter and print option both in datatable mvc c#

I want print option as well as pageLength filter in datatable working in mvc c# .我想要在 mvc c# 中工作的数据表中的打印选项和 pageLength 过滤器 I search in internet but related to this topic I didnt found anything.我在互联网上搜索但与此主题相关的我没有找到任何内容。 Infact in stackoverflow too.实际上在 stackoverflow 中也是如此。 So, I am surprise that it is possible or not.所以,我很惊讶这可能与否。 If yes then how and if no then why ?如果是,那么如何,如果不是,那么为什么

I code for print and for the pageLength filter but it just showing the print option not pageLength filter option and if I remove the print option then it will show the pageLength filter option.我为打印和 pageLength 过滤器编码,但它只显示打印选项而不是 pageLength 过滤器选项,如果我删除打印选项,那么它将显示 pageLength 过滤器选项。 Confuse why this happening?混淆为什么会发生这种情况?

Here is my view code for print and for the pageLength filter这是我用于打印和 pageLength 过滤器的查看代码

$(document).ready(function () {
    // DataTable
    var table = $("#dataGrid").DataTable({
        //Report Export Start
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'print',
                exportOptions: {
                    columns: [0, 1, 2]
                },
                className: 'btn btn-default btn-xs glyphicon glyphicon-print',
                footer: true
            }
        ],
        //Report Export End

        "scrollX": false,  // scrolling horizontal
        "bSort": false,
        "bFilter": false,
        "processing": true, // for show progress bar
        "serverSide": true, // for process server side
        "filter": true, // this is for disable filter (search box)
        //"orderMulti": false, // for disable multiple column at once
        "pageLength": 5,
        "lengthMenu": [5, 10, 50, 100, 1000, 10000],

        "ajax": {
            "url": "/Ledger/LedgerList",
            "type": "POST",
            "datatype": "json",
            "data": function (d) {
                return $.extend({}, d, {
                    'SearchLedgerType': document.getElementById("search_ledgerType").value                        
                });
            }
        },

        "columnDefs":
            [{
                "targets": [2, 3],
                "visible": false,
                "searchable": true,
                "orderable": false
            }
            ],
        "columns": [
            { "data": "id", "name": "Id", "autoWidth": true },
            { "data": "name", "name": "Name", "autoWidth": true },
            { "data": "address", "name": "Address", "autoWidth": true },                    
        ]
    });
});

And here is my controller,这是我的控制器,

[HttpPost]
public IActionResult LedgerList(Ledger ledger)
{
    var draw = Request.Form["draw"];
    var start = Request.Form["start"].ToString();
    var length = Request.Form["length"].ToString();
    var sortColumn = Request.Form["columns[" + Request.Form["order[0][column]"] + "][name]"];
    var sortColumnDir = Request.Form["order[0][dir]"];
    var searchValue = Request.Form["search[value]"];

    //Paging Size (10,20,50,100)    
    int pageSize = length != null ? Convert.ToInt32(length) : 0;
    int skip = start != null ? Convert.ToInt32(start) : 0;
    int recordsTotal = 0;

    // Getting all Customer data    
    var dataList = (from l in _Db.Ledger
                    select new Ledger
                    {
                        Id=l.Id,
                        Name = l.Name,
                        Address = l.Address,
                    }).ToList();

    // filter data
    if (ledger.SearchLedgerType != null)
        dataList = dataList.Where(d => d.LedgerType.Trim() == ((LedgerType)Convert.ToInt32(ledger.SearchLedgerType)).ToString()).ToList();

    //Sorting    
    if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
    {
        var propertyInfo = typeof(Ledger).GetProperty(sortColumn);
        if (propertyInfo != null)
        {
            if (sortColumnDir == "asc")
                dataList = dataList.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
            else
                dataList = dataList.OrderByDescending(x => propertyInfo.GetValue(x, null)).ToList();
        }
    }

    //Search    
    if (!string.IsNullOrEmpty(searchValue))
        dataList = dataList.Where(m => m.Name.ToLower().Contains(searchValue.ToString().ToLower())).ToList();

    //total number of rows count     
    recordsTotal = dataList.Count();

    //Paging     
    var data = dataList.Skip(skip).Take(pageSize).ToList();

    //Returning Json Data
    return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}

Now, this is the code for both print as well as pageLength filter but in output just I get the print option not pageLength option for more clear let see the image of that output现在,这是打印和 pageLength 过滤器的代码,但在输出中只是我得到了打印选项而不是 pageLength 选项以获得更清晰的让我们看看该输出的图像

在此处输入图片说明

and if I remove the print code from the above code ie如果我从上面的代码中删除打印代码,即

dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'print',
                    exportOptions: {
                        columns: [0, 1, 2]
                    },
                    className: 'btn btn-default btn-xs glyphicon glyphicon-print',
                    footer: true
                }
            ],

then I get the below image result然后我得到下面的图像结果

在此处输入图片说明

Please help me to solve this issue.请帮我解决这个问题。

Thank You.谢谢你。

dom: 'Bfrtip'替换为dom: 'Blfrtip'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM