// // o---------------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net/license.html | // | | // | RGraph is dual-licensed under the Open Source GPL license. That means that it's | // | free to use and there are no restrictions on what you can use RGraph for! | // | If the GPL license does not suit you however, then there's an inexpensive | // | commercial license option available. See the URL above for more details. | // o---------------------------------------------------------------------------------o // // Having this here means that the RGraph libraries can be // included in any order, instead of you having to include // the common core library first. // // Define the RGraph global variable RGraph = window.RGraph || {isrgraph:true,isRGraph: true,rgraph:true}; // // The constructor. This function sets up the object. // RGraph.Datagrid = RGraph.DataGrid = function (conf) { // // This facilitates easy clearing of the datagrid. It // empties the nodes of the container div tag. // this.clear = function () { RGraph.fireCustomEvent(this, 'beforeclear'); // Reset the row_selected flag on the data. Do not use the // deselectAll() function here as rowSelectable may not be // enabled // for(var i=0; i a) { return 1; } else if (b < a) { return -1; } else { return 0; } } } else if (dir === 1) { // Allow for a custom sort function // if (RGraph.isFunction (obj.properties.sortableCompare)) { return (obj.properties.sortableCompare)(obj, col, dir, a[col].value, b[col].value); } else if (RGraph.isArray (obj.properties.sortableCompare) && RGraph.isFunction (obj.properties.sortableCompare[col])) { return (obj.properties.sortableCompare[col])(obj, col, dir, a[col].value, b[col].value); } else { var a = a[col].value; var b = b[col].value; if (RGraph.isString(a)) a = a.toLowerCase(); if (RGraph.isString(b)) b = b.toLowerCase(); if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } } } else { a[col].original_index - b[col].original_index; } }); RGraph.fireCustomEvent(obj, 'sort'); return data; }; // // This function saves the size of the columns // to a variable in localStorage (in the browser). // this.saveColumnSizesToLocalStorage = function (obj) { if (obj.properties.columnsResizablePersistent) { var str = JSON.stringify(obj.columnWidths); window.localStorage[obj.resizableColumnsPersistentLocalStorageKey] = str; } }; // // This function loads the size of the columns // into the object. // this.loadColumnSizesFromLocalStorage = function (obj) { if (obj.properties.columnsResizablePersistent && window.localStorage[obj.resizableColumnsPersistentLocalStorageKey] && window.localStorage[obj.resizableColumnsPersistentLocalStorageKey].length) { var data = JSON.parse(window.localStorage[obj.resizableColumnsPersistentLocalStorageKey]); return data; } }; // // This function allows you to reset column widths back // to their original, unaltered state. // // @param boolean twice This function can be run twice, // and is by default. This is due // to a strange bug. // this.resetColumnWidths = function (twice = true) { this.columnWidths = []; this.calculateColumnWidths(); window.localStorage[this.resizableColumnsPersistentLocalStorageKey] = ''; this.redraw(); // // Strange but necessary. // if (twice) { this.resetColumnWidths(false); } }; // // This function is for saving the sort state to // localStorage - making for persistent sorting. // this.saveSortDataToLocalStorage = function (obj) { if (obj.properties.sortablePersistent) { var key = obj.sortableColumnPersistentLocalStorageKey; window.localStorage[key] = JSON.stringify([ obj.sortColumn, obj.sortDir ]); } }; // // This function is for loading the sort information from // localStorage. // this.loadSortDataFromLocalStorage = function (obj) { if (obj.properties.sortablePersistent) { var key = obj.sortableColumnPersistentLocalStorageKey; var str = localStorage[obj.sortableColumnPersistentLocalStorageKey]; var arr = []; if (str) { arr = JSON.parse(str); } return arr; } }; // // This function allows you to reset the sort data back // to the default "not sorted" state // this.resetSortData = function () { window.localStorage[this.sortableColumnPersistentLocalStorageKey] = ''; this.sortColumn = null; this.sortDir = null; // // Sort the array so that the data goes back to its // original order // this.data.sort(function (a,b) { return a[0].original_row_index - b[0].original_row_index; }); this.redraw(); }; // Returns the data // // @param mixed ... This can be a string noting the cell // to get. It's optional and if not given // the whole data is returned as 2D array. // this.getData = function () { var args = RGraph.getArgs(arguments, 'id'); var out = []; // // Return ALL of the data. Simply call the function with //no arguments: // // myDatagrid.getData(); // // if (RGraph.isNullish(args.id)) { for (var i=0; i (data.length - 1) ) { return this.append(args.data); } if (args.index < 0) { args.index = args.index + data.length; } if (args.index < 0) { args.index = 0; } // // Insert the data into the data array at the correct // place // for (var i=0,newData=[]; i