如何在jtable jquery插件中获取当前排序条件

时间:2015-01-15 10:32:08

标签: javascript jquery jquery-jtable

我在我的asp.net mvc web应用程序中使用Jtable Jquery plugin进行CRUD操作。这工作正常但在某些功能中我想要网格的当前排序标准。所以我阅读了它的文档,但没有找到任何方法或事件,通过它我的jquery代码获得网格的当前排序标准。单击页面加载或排序列链接时,将通过ajax调用将排序条件发送到控制器以进行处理并获取绑定到网格的数据。我能够在服务器端获得它,但我希望它在jquery的客户端使用它,但不知道如何获得它。任何解决方案都非常有用..

谢谢....

2 个答案:

答案 0 :(得分:1)

的Javascript

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

控制器

[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方法

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

            }

答案 1 :(得分:0)

由于您希望在客户端javascript代码中获取排序条件值,因此可以通过标准jQuery扩展过程扩展jQuery jTable来实现此目的。然后,一旦进入扩展代码,就可以写下:

this._lastSorting

以上将给你:

ObjectfieldName: "size"
sortOrder: "ASC"

如果您需要有关如何扩展jTable的信息,可以查看this关于如何扩展jQuery插件的问题。

希望这有帮助。