You could dynamically auto-size the column. This is the solution i was able to come up with. Self.tables()[tableIndex] is an object like {Rows: [], Headers: []}. Additionally, you could get the type of the column dynamically for sorting.
self.createJSGridTable = function (cssIdOfTable, tableIndex) {
$("#" + cssIdOfTable).jsGrid({
width: "100%",
height: "400px",
inserting: false,
editing: false,
sorting: true,
paging: true,
noDataContent: "No Record Found",
loadMessage: "Please, wait...",
data: self.tables()[tableIndex].Rows,
fields: self.getColumns(self.tables()[tableIndex])
});
$("#sort").click(function () {
var field = $("#sortingField").val();
$("#" + cssIdOfTable + "").jsGrid("sort", field);
});
};
self.getColumns = function (table) {
columnsAsObjects = [];
var props = [];
if (table != null && table.Rows != null && table.Rows.length > 0) {
props = self.propertyNamesOfRow(table.Rows[0]);
}
for (var i = 0; i < props.length; i++) {
var currentProp = props[i].toString();
columnsAsObjects.push({ name: currentProp, type: self.getType(table.Rows, currentProp), width: self.getColumnWidth(table.Rows, currentProp) + "px" });
}
return columnsAsObjects;
};
self.getType = function (rows, columnName) {
for (var i = 0; i < rows.length; i++) {
if (self.isNumber(rows[i][columnName])) {
return "text";
}
}
return "number";
};
self.getColumnWidth = function (rows, columnName) {
var maxLength = columnName.toString().length;
for (var i = 0; i < rows.length; i++) {
if (rows[i][columnName] != null) {
maxLength = rows[i][columnName].toString().length > maxLength ? rows[i][columnName].toString().length : maxLength;
}
}
return maxLength * 10;
};
self.isNumber = function(value){
return value != null && value != "" && typeof value != "number" && isNaN(parseInt(value));
};
Additionally, I made a whole helper for doing JSGrid type stuff dynamically within my app.
```javascript
var JSGridHelper = (function () {
function Controller(records, columnsWithDate) {
var self = this;
self.records = records;
self.columnsWithDate = columnsWithDate;
self.getControllerObject = {
loadData: function (filter) {
return $.grep(self.records, function (record) {
return shouldRecordBeKeptInResults(filter, record, self.records, self.columnsWithDate);
});
}
};
};
var shouldRecordBeKeptInResults = function (filter, record, records, columnsWithDate) {
var props = propertyNamesOfRow(record);
var returnRecord = true;
for (var i = 0; i < props.length; i++) {
var currentProp = props[i].toString();
if (filterAndRecordValuesAreNotEqual(filter[currentProp], record[currentProp])) {
if (getType(records, currentProp, columnsWithDate) === "text") {
if (record[currentProp] !== null && !doesRecordIncludeFilterValue(record[currentProp], filter[currentProp])) {
returnRecord = false;
}
}
else {
returnRecord = false;
}
}
}
return returnRecord;
};
var filterAndRecordValuesAreNotEqual = function (filterValue, recordValue) {
return filterValue && filterValue !== '' && recordValue != filterValue;
};
var doesRecordIncludeFilterValue = function (recordValue, filterValue) {
return recordValue.toLowerCase().includes(filterValue.toLowerCase());
};
var createNewJSGridForEachTable = function (tables, columnsWithDate) {
var baseId = "jsGrid";
for (var i = 0; i < tables.length; i++) {
if (i == 0) {
$("#tablesDiv").empty();
}
var jsGridId = baseId + (parseInt(i) + 1);
$("#tablesDiv").append('<div id="' + jsGridId + '"></div>');
createJSGridTable(tables, jsGridId, i, columnsWithDate);
}
};
var createJSGridTable = function (tables, cssIdOfTable, tableIndex, columnsWithDate) {
var controllerInstance = new Controller(tables[tableIndex].Rows, columnsWithDate);
$("#" + cssIdOfTable).jsGrid({
width: "100%",
height: "400px",
filtering: true,
inserting: false,
editing: false,
sorting: true,
autoload: true,
paging: true,
controller: controllerInstance.getControllerObject,
noDataContent: "No Record Found",
loadMessage: "Please, wait...",
data: tables[tableIndex].Rows,
fields: getColumns(tables[tableIndex], columnsWithDate)
});
$("#sort").click(function () {
var field = $("#sortingField").val();
$("#" + cssIdOfTable + "").jsGrid("sort", field);
});
};
var propertyNamesOfRow = function (obj) {
var returnArray = [];
$.each(obj, function (key, value) { returnArray.push(key); });
return returnArray;
};
var getColumns = function (table, columnsWithDate) {
columnsAsObjects = [];
var props = [];
if (table != null && table.Rows != null && table.Rows.length > 0) {
props = propertyNamesOfRow(table.Rows[0]);
}
for (var i = 0; i < props.length; i++) {
var currentProp = props[i].toString();
columnsAsObjects.push({ name: currentProp, type: getType(table.Rows, currentProp, columnsWithDate), width: getColumnWidth(table.Rows, currentProp) + "px" });
}
columnsAsObjects.push(
{
type: "control",
modeSwitchButton: false,
editButton: false,
deleteButton: false,
}
);
return columnsAsObjects;
};
var getType = function (rows, columnName, columnsWithDate) {
if (rows && rows.length > 0) {
if (isDate(rows[0], columnName, columnsWithDate)) {
return "date";
}
for (var i = 0; i < rows.length; i++) {
if (rows[i][columnName] !== null && !isNotANumber(rows[i][columnName])) {
return "number";
}
}
}
return "text";
};
var getColumnWidth = function (rows, columnName) {
var maxLength = columnName.toString().length;
for (var i = 0; i < rows.length; i++) {
if (rows[i][columnName] != null) {
maxLength = rows[i][columnName].toString().length > maxLength ? rows[i][columnName].toString().length : maxLength;
}
}
return maxLength * 10;
};
var isDate = function (row, columnName, columnsWithDate) {
if (columnsWithDate && columnName) {
var props = Object.keys(row);
for (var j = 0; j < columnsWithDate.length; j++) {
for (var k = 0; k < props.length; k++) {
if (props[k] == columnName && columnsWithDate[j] == k) {
return true;
}
}
}
}
return false;
};
var isNotANumber = function (value) {
return value != null && value != "" && typeof value != "number" && isNaN(parseInt(value));
};
return {
CreateNewJSGridForEachTable: createNewJSGridForEachTable
}
})();
'''
Most helpful comment
I was able to accommodate column resizing using the colResizable library and adding the following code to my grid: