Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DtParameters dtParameters error #9

Open
karthikhane opened this issue Oct 5, 2021 · 0 comments
Open

DtParameters dtParameters error #9

karthikhane opened this issue Oct 5, 2021 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@karthikhane
Copy link

DtParameters dtParameters can't initilize and getting ajaxy error in data loading.
Draw = 0,
search = null
column = null
length = null

[HttpPost]
public async Task LoadTable(DtParameters dtParameters, int districtid, int schoolid)
{
schoolid = 1;
var searchBy = dtParameters.Search?.Value;

        // if we have an empty search then just order the results by Id ascending
        var orderCriteria = "Id";
        var orderAscendingDirection = true;

        if (dtParameters.Order != null)
        {
            // in this example we just default sort on the 1st column
            orderCriteria = dtParameters.Columns[dtParameters.Order[0].Column].Data;
            orderAscendingDirection = dtParameters.Order[0].Dir.ToString().ToLower() == "asc";
        }
            //Direct table list into Grid
            using (var Context = new SchooldexAOIContext())
            {
                 var GridData = _context.UserSetupInfos.AsQueryable();

                if (!string.IsNullOrEmpty(searchBy))
                {
                    GridData = GridData.Where(r => r.FirstName != null && r.FirstName.ToUpper().Contains(searchBy.ToUpper()) ||
                                       r.LastName != null && r.LastName.ToUpper().Contains(searchBy.ToUpper()) ||
                                       r.Email != null && r.Email.ToUpper().Contains(searchBy.ToUpper()));                                          
                }
                GridData = orderAscendingDirection ? GridData.OrderByDynamic(orderCriteria, DtOrderDir.Asc) : GridData.OrderByDynamic(orderCriteria, DtOrderDir.Desc);

                // now just get the count of items (without the skip and take) - eg how many could be returned with filtering
                var filteredResultsCount = await GridData.CountAsync();
                var totalResultsCount =  await _context.UserSetupInfos.CountAsync();
                return Json(new DtResult<SchooldexAPI_DB.Contexts.UserSetupInfo>
                {
                    Draw = dtParameters.Draw,
                    RecordsTotal = totalResultsCount,
                    RecordsFiltered = filteredResultsCount,
                    // Data = GridData.ToList()
                    Data = await GridData
                .Skip(dtParameters.Start)
                .Take(dtParameters.Length)
                .ToListAsync()


                }); 


            }

           
        //}
        //return null;
    }

Js files

var table;

$(document).ready(function () {
// $.fn.dataTable.moment("DD/MM/YYYY HH:mm:ss");
// $.fn.dataTable.moment("DD/MM/YYYY");

table = $("#userlistgrid").DataTable({
    // Design Assets
    stateSave: true,
    autoWidth: true,
    // ServerSide Setups
    processing: true,
    serverSide: true,
    // Paging Setups
    paging: true,
    // Searching Setups
    searching: { regex: true },
    // Ajax Filter
    ajax: {
        url: "/Admin/LoadTable",
        type: "POST",           
        contentType: "application/json",
        dataType: "json",
        data: function (d) {
            d.districtid = 0;   //param 1
            d.schoolid = 1;    //param 2
            return JSON.stringify(d);
        }
    },
    // Columns Setups
    columns: [
        { data: "Id" },
        { data: "FirstName" },
        { data: "LastName" } ,
        { data: "Email" },
       // { data: "UserName" }
    ],
    // Column Definitions
    columnDefs: [
        { targets: "no-sort", orderable: false },
        { targets: "no-search", searchable: false },
        {
            targets: "trim",
            render: function (data, type, full, meta) {
                if (type === "display") {
                    data = strtrunc(data, 10);
                }

                return data;
            }
        },
        { targets: "date-type", type: "date-eu" },
        {
            targets: 10,
            data: null,
            defaultContent: "<a class='btn btn-link' role='button' href='#' onclick='edit(this)'>Edit</a>",
            orderable: false
        },
    ]
});

});

function strtrunc(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
}
else {
return str;
}
}

function edit(rowContext) {
if (table) {
var data = table.row($(rowContext).parents("tr")).data();
alert("Example showing row edit with id: " + data["id"] + ", name: " + data["name"]);
}
}

@DavidSuescunPelegay DavidSuescunPelegay added the bug Something isn't working label Oct 25, 2021
@DavidSuescunPelegay DavidSuescunPelegay self-assigned this Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants