Code for Add/Moving rows
Code for Add/Moving rows
This isn't a plug-in, but some methods I wrote. The InsertDataTableRow method assumes that the data is a single array. More handling would be needed to handle other possibilities. Also, I am sure there is room for improvement. I am just curious what others think, and if they have suggestions. Thanks.
var GetDataTableObject = function (table) {
var $table = null;
if (typeof table == 'string') {
$table = $(table);
} else if (table instanceof $) {
$table = table;
} else {
$table = $(table);
}
if (!$.fn.DataTable.fnIsDataTable($table[0])) {
return null;
} else {
return $table.dataTable();
}
}
var InsertDataTableRow = function (table, data, newRowPos, sortColPos, sort) {
var result = false;
var $dataTable;
$dataTable = GetDataTableObject(table);
if ($dataTable == null) {
return;
}
var numRows = $dataTable.fnSettings().fnRecordsTotal();
if (newRowPos == null || newRowPos < 0) {
data[sortColPos] = numRows;
$dataTable.fnAddData(data, true);
} else {
var currentRow;
for (var i = 0; i < numRows; i++) {
currentRow = $dataTable.fnGetData(i);
if (currentRow[sortColPos] >= newRowPos) {
$dataTable.fnUpdate(parseInt(currentRow[sortColPos],10) + 1, i, sortColPos, false, false);
}
}
data[sortColPos] = newRowPos;
$dataTable.fnAddData(data, true);
}
if ($dataTable.fnSettings().fnRecordsTotal() > numRows) {
result = true;
}
if (result && sort) {
$dataTable.fnSort([[sortColPos, 'asc']]);
}
return result;
}
var MoveDataTableRow = function (sourceTable, targetTable, row, position) {
var insertResult = false;
var $sourceDataTable;
var $targetDataTable;
var $row;
$sourceDataTable = GetDataTableObject(sourceTable);
if ($sourceDataTable == null) {
return;
}
$targetDataTable = GetDataTableObject(targetTable);
if ($targetDataTable == null) {
return;
}
if (typeof row == 'string') {
$row = $(row);
} else if (row instanceof $) {
$row = row;
} else {
$row = $(row);
}
var data = $sourceDataTable.fnGetData(row[0]);
*** Note that the 0 is a hard coded sequence column position based on my implementation.
insertResult = InsertDataTableRow($targetDataTable, data, position, 0, true);
var deletedRow = $sourceDataTable.fnDeleteRow($row[0]);
return (insertResult && deletedRow != null && deletedRow.length != 0);
}
var GetDataTableObject = function (table) {
var $table = null;
if (typeof table == 'string') {
$table = $(table);
} else if (table instanceof $) {
$table = table;
} else {
$table = $(table);
}
if (!$.fn.DataTable.fnIsDataTable($table[0])) {
return null;
} else {
return $table.dataTable();
}
}
var InsertDataTableRow = function (table, data, newRowPos, sortColPos, sort) {
var result = false;
var $dataTable;
$dataTable = GetDataTableObject(table);
if ($dataTable == null) {
return;
}
var numRows = $dataTable.fnSettings().fnRecordsTotal();
if (newRowPos == null || newRowPos < 0) {
data[sortColPos] = numRows;
$dataTable.fnAddData(data, true);
} else {
var currentRow;
for (var i = 0; i < numRows; i++) {
currentRow = $dataTable.fnGetData(i);
if (currentRow[sortColPos] >= newRowPos) {
$dataTable.fnUpdate(parseInt(currentRow[sortColPos],10) + 1, i, sortColPos, false, false);
}
}
data[sortColPos] = newRowPos;
$dataTable.fnAddData(data, true);
}
if ($dataTable.fnSettings().fnRecordsTotal() > numRows) {
result = true;
}
if (result && sort) {
$dataTable.fnSort([[sortColPos, 'asc']]);
}
return result;
}
var MoveDataTableRow = function (sourceTable, targetTable, row, position) {
var insertResult = false;
var $sourceDataTable;
var $targetDataTable;
var $row;
$sourceDataTable = GetDataTableObject(sourceTable);
if ($sourceDataTable == null) {
return;
}
$targetDataTable = GetDataTableObject(targetTable);
if ($targetDataTable == null) {
return;
}
if (typeof row == 'string') {
$row = $(row);
} else if (row instanceof $) {
$row = row;
} else {
$row = $(row);
}
var data = $sourceDataTable.fnGetData(row[0]);
*** Note that the 0 is a hard coded sequence column position based on my implementation.
insertResult = InsertDataTableRow($targetDataTable, data, position, 0, true);
var deletedRow = $sourceDataTable.fnDeleteRow($row[0]);
return (insertResult && deletedRow != null && deletedRow.length != 0);
}
This discussion has been closed.