I am working on a C# ASP.NET Core MVC app and I use jQuery datatable to display data from a database. The problem is I am not able to select single or multiple rows in this table.
I have a table like this in my .cshtml:
<table class="table display table-bordered" id="DATATABLE">
</table>
This is in the script tag:
$("#DATATABLE").DataTable({
serverSide: true,
filter: true,
searchDelay: 1000,
scrollY: StaticData.TABLE_HEIGHT + 'px',
lengthMenu: StaticData.TABLE_PAGE_SIZE,
language: { searchPlaceholder: "Name, Teacher" },
scrollCollapse: true,
ajax: {
url: '/STUD_MANAGEMENT/LoadStud',
type: 'GET',
datatype: 'json',
headers: { 'RequestVerificationToken': 'your json token' },
data: (d) => {
return { draw: d.draw, start: d.start, length: d.length, search: d.search.value, FilterByColumn: d.columns[d.order[0].column].data, ASC_DSEC: d.order[0].dir }
},
beforeSend: () => { ShowLoader(); },
complete: () => { HideLoader(); },
dataSrc: (json) => {
json = json.data;
return json;
}
},
columnDefs: [{ className: "dt-center", targets: [5, 6, 7, 8, 11], width: '2%', }],
columns: [
{ data: 'STUD_ID', title: 'STUD ID', autoWidth: false, visible: false },
{ data: 'CLASS_ID', title: 'CLASS ID', autoWidth: false, visible: false },
{ data: 'NAME', title: 'Name', autoWidth: true, searchable: true },
{ data: 'AGE', title: 'Age', autoWidth: true },
{ data: 'TEACHER', title: 'Teacher', autoWidth: true },
]
});
And the LoadStud()
method in the STUD_MANAGEMENT
controller is shown here:
public IActionResult LoadStud(int draw = 1, int start = 0, int length = 10,
string search = "", string FilterByColumn = "", string ASC_DSEC = "")
{
List<STUD_MANAGEMENT> ListData = new List<STUD_MANAGEMENT>();
int recordsTotal = 0;
STUD_MANAGEMENT dm = new STUD_MANAGEMENT();
dm.STUD_ID = 1;
dm.CLASS_ID = 1;
dm.NAME = "James";
dm.TEACHER = "SEPHORA";
dm.AGE = "12";
STUD_MANAGEMENT dm1 = new STUD_MANAGEMENT();
dm1.STUD_ID = 2;
dm1.CLASS_ID = 2;
dm1.NAME = "Naneem";
dm1.TEACHER = "Chithra";
dm1.AGE = "15";
STUD_MANAGEMENT dm11 = new STUD_MANAGEMENT();
dm11.STUD_ID = 3;
dm11.CLASS_ID = 3;
dm11.NAME = "Sony";
dm11.TEACHER = "Mano USA";
dm11.AGE = "3";
ListData.Add(dm);
ListData.Add(dm1);
ListData.Add(dm11);
recordsTotal = ListData.Count();
var jsonData = new { draw = draw,
recordsFiltered = recordsTotal,
recordsTotal = recordsTotal,
data = ListData };
return Ok(jsonData);
}
This is the STUD_MANAGEMENT
class:
public class STUD_MANAGEMENT
{
public int STUD_ID { get; set; }
public int CLASS_ID { get; set; }
public string NAME { get; set; }
public int AGE { get; set; }
public string TEACHER { get; set; }
}
I want to select single/multiple row in this datatable and suppose that there is a button near by this datatable and onclick() of that button should return the selected row (s) of this datatable.
2
Answers
You should configure multi-row selection support using
select: { style: 'multi' }
.Then, you can add a button under the table:
To get the selected row data correctly when clicking a button, use
.selected
class to manage the row selection state by adding or removing the selected class:Edit:
It could be due to version differences. Below are the versions I am using:
My test result:
DataTables Select is an extension library for DataTables. To use it, you must import the library:
I believe that the 1.7.0 version should be compatible with the DataTables 1.x.x version.
If your downloaded DataTables package includes the DataTables Select files, you can import from the local file instead of the CDNJS website. Otherwise, you can choose to download the DataTables package by including the Select library but it seems to download with the latest version (DataTables 2.1.4) only. The Select library will be included in the datatables.js/datatables.min.js file and you don’t need to import the library externally as I did on top.
And enable the multiple selection setting in the DataTables:
Demo @ StackBlitz