0

I am using Jtable Jquery plugin for CRUD operation in my asp.net mvc web application. This works properly but in some functionality I want current sorting criteria for grid. So I read documentation for it but didn't find any method or event by which I get current sorting criteria for grid in my jquery code. When page loaded or sort column link is clicked sort criteria is sent to controller by ajax call for processing and getting data to bind to grid. I am able to get it on serverside but I want it to use it in clientside in jquery but don't know how to get it. Any solution for this is really helpful..

Thank you....

Code Explorer
  • 65
  • 2
  • 7

2 Answers2

1

Javascript

sorting: true, //Enable sorting
defaultSorting: 'Name ASC', //Sort by Name by default

Controller

[HttpPost]
public JsonResult PersonList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
    try
    {
        int personCount = _personRepository.GetPersonCount();
        List<person> persons = _personRepository.GetPersons(jtStartIndex, jtPageSize, jtSorting);
        return Json(new { Result = "OK", Records = persons, TotalRecordCount = personCount });
    }
    catch (Exception ex)
    {
        return Json(new { Result = "ERROR", Message = ex.Message });
    }
} 

GetPersons Method with Sorting Criterias

public IEnumerable GetDeployments(string show, string name, int startIndex, int count, string sorting)
        {

//Get List of Persons from Database

    if (!string.IsNullOrEmpty(name))
               {
                   deploymentsList = deploymentsList.Where(p => p.KomponentenName.ToString().StartsWith(name));
               }

               if (string.IsNullOrEmpty(sorting) || sorting.Equals("KomponentenName ASC"))
               {
                   deploymentsList = deploymentsList.OrderBy(p => p.KomponentenName);
               }
               else if (sorting.Equals("KomponentenName DESC"))
               {
                   deploymentsList = deploymentsList.OrderByDescending(p => p.KomponentenName);
               }
               else if (sorting.Equals("datum ASC"))
               {
                   deploymentsList = deploymentsList.OrderBy(p => p.datum);
               }
               else if (sorting.Equals("datum DESC"))
               {
                   deploymentsList = deploymentsList.OrderByDescending(p => p.datum);
               }
               else if (sorting.Equals("RequesterName ASC"))
               {
                   deploymentsList = deploymentsList.OrderBy(p => p.RequesterName);
               }
               else if (sorting.Equals("RequesterName DESC"))
               {
                   deploymentsList = deploymentsList.OrderByDescending(p => p.RequesterName);
               }
               else if (sorting.Equals("dateDeployed ASC"))
               {
                   deploymentsList = deploymentsList.OrderBy(p => p.dateDeployed);
               }
               else if (sorting.Equals("dateDeployed DESC"))
               {
                   deploymentsList = deploymentsList.OrderByDescending(p => p.dateDeployed);
               }
               else
               {
                   deploymentsList = deploymentsList.OrderBy(p => p.KomponentenName); //Default!
               }

                 return count > 0
                           ? deploymentsList.Skip(startIndex).Take(count).ToList() //Paging
                           : deploymentsList.ToList(); //No paging

            }
mhx8
  • 26
  • 2
0

Since you want to get the sorting criteria value within client side javascript code, you can achieve this by extending the jQuery jTable through standard jQuery extension process. Then once you are inside your extension code, you can write:

this._lastSorting

The above will give you:

ObjectfieldName: "size"
sortOrder: "ASC"

You can take a look at this SO question on how to extend a jQuery plugin if you need information on how to extend jTable.

Hope this helps.

Community
  • 1
  • 1
AUR
  • 623
  • 7
  • 14