2017-04-04 70 views
0

最终,我想将我的表的列排序发送到服务器,以便我可以从数据库检索排序的分页记录集。这行代码让我走这条道路:如何通过使用Bootstrap Datatables的索引获取列名?

$("#results").DataTable().order(); 

但是,它返回一个这样的数组:

[[0,"asc"]] 

我不能依靠列索引从客户方阵列为了我的数据集在服务器端。我需要的是一个这样的数组:

[["Name","asc"],["HireDate","desc"]] 

如果我写这样一个API,我会做的列名,像这样访问:

$("#results").DataTable().columns()[0].name; 
$("#results").DataTable().columns(0).name; 

似乎没有了不过,DataTable API非常简单。

回答

0

在您的数据表中,如果您想要在API中检索名称,则需要将名称参数写入该字段。检查我的娄代码,

.DataTable({ 
//rest code, 
"order": [ 
     [0, "asc"] 
    ], 
//rest code, 
"columns": [ 
      { "data": "HireDate", name: "Date", }, 
//rest columns 

在这里,我的名字日期设置到外地雇佣日期

而在我的API方法,

string _ordByColumn = 
     GetQueryValueByName.Get(Request.GetQueryNameValuePairs(), 
           "order[0].column"); 
string _ordColumnName = 
     GetQueryValueByName.Get(Request.GetQueryNameValuePairs(), "columns[" 
           + _ordByColumn + "].name"); 
string _ordDirection = 
     GetQueryValueByName.Get(Request.GetQueryNameValuePairs(), 
           "order[0].dir"); 
if (!string.IsNullOrEmpty(_ordByColumn)) 
     { 
      switch (_ordColumnName) 
      { 
       case "Date": 
        { 
         if (string.Compare(_ordDirection, "asc") == 0) 
         { 
          //retrieve data order by Date 
         } 
         else 
         { 
          //retrieve data order by descending Date 
         } 
         break; 
        } 
       //rest cases and then default 
       default: 
        { 
         //retrieve data order by what ever you want 
         break; 
        } 
       } 
      } 

而且我GetQueryValueByName类波纹管,

public static class GetQueryValueByName 
{ 
    public static string Get(IEnumerable<KeyValuePair<string, string>> _req, 
          string key) 
    { 
     return _req.FirstOrDefault(ma => string.Compare(ma.Key, key) == 
            0).Value; 
    } 
} 

可以读取查询字符串作为你的方式。

相关问题