Datatable Custom Pagelength

Datatable Custom Pagelength

Has SanHas San Posts: 5Questions: 1Answers: 0
edited January 2021 in Free community support

I am using one datatable but i have a condition. i have variable named month i want to do if month is not equal to empty then i have to display my all records but if month is empty then the pageLength of datatable remains same as it is 10 and pagination remains same as well

Replies

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    You can use the page.len() API to set the length based on your condition. As described in the docs use -1 to show all the rows on one page.

    Kevin

  • Has SanHas San Posts: 5Questions: 1Answers: 0

    thanks Kevin for you help but i try this when i use my if condition in my ajax it displays the syntax error which is saying JS ',' expected simple words i cant use if condition in my ajax

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    That is not the place for using the API. It would be used after Datatables is initialized. If the month variable is set before Datatables initialization you might be able to use the pageLength option in the init settings. Something like this maybe:

    pageLength: month ? -1 : 10,
    

    If this doesn't help then please provide more details of you you want this process to do.

    Kevin

  • Has SanHas San Posts: 5Questions: 1Answers: 0

    Here is my code where i getting the all records

    function FilteredData(CustomerName) {
    var customer = document.getElementById('CustomerName').value;
    var customerNumber = document.getElementById('CustomerNumber').value;
    var schedule = document.getElementById('Schedule').value;
    var selectNumber = document.getElementById('selectNumber').value;
    var selectSchedule = document.getElementById('selectSchedule').value;
    var completed = document.getElementById('Completed').value;
    var StartDate = document.getElementById('StartDate').value;
    var completedornot = document.getElementById('CompletedorNot').value;
    var month = document.getElementById('Month').value;
    var Active = document.getElementById('Active').value;

            var b1 = document.getElementById("buttonn");
            var b2 = document.getElementById("buttonn1");
            if (customer == "" && schedule == "" && completed == "" && StartDate == "" && selectNumber == "" && selectSchedule == "" && completedornot == "" && month == ""
                && customerNumber == "" && Active == "") {
                b1.style.display = "block";
                b2.style.display = "none";
            }
            if (customer != "" || schedule != "" || completed != "" || StartDate != "" || selectNumber == "" && selectSchedule != "" || completedornot != "" != month != ""
                || customerNumber != "" || Active != "") {
                b2.style.display = "block";
                b1.style.display = "none";
            }
    
            $('#example').DataTable().clear();
            $('#example').DataTable().destroy();
            if (month != "" || month != null || month != undefined) {
                $('#example').DataTable(
                    {
                        dom: '<"datatable-header"fl><"datatable-scroll-wrap"t><"datatable-footer"ip>',
                        bFilter: false,
                        pageLength: month ? -1 : 10 // Here is Your Code
                        "ajax": {
                            "url": '@Url.Action("AllCustomerContracts", "CustomerContract")',
                            "data": {
                                CustomerName: customer,
                                CustomerNumber: customerNumber,
                                Schedule: schedule,
                                selectNumber: selectNumber,
                                selectSchedule: selectSchedule,
                                Completed: completed,
                                StartDate: StartDate,
                                CompletedorNot: completedornot,
                                Month: month,
                                Active: Active,
                            },
                            "type": "POST",
                            "datatype": "json",
                        },
                        "columns": [
                            { "data": "OfficeId", "name": "OfficeId" },
                            { "data": "ScaleType", "name": "ScaleType" },
                            //{ "data": "ScaleName", "name": "ScaleName" },
                            { "data": "ModelNo", "name": "ModelNo" },
                            { "data": "ReferenceNo", "name": "ReferenceNo" },
                            { "data": "Inspection", "name": "Inspection" },
                            { "data": "StartDate", "name": "StartDate" },
                            { "data": "Completed", "name": "Completed" },
                            { "data": "Notes", "name": "Notes" },
                            { "data": "Id", "name": "Id" },
                        ],
                        'columnDefs': [
                            {
    
                                render: function (data, type, full, meta) {
                                },
                                targets: 8
                            }
                        ],
    
                        "serverSide": "true",
                        "order": [0, "asc"],
                        "processing": "true",
                        "language":
                        {
                            "processing": "Processing... Please wait"
                        },
                    })
            }
    

    As You can see the code when function calls we are getting values in variables in the **month **variable i need to handle
    **if(month != "" || month != null || month != undefined{ pageLength Should be -1 or 100 }**

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    edited January 2021

    pageLength: month ? -1 : 10 // Here is Your Code

    First you don't have the , at the end of the line to separate the options. So you are probably getting a syntax error.

    Maybe you need something more like this:

    if (month != "" || month != null || month != undefined) {
        var myPageLength = -1;
    } else {
        var myPageLength = 10;
    }
    $('#example').DataTable(
        {
            dom: '<"datatable-header"fl><"datatable-scroll-wrap"t><"datatable-footer"ip>',
            pageLength: myPageLength,
    ....
    

    Kevin

This discussion has been closed.