gpt4 book ai didi

jquery - 数据表搜索在服务器端不起作用

转载 作者:行者123 更新时间:2023-12-01 04:00:24 25 4
gpt4 key购买 nike

我已经在代码中设置了数据表,并且使用以下 jquery 代码从服务器获取值。

我使用的代码如下:-

var dt = $(".ajaxTable").DataTable({
ajax: getData,
// lengthChange: false,
serverSide: true,
processing: true,
searching: true,
"responsive": true,
"filter":true,
"sDom": '"<"right_fter"<"serch_xop"fp>>t<"bottom_tb_Pl"il><"clear">',
"bPagingType": "first_last_numbers",
// "bPaginate": false,
order: [],
columnDefs: [
{ orderable: false }
],
language: {
paginate: {

},
"search": '',
"searchPlaceholder":'Search property…'
},
pageLength: pageSize,
"columns": [
{
render: propertyFavorite, className: "column-center" },
{ "data": "RoofLinkText", className:"key", render: editLink },
{ "data": "Address" },
{ "data": "City" },
{ "data": "State" },
{ "data": "Code" },
{ "data": "Market" },
{ "data": "CompanyName" },
{ render:actionLink, orderable:false, className:"actions" }
],
order: [[1, "asc"]],

});
$(".dataTables_wrapper .right_fter").prepend('<div class="title_bar">Properties</div>');

dt.columns().iterator('column', function (ctx, idx) {
$(dt.column(idx).header()).append('<span class="sort-icon" />');
});

但是当我尝试搜索该选项时,数据表无法检测到搜索列中的更改。

编辑

var getData = rm.configData({
tableSel: '.ajaxTable',
url: listUrl,
formatter: function (o) {
//console.log(o.TotalRecords);
return {
"recordsTotal": o.TotalRecords,
"recordsFiltered": o.TotalRecords,
"data": o.Properties

}
}
})

服务器端代码

public ActionResult List(
string sort = "RoofName",
bool sortAsc = true,
string search = null,
int page = 1,
int pageSize = 10,
string company = null,
string region = null,
string market = null,
string message = null)
{
Domain.Models.User user = Account.AccountManager.GetCurrentUser();

if (String.IsNullOrEmpty(region) && !String.Equals("all", user.Region, StringComparison.OrdinalIgnoreCase))
region = user.Region;

if (String.IsNullOrEmpty(market) && !String.Equals("all", user.Market, StringComparison.OrdinalIgnoreCase))
market = user.Market;

var model = new PropertySearchViewModel();
model.Sort = sort;
model.SortAsc = sortAsc;
model.Search = search;
model.Page = page;
model.PageSize = pageSize;
model.FilterCompanies = company;
model.FilterRegions = region;
model.FilterMarkets = market;

int[] companyIDs = ParamHelper.ToIntArray(model.FilterCompanies);
string[] regions = ParamHelper.ToStringArray(model.FilterRegions);
string[] markets = ParamHelper.ToStringArray(model.FilterMarkets);

if (model.Page == 0) model.Page = 1;

int totalRecords;
IEnumerable<RM.Domain.Models.Search.PropertySearch> properties = _roofData.Search(
page,
model.PageSize,
out totalRecords,
model.Sort,
model.SortAsc,
model.Search,
companyIDs,
regions,
markets,
user.UserID,
user.AssetManager,
user.PropertyManager,
user.RoofGroup,
null);


model.Properties = SearchPropertyModel.FromProperty(properties);
model.TotalRecords = totalRecords;

if (model.Page > model.TotalPages)
model.Page = 1;

JsonResult result = Json(model, JsonRequestBehavior.AllowGet);
return result;
}

你能帮我吗?

问候阿布舍克

最佳答案

因此,首先,为了使 DataTable 服务器端代码可重用并减少代码量,理想情况下,您应该对接收到的参数使用模型,这样您就可以更轻松地导航接收到的数据。

因此,模型包含以下结构:

public class DataTableRequestModel
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public Search search { get; set; }
public List<Order> order { get; set; }
}

public class Column
{
public string data { get; set; }
public string name { get; set; }
public bool searchable { get; set; }
public bool orderable { get; set; }
public Search search { get; set; }
}

public class Search
{
public string value { get; set; }
public string regex { get; set; }
}

public class Order
{
public int column { get; set; }
public string dir { get; set; }
}

此处的完整参数列表:https://datatables.net/manual/server-side

对于 Controller ,为了使搜索正常工作,我必须使用 POST 请求,与 GET 请求一样,搜索[value] 保持为空。

因此,作为 Controller ,您可以使用以下内容:

[HttpPost]
public ActionResult GetMyList (DataTableRequestModel model)

并使用 model.search.value 或正则表达式或按列从模型中获取值以进行通用搜索。

此外,您可以查看此post这非常有帮助。

希望这对您有所帮助,理想情况下您可以使用调试器来检查您正在接收的实际数据以及您正在获取的准确数据。

关于jquery - 数据表搜索在服务器端不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46379107/

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