【发布时间】:2019-09-20 19:12:05
【问题描述】:
努力实现
使用 Datatable,我正在尝试实现列搜索。我希望将search[value] 和column index 传递给Controller,我有一个filter method,它将过滤和加载列表。
我的尝试
使用以下代码,我将keyup change 绑定到每一列。在keyup change 期间,我设法从表中得到valueData 和column index
table.columns().eq(0).each(function (colIdx) {
$('input', $('.filters td')[colIdx]).bind('keyup change', function () {
var searchColIndex = $(this).parent().index();
//=== During keyup or change, it will go through each column
var columnIndex = colIdx;
//=== When the searchCol matches the columnIndex
if (searchColIndex == columnIndex) {
//=== Ideally will call Draw and pass the value to Controller
var valuedata = $(this).val();
//table.column(0).search($(this).val()).draw();
table
.column(colIdx)
.search(valuedata)
.draw();
}
});
});
Controllers 无法获取search value 或column index
但如果我在右上角搜索框搜索,Controllers 可以请求值
代码 https://github.com/BROMVC5/BROSTANDARD.git
穆拉特建议
var table = $('#tablePassword').DataTable({
"paging": true,
"ordering": true,
"processing": true, // control the processing indicator.
"serverSide": true, // recommended to use serverSide when data is more than 10000 rows for performance reasons
"info": true, // control table information display field
"lengthChange": true,
"lengthMenu": [[5, 20, 50, -1], [5, 20, 50, "All"]], // use the first inner array as the page length values and the second inner array as the displayed options
"colReorder": true,
"orderMulti": true, // for disable multiple column at once
"language": {
searchPlaceholder: "Search records"
},
"order": [["3", "desc"]], //=== Not working during stateSave
//"ajax":{
// "url": "/Password/AjaxGetJsonData",
// "type": "POST"
//},
"AjaxSource": "/Password/AjaxGetJsonData",
"fnServerData": function (sSource, aoData, fnCallback) {
aoData.push({ "name": "all", "value": true });
$.getJSON(sSource, aoData, function (json) {
fnCallback(json)
});
},
//*** Added the following code ****
"columnDefs": [
{
"width": "5%", "targets": 'NumCol', "data": "id",
"orderable": false,
"render": function (data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
}
},
{
"targets": 'LoginCol',
"data": "LoginID", "orderable": true
},
{
"targets": 'NameCol',
"data": "Name", "orderable": true
},
{
"targets": 'DtCreatedCol',
"data": "DateCreated", "orderable": true
},
{
"targets": 'EditCol', // The third column
"className": "text-center",
"orderable": false,
"render": function (data, type, full, meta) {
return '<a href="/Password/PasswordDet/' + full.AutoINC + '"><img src="../../Content/myPics/edit-2-24.png" ></a>';
}
}
],
});
$('body').on('click', '#btnGet', function () {
//to get currently clicked row object
var row = $(this).parents('tr')[0];
//to get currently clicked row data
var rowData = table.row(row).data();
//to get currently clicked row id (first column value (at index [0])
var rowId = rowData[0];
var row = $(this).closest("tr"), // Finds the closest row <tr>
rowId = row.find("td:nth-child(1)").text();
});
Murat 建议的控制器未调用并挂起
【问题讨论】:
-
您是否在
Request.QueryString中获得任何数据?你在AjaxGetJsonDataGet或Post上使用什么HttpVerb? -
只需使用 Request["search[value]"]。我认为这会奏效。
-
@UxmaanAli 无法使用 Request["search[value]"]。
-
@vikscool 我正在使用
Get我已经发布了 Ajax 代码和其他内容。 -
您获得
search[value]的blank/empty字符串的原因是您启用了"filter": true,它启用了表格右上角的搜索框。您正在尝试从每列的各个搜索框中调用搜索,但您正在读取的值来自主搜索框。因此,将读取逻辑更改为:columns[index][search][value]以使搜索工作。
标签: c# ajax asp.net-mvc datatable datatables