Is there a way to sort a datatable by column? I have tried:
fd.control('SOWTable').widget.dataSource._sort = {field: "ItemCategory", dir: "asc"};
I do not get an error but do not get the desired result.
Hello @cwalter2,
Please try out this code:
fd.control('DataTable1').widget.dataSource.sort( { field: "Column1", dir: "asc" });
//clears sorting
fd.control('DataTable1').widget.dataSource.sort({})
Hello,
Is it also possible to save the datatable sorted (on two columns) to the SharePoint field?
(I found some code to sort the data in the SharePoint field, but the result is every time overwritten)
Thank you,
Daniël
var sort_by;
var hours = ;// sort utility functions
var default_cmp = function(a, b) {
if (a == b) return 0;
return a < b ? -1 : 1;
},
getCmpFunc = function(primer, reverse) {
var cmp = default_cmp;
if (primer) {
cmp = function(a, b) {
return default_cmp(primer(a), primer(b));
};
}
if (reverse) {
return function(a, b) {
return -1 * cmp(a, b);
};
}
return cmp;
};// actual implementation function sort_by() { var fields = [], n_fields = arguments.length, field, name, reverse, cmp; // preprocess sorting options for (var i = 0; i < n_fields; i++) { field = arguments[i]; if (typeof field === 'string') { name = field; cmp = default_cmp; } else { name = field.name; cmp = getCmpFunc(field.primer, field.reverse); } fields.push({ name: name, cmp: cmp }); } return function(A, B) { var a, b, name, cmp, result; for (var i = 0, l = n_fields; i < l; i++) { result = 0; field = fields[i]; name = field.name; cmp = field.cmp; result = cmp(A[name], B[name]); if (result !== 0) break; } return result; }; }
hours = JSON.parse(fd.field('SPfield').value);
hours.sort(sort_by('Dag', {
name: 'Open',
primer: parseInt,
reverse: false
}));fd.field('SPfield').value = JSON.stringify(hours);
Hello @danieljr,
You need to re-write the DataTable control with the sorted data, not the field that stores it's value.
function sortAscending( a, b ) {
if ( a.Column1 < b.Column1 ){
return -1;
}
if ( a.Column1 > b.Column1 ){
return 1;
}
return 0;
}
var sorted = fd.control('DataTable1').value.sort(sortAscending);
//clear DataTable
fd.control('DataTable1').clear();
//add sorted data to the control
fd.control('DataTable1').value = sorted;