我已经在我的代码中设置了数据表,并且我使用以下代码从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;
}
你能帮帮我吗?
此致 阿布舍克巴克
答案 0 :(得分:1)
首先,为了使DataTable服务器端代码可重用并减少代码量,理想情况下,您可以使用Model接收参数,这样可以更轻松地导航接收到的数据。
因此,一个包含以下结构的模型:
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
对于控制器,为了使搜索起作用,我不得不使用POST请求,与GET请求一样,搜索[value]保持为空。
因此,作为控制器,您可以使用以下内容:
[HttpPost]
public ActionResult GetMyList (DataTableRequestModel model)
使用model.search.value或regex或列从模型中获取通用搜索的值。
此外,您可以查看此post,这非常有帮助。
希望这会有所帮助,理想情况下,您可以使用调试器来检查您收到的实际数据。