isDragTarget: boolean;
isSortable: boolean;
isMultiSortable: boolean;
+
+ // True if the column was automatically generated.
+ isAuto: boolean;
+
flesher: (obj: any, col: GridColumn, item: any) => any;
getCellContext(row: any) {
add(col: GridColumn) {
this.applyColumnDefaults(col);
-
- if (this.getColByName(col.name)) { return; } // avoid dupes
+
+ if (!this.insertColumn(col)) {
+ // Column was rejected as a duplicate.
+ return;
+ }
if (col.isIndex) { this.indexColumn = col; }
}
this.applyColumnSortability(col);
+ }
+
+ // Returns true if the new column was inserted, false otherwise.
+ // Declared columns take precedence over auto-generated columns
+ // when collisions occur.
+ // Declared columns are inserted in front of auto columns.
+ insertColumn(col: GridColumn): boolean {
+
+ if (col.isAuto) {
+ if (this.getColByName(col.name)) {
+ // New auto-generated column conflicts with existing
+ // column. Skip it.
+ return false;
+ } else {
+ // No collisions. Add to the end of the list
+ this.columns.push(col);
+ return true;
+ }
+ }
+
+ // Adding a declared column.
+
+ // Check for dupes.
+ for (let idx = 0; idx < this.columns.length; idx++) {
+ const testCol = this.columns[idx];
+ if (testCol.name === col.name) { // match found
+ if (testCol.isAuto) {
+ // new column takes precedence, remove the existing column.
+ this.columns.splice(idx, 1);
+ break;
+ } else {
+ // New column does not take precedence. Avoid
+ // inserting it.
+ return false;
+ }
+ }
+ }
+ // Delcared columns are inserted just before the first auto-column
+ for (let idx = 0; idx < this.columns.length; idx++) {
+ const testCol = this.columns[idx];
+ if (testCol.isAuto) {
+ if (idx === 0) {
+ this.columns.unshift(col);
+ } else {
+ this.columns.splice(idx - 1, 0, col);
+ }
+ return true;
+ }
+ }
+
+ // No insertion point found. Toss the new column on the end.
this.columns.push(col);
+ return true;
}
getColByName(name: string): GridColumn {
col.idlFieldDef = field;
col.datatype = field.datatype;
col.isIndex = (field.name === pkeyField);
+ col.isAuto = true;
this.columnSet.add(col);
});
}