javascript 如何在服务器端处理数据表上使用搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27920289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use search on datatables with server-side processing?
提问by radbyx
When working with datatables server-side
proccessing. How does the search value being passed to the server? I've looked in the doc.
处理数据表时server-side
。搜索值如何传递给服务器?我看过文档。
The datatable sends automatically the draw
, start
and the length
to the server. Can and should I do something simular with the search
? The documentation mention search[value]
but I don't know how to interpretive it.
数据表自动发送draw
,start
和length
到服务器。我可以而且应该用 做一些类似的事情search
吗?文档提到search[value]
但我不知道如何解释它。
CLIENT
客户
$(document).ready(function () {
var url = '@Url.Action("GetJsonData", "Home")';
$('#example').dataTable({
'searching': true,
"paging": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": url,
"type": "GET"
},
"columns": [
{ "data": "id" },
{ "data": "name" }
]
});
});
SERVER
服务器
public JsonResult GetJsonData(string draw, int start, int length, string search)
{
var hugeDataArr = new object[100];
var returnDataArr = new object[length];
for (int i = 0; i < hugeDataArr.Length; i++)
{
hugeDataArr[i] = new
{
DT_RowId = i,
id = "id" + i.ToString().PadLeft(2, '0'),
name = "name???" + i.ToString().PadLeft(2, '0')
};
}
for (int i = 0; i < length; i++)
{
returnDataArr[i] = hugeDataArr[start + i];
}
JsonResult json = Json(new
{
draw = Convert.ToInt32(draw),
recordsTotal = 100, // calculated field
recordsFiltered = 50, // calculated field
data = returnDataArr
}, JsonRequestBehavior.AllowGet);
return json;
}
采纳答案by radbyx
I use this, because it's never null but an empty string.
我使用它,因为它永远不会为空,而是一个空字符串。
Request.Form.GetValues("search[value]")[0]
回答by ravichandra vydhya
You are not supposed to use search as parameter.But it automatically is part of your query string.
您不应该使用搜索作为参数。但它会自动成为您的查询字符串的一部分。
public JsonResult GetJsonData(string draw, int start, int length)
{
string search = Request.QueryString["search[value]"];
// your code for search filtering
}
thanks ravi
谢谢拉维
回答by Gobind Gupta
if you want to get value of serach box at server-side:
如果你想在服务器端获取 serach box 的值:
string search = Request.Form.GetValues("search[value]").FirstOrDefault();
string search = Request.Form.GetValues("search[value]").FirstOrDefault();
This will give you the value of search box.
这将为您提供搜索框的价值。
回答by Hamza Khanzada
If the search[value]
parameter is always null then make sure you are using POST
Ajax call on the client side and your action method of the controller is marked as [HttpPost]
.
如果search[value]
参数始终为 null,则确保您POST
在客户端使用Ajax 调用,并且控制器的操作方法标记为[HttpPost]
。
Clientside:
客户端:
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": url,
"type": "POST"
},
//Other configurations
});
Server:
服务器:
[HttpPost]
public JsonResult GetJsonData(string draw, int start, int length)
{
string search = Request.QueryString["search[value]"];
// your code for search filtering
}
回答by dingensundso
As written in the docs datatables passes a search array with two values. search[value] is the search string which you'll need for filtering and search[regex] is just a boolean, expressing whether the search[value] should be interpreted as regex or just string.
正如 docs 数据表中所写的那样,传递一个具有两个值的搜索数组。search[value] 是过滤所需的搜索字符串,search[regex] 只是一个布尔值,表示 search[value] 应该被解释为正则表达式还是字符串。
If you're searching for values in a specific column the search string will be in columns[i][search][value]
如果您在特定列中搜索值,则搜索字符串将位于 columns[i][search][value]