Sfoglia il codice sorgente

TableAjax fix plugin state (data: rows, cols)

Piotr Labudda 10 anni fa
parent
commit
2d51368adc
1 ha cambiato i file con 53 aggiunte e 30 eliminazioni
  1. 53 30
      SE/se-lib/TableAjax.php

+ 53 - 30
SE/se-lib/TableAjax.php

@@ -553,6 +553,7 @@ class TableAjax extends ViewAjax {
 		var _mapEditorWrap; // map editor wrapper node
 		var _mapEditorDialog; // map editor jquery ui dialog node
 
+		var _state;// state - to replace variables below (date, ...)
 		var _data; //columns and rows
 		var _currPage = 1; // current page
 		var _pageSize; // current pagesize
@@ -634,12 +635,15 @@ class TableAjax extends ViewAjax {
 		 */
 		priv.renderTable = function () {
 			//create table itself
+/// console.log('renderTable:: _data', _data);//TODO:DBG:RMME
+/// console.log('renderTable:: _table', _table);//TODO:DBG:RMME
 			if (!_table) {
 				_head = _body = _foot = undefined;
 				_table = $('<table class="AjaxTable table table-striped table-hover table-bordered table-condensed"></table>').appendTo(_cont);
 			}
 
 			//create the header which will later hold both sorting and filtering
+/// console.log('renderTable:: _head', _head);//TODO:DBG:RMME
 			if (!_head) {
 				_table.find('thead').remove();
 				_headSort = _headFilter = _headSpecialFilter = undefined;
@@ -1300,8 +1304,6 @@ class TableAjax extends ViewAjax {
 		};
 
 		priv.renderFooter = function() {
-///				$(_cont).next('.foot').remove();
-///				_foot = $('<div class="foot"></div>').insertAfter(_cont);
 			var currentFooterNode = $(_cont).next('.foot');
 			if (0 === currentFooterNode.length) {// initial create .foot element after _cont
 				currentFooterNode = $('<div class="foot"></div>').insertAfter(_cont);
@@ -1591,13 +1593,13 @@ class TableAjax extends ViewAjax {
 				success: function (data) {
 					if (priv.options.debug) console.log('request finished L.<?php echo __LINE__; ?>');
 
-					//assign the new data
+					// assign the new data
 					if (data.d && data.d.cols)
-						priv.setData(data.d, skipCols, resetChecked);
+						priv.setStateData(data.d, skipCols, resetChecked);
 					else
-						priv.setData(data, skipCols, resetChecked);
+						priv.setStateData(data, skipCols, resetChecked);
 
-					// set initial filters
+					// set initial filters (_filterCols)
 					if (filtersInitSet && _data && _data.cols) {
 						$.map(reqData, function(fltrValue, fltr){
 							var colName = null;
@@ -1618,11 +1620,19 @@ class TableAjax extends ViewAjax {
 						});
 					}
 
-					//assign the new data
-					if (data.d && data.d.cols)
-						priv.setData(data.d, skipCols, resetChecked);
-					else
-						priv.setData(data, skipCols, resetChecked);
+					// assign the new state (data)
+					state = {data: {}};
+					if (data.d && data.d.cols) {
+						if (!skipCols) state.data.cols = data.d.cols;
+						state.data.rows = data.d.rows || [];
+						state.data.total = data.d.total || 0;
+					}
+					else {
+						if (!skipCols) state.data.cols = data.cols || {};
+						state.data.rows = data.rows || [];
+						state.data.total = data.total || 0;
+					}
+					priv.setState(state);
 
 					if (typeof callback == "function")
 						callback.call(this);
@@ -1638,7 +1648,26 @@ class TableAjax extends ViewAjax {
 		/*
 		 assigns the new data.
 		 */
-		priv.setData = function (pData, skipCols, resetChecked) {
+		priv.setState = function (state) {
+			var oldState = _state,// TODO: use to check what really changed
+					renderParts = [];
+			if (state.data) {
+				if (state.data.cols && state.data.cols.length > 0) {
+					priv.setStateData(state.data);
+					renderParts.push('head');
+				} else {
+					priv.setStateData(state.data, true);
+				}
+				renderParts.push('body');
+			}
+
+/// console.log('setState::renderParts: ', renderParts);//TODO:DBG:RMME
+			if (renderParts.length > 0) {
+				jQuery(_cont).trigger('TableAjax:render', renderParts);
+			}
+		};
+
+		priv.setStateData = function (pData, skipCols, resetChecked) {
 			var data = $.extend(true, {}, pData);
 			data.fromRow = _data && _data.fromRow || 0;
 			data.toRow = _data && _data.toRow || 0;
@@ -1668,13 +1697,6 @@ class TableAjax extends ViewAjax {
 			//wash the new data a bit
 			_uniqueCol = "";
 			$.each(_data.cols, function (col, props) {
-				//set sorting
-				if (!_currSortCol && props.sortOrder) {
-					_currSortCol = col;
-					_currSortFlip = props.sortOrder != "asc";
-				}
-
-				//default to string type if missing
 				if (!props.type) _data.cols[col].type = "string";
 
 				//if several unique columns is defined, use the first.
@@ -1683,8 +1705,6 @@ class TableAjax extends ViewAjax {
 					else props.unique = false;
 				}
 
-				//if index property is missing, create one
-				if (!props.index) _data.cols[col].index = new Number(0);//priv.ext.XDate();
 				props.column = col;
 
 				//set any initial filter
@@ -1722,11 +1742,6 @@ class TableAjax extends ViewAjax {
 						_uniqueCols[row[_uniqueCol]] = row;
 				});
 			}
-
-			if (!skipCols) {
-				_head = undefined;
-			}
-			jQuery(_cont).trigger('TableAjax:render', ['body', 'foot']);
 		};
 
 		/**
@@ -2506,10 +2521,18 @@ class TableAjax extends ViewAjax {
 				data: reqData,
 				async: true,
 				success: function (data) {
-					if (data.d && data.d.cols)
-						priv.setData(data.d, skipCols, resetChecked);
-					else
-						priv.setData(data, skipCols, resetChecked);
+					state = {data: {}};
+					if (data.d && data.d.cols) {
+						if (!skipCols) state.data.cols = data.d.cols;
+						state.data.rows = data.d.rows || [];
+						state.data.total = data.d.total || 0;
+					}
+					else {
+						if (!skipCols) state.data.cols = data.cols || {};
+						state.data.rows = data.rows || [];
+						state.data.total = data.total || 0;
+					}
+					priv.setState(state);
 					_table.parent().parent().removeClass('AjaxTable-loading');
 				},
 				error: function (err) {