From: Scott Baker Date: Thu, 11 Dec 2014 10:27:04 +0000 (-0800) Subject: datatable in a marionette view, WIP X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=1d364e74304593e7057ddb3b37958c4f34ce42f4;p=plstackapi.git datatable in a marionette view, WIP --- diff --git a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js index 5ebc006..b7cfc58 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js +++ b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js @@ -702,6 +702,124 @@ XOSListView = FilteredCompositeView.extend({ }, }); +XOSDataTableViewOld = Marionette.View.extend( { + //tagName: "table", + el: "
", + + initialize: function() { + //$(this.el).html('

scott was here

' ); + }, + + render: function() { + //this.el = "
"; + console.log(this.el); + console.log(this.$el); + console.log("render!"); + actualEntries = []; + actualEntries.push(["foo", "bar", "1", "2"]); + actualEntries.push(["zoo", "zar", "0", "9"]); + + oTable = $(this.el).find("table").dataTable( { + "bJQueryUI": true, + "aaData": actualEntries, + "bStateSave": true, + "aoColumns": [ + { "sTitle": "Slice" }, + { "sTitle": "Privilege" , sClass: "alignCenter"}, + { "sTitle": "Number of Slivers" , sClass: "alignCenter"}, + { "sTitle": "Number of Sites" , sClass: "alignCenter"}, + ] + } ); + dtcounter = dtcounter + 1; + + return this; + }, + +}); + +XOSDataTableView = Marionette.View.extend( { + el: '
', + + filter: undefined, + + initialize: function() { + }, + + render: function() { + var view = this; + + view.aoColumns = []; + _.each(this.collection.listFields, function(fieldName) { + mRender = undefined; + if (fieldName in view.collection.foreignFields) { + var foreignCollection = view.collection.foreignFields[fieldName]; + mRender = function(x) { return idToName(x, foreignCollection, "humanReadableName"); }; + } else if ($.inArray(fieldName, view.collection.detailLinkFields)>=0) { + var collectionName = view.collection.collectionName; + mRender = function(x,y,z) { return '' + x + ''; }; + } + view.aoColumns.push( {sTitle: fieldNameToHumanReadable(fieldName), mData: fieldName, mRender: mRender} ); + }); + + oTable = $(this.el).find("table").dataTable( { + "bJQueryUI": true, + "bStateSave": true, + "bServerSide": true, + "aoColumns": view.aoColumns, + + fnServerData: function(sSource, aoData, fnCallback, settings) { + var compareColumns = function(sortCols, sortDirs, a, b) { + result = a[sortCols[0]] < b[sortCols[0]]; + if (sortDirs[0] == "desc") { + result = -result; + } + return result; + }; + + // function used to populate the DataTable with the current + // content of the collection + var populateTable = function() + { + // clear out old row views + rows = []; + + sortDirs = []; + sortCols = []; + _.each(aoData, function(param) { + if (param.name == "sSortDir_0") { + sortDirs = [param.value]; + } else if (param.name == "iSortCol_0") { + sortCols = [view.aoColumns[param.value].mData]; + } + }); + + aaData = view.collection.toJSON(); + + if (view.filter) { + aaData = aaData.filter( function(row) { model = {}; model.attributes = row; return view.filter(model); } ); + } + + aaData.sort(function(a,b) { return compareColumns(sortCols, sortDirs, a, b); }); + + // these 'meta' attributes are set by the collection's parse method + var totalSize = view.collection.length; + var filteredSize = view.collection.length; + + return fnCallback({iTotalRecords: totalSize, + iTotalDisplayRecords: filteredSize, + aaData: aaData}); + }; + + aoData.shift(); // ignore sEcho + populateTable(); + }, + } ); + + return this; + }, + +}); + idToName = function(id, collectionName, fieldName) { return xos.idToName(id, collectionName, fieldName); };