gpt4 book ai didi

javascript - 使用用于过滤 EF 的 WEB API OData 服务自定义 DataTables - 列搜索不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:53 25 4
gpt4 key购买 nike

我在客户端使用 DataTables,在服务器端使用 ASP.NET WEB API OData 可查询服务。在服务器端对 DataTable 列进行排序和过滤的问题在于,DataTable 正在生成包含所有列信息的可怕的超长请求,即使它们不用于排序或过滤也是如此。我决定编写从客户端到服务器的自定义 AJAX 调用,以创建简洁的 odata 查询,它可以轻松应用于 EF 上下文。不幸的是,列搜索字段已停止呈现为输入。可能是什么问题?

JavaScript 和 HTML 代码:

$(document).ready(function() {
var options = new Object();
options.searching = true;
options.searchable = true;
options.aoColumns = [{
"sName": "USER_NAME",
"searchable": true
}];
options.bProcessing = true;
options.bServerSide = true;
options.sAjaxSource = "http://localhost/api/list";
options.fnServerData = function(sSource, aoData, fnCallback) {
var filter = "$top=5"; //just as example
$.getJSON(sSource, filter, function(json) {
fnCallback(json);
});
}
$('#myTable').dataTable(options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<table id="myTable" class="table">
<thead>
<tr>
<th>
First Name
</th>
...

</tr>
</thead>
<tfoot>
<tr>
<th>
First Name
</th>
...
</tr>
</tfoot>

</table>

该服务工作正常,看起来像这样(我已使此代码尽可能易于理解)。C#代码:

public HttpResponseMessage List(ODataQueryOptions<User> options)
{
var result = oDataQueryOptions.ApplyTo(_context.Users) as IEnumerable<User>;
JQueryJsonResponse jsonUserResult = new JQueryJsonResponse
{
Draw = xxx,
iTotalRecords = xxx,
iTotalDisplayRecords = xxx,
aaData = result.ToList()
};
return Request.CreateResponse(HttpStatusCode.OK, jsonUserResult);
}

我希望是这样的: expected rendering但我明白了: enter image description here

最佳答案

CAUSE

您使用 options.bServerSide = true; 启用了服务器端处理模式。服务器端处理方式下,过滤、分页、排序计算均由服务器完成。

SOLUTION

需要在服务端处理客户端发送的参数,进行过滤、分页和排序。参见 full list of parameters以服务器端处理方式发送。

另一种解决方案是使用 options.bServerSide = false; 禁用服务器端处理模式,让 DataTables 在客户端执行过滤、分页和排序。

关于javascript - 使用用于过滤 EF 的 WEB API OData 服务自定义 DataTables - 列搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com