Permalink
Browse files

fix: datatable支持前端缓存,datatable存储当前页数据,page中存储每页的数据

  • Loading branch information...
liuyk
liuyk committed Nov 25, 2016
1 parent ca8685d commit b464a09a8de5df2da0f32865bae7f78d11285344
View
@@ -27,7 +27,10 @@ const setData = function (data,options) {
if(data.totalRow || data.totalRow === 0){
var newTotalRow = data.totalRow;
}else{
var newTotalRow = data.rows.length; //后续要考虑状态,del的不计算在内
if(data.rows)
var newTotalRow = data.rows.length;
else
var newTotalRow = this.totalRow();
}
var select, focus,unSelect=options?options.unSelect:false;
@@ -41,19 +44,30 @@ const setData = function (data,options) {
return;
}
else {
// 首先删除数据,然后将当前页数据插入
this.removeAllRows();
select = this.getPage(newIndex).selectedIndices
focus = this.getPage(newIndex).focus
this.setRows(this.getPage(newIndex).rows, options)
var rows = this.setRows(this.getPage(newIndex).rows, options)
this.getPage(newIndex).rows = rows;
}
// 后台传入totalPages及totalRow才进行更新
if(data.totalPages){
this.totalPages(data.totalPages)
}
if(data.totalRow){
this.totalRow(data.totalRow)
}
} else {
select = data.select||(!unSelect?[0]:[]);
focus = data.focus !== undefined ? data.focus : data.current;
this.setRows(data.rows, options);
this.totalPages(newTotalPages)
this.totalRow(newTotalRow)
}
this.pageIndex(newIndex)
this.pageSize(newSize)
this.totalPages(newTotalPages)
this.totalRow(newTotalRow)
this.updateSelectedIndices()
View
@@ -15,104 +15,158 @@ const getData = function () {
return datas
}
/**
* 将page转为row对象格式
*/
const page2data = function(page, pageIndex){
var data = {}
data.focus = page.focus;
data.index = pageIndex;
data.select = page.selectedIndices;
return data;
}
const getDataByRule = function (rule) {
var returnData = {}, datas = null, rows;
returnData.meta = this.meta
returnData.params = this.params
rule = rule || DataTable.SUBMIT.current
if (rule == DataTable.SUBMIT.current) {
datas = []
var currIndex = this.focusIndex()
if (currIndex == -1)
currIndex = this.getSelectedIndex()
rows = this.rows();
for (var i = 0, count = rows.length; i < count; i++) {
if (i == currIndex)
datas.push(rows[i].getData())
else
datas.push(rows[i].getEmptyData())
rule = rule || DataTable.SUBMIT.current;
// 存在多页及不存在多页分开处理
if(this.pageCache){
var pages = this.getPages();
if(rule == DataTable.SUBMIT.current || rule == DataTable.SUBMIT.focus){
datas = []
var pageIndex = this.pageIndex();
var currPage = pages[pageIndex];
if(currPage){
var currIndex = this.focusIndex()
if(rule == DataTable.SUBMIT.current){
if (currIndex == -1)
currIndex = this.getSelectedIndex()
}
var data = page2data(currPage,pageIndex);
data.rows = [];
for(var i = 0, count = currPage.rows.length; i < count; i++){
var row = currPage.rows[i].getData();
if(i != currIndex)
row.data = {}
data.rows.push(row);
}
datas.push(data);
}
}
}
else if (rule == DataTable.SUBMIT.focus) {
datas = []
rows = this.rows();
for (var i = 0, count = rows.length; i < count; i++) {
if (i == this.focusIndex())
datas.push(rows[i].getData())
else
datas.push(rows[i].getEmptyData())
else if (rule == DataTable.SUBMIT.all || rule == DataTable.SUBMIT.allPages) {
datas = []
for(var i = 0; i < pages.length; i++){
var currPage = pages[i]
var data = page2data(currPage,i);
data.rows = [];
for(var i = 0; i < currPage.rows.length; i++){
data.rows.push(currPage.rows[i].getData())
}
datas.push(data)
}
}
}
else if (rule == DataTable.SUBMIT.all) {
datas = this.getData()
}
else if (rule == DataTable.SUBMIT.select) {
datas = this.getSelectedDatas(true)
}
else if (rule == DataTable.SUBMIT.change) {
datas = this.getChangedDatas()
}
else if (rule === DataTable.SUBMIT.empty) {
datas = []
}
if (this.pageCache && datas != null) {
datas = [{index: this.pageIndex(), select: this.getSelectedIndexs(), focus: this.focusIndex(), rows: datas}]
}
if (rule == DataTable.SUBMIT.allSelect) {
datas = []
var totalPages = this.totalPages();
//缓存页数据
for (var i = 0; i < totalPages; i++) {
if (i == this.pageIndex()) {
//当前页数据
datas.push({
index: this.pageIndex(),
select: this.getSelectedIndexs(),
focus: this.focusIndex(),
rows: this.getSelectedDatas()
});
} else {
var page = this.cachedPages[i];
if (page) {
datas.push({
index: i,
select: page.selectedIndices,
focus: page.focus,
rows: page.getSelectDatas()
});
else if (rule == DataTable.SUBMIT.select) {
datas = []
var pageIndex = this.pageIndex();
var currPage = pages[pageIndex];
if(currPage){
var data = page2data(currPage,pageIndex);
data.rows = [];
for(var i = 0, count = currPage.rows.length; i < count; i++){
var row = currPage.rows[i].getData();
if(data.select.indexOf(i) < 0)
row.data = {}
data.rows.push(row);
}
datas.push(data);
}
}
} else if (rule == DataTable.SUBMIT.allPages) {
datas = []
var totalPages = this.totalPages();
//缓存页数据
for (var i = 0; i < totalPages; i++) {
if (i == this.pageIndex()) {
//当前页数据
datas.push({
index: this.pageIndex(),
select: this.getSelectedIndexs(),
focus: this.focusIndex(),
rows: this.getData()
});
} else {
var page = this.cachedPages[i];
if (page) {
datas.push({index: i, select: page.selectedIndices, focus: page.focus, rows: page.getData()});
else if (rule == DataTable.SUBMIT.allSelect) {
datas = []
for(var i = 0; i < pages.length; i++){
var currPage = pages[i]
var data = page2data(currPage,i);
data.rows = [];
for(var j = 0, count = currPage.rows.length; j < count; j++){
var row = currPage.rows[j].getData();
if(data.select.indexOf(j) < 0)
row.data = {}
data.rows.push(row);
}
datas.push(data)
}
}
}
if (this.pageCache) {
else if (rule == DataTable.SUBMIT.change) {
datas = []
for(var i = 0; i < pages.length; i++){
var currPage = pages[i]
var data = page2data(currPage,i);
data.rows = [];
for(var j = 0, count = currPage.rows.length; j < count; j++){
var row = currPage.rows[j].getData();
if(row.status == Row.STATUS.NORMAL){
row.data = {}
}
data.rows.push(row)
}
datas.push(data)
}
}
else if (rule === DataTable.SUBMIT.empty) {
datas = []
}
if(pages.length < 1 || !pages[this.pageIndex()]){
datas = [{ index: this.pageIndex(), select: [], focus: -1, rows: [] }];
}
returnData.pages = datas;
} else {
}else{
if (rule == DataTable.SUBMIT.current) {
datas = []
var currIndex = this.focusIndex()
if (currIndex == -1)
currIndex = this.getSelectedIndex()
rows = this.rows();
for (var i = 0, count = rows.length; i < count; i++) {
if (i == currIndex)
datas.push(rows[i].getData())
else
datas.push(rows[i].getEmptyData())
}
}
else if (rule == DataTable.SUBMIT.focus) {
datas = []
rows = this.rows();
for (var i = 0, count = rows.length; i < count; i++) {
if (i == this.focusIndex())
datas.push(rows[i].getData())
else
datas.push(rows[i].getEmptyData())
}
}
else if (rule == DataTable.SUBMIT.all) {
datas = this.getData()
}
else if (rule == DataTable.SUBMIT.select) {
datas = this.getSelectedDatas(true)
}
else if (rule == DataTable.SUBMIT.change) {
datas = this.getChangedDatas()
}
else if (rule === DataTable.SUBMIT.empty) {
datas = []
}
returnData.rows = datas
returnData.select = this.getSelectedIndexs()
returnData.focus = this.getFocusIndex()
}
returnData.pageSize = this.pageSize()
returnData.pageIndex = this.pageIndex()
returnData.isChanged = this.isChanged()
@@ -92,7 +92,12 @@ import {
setPages,
hasPage,
clearCache,
cacheCurrentPage
cacheCurrentPage,
updatePagesSelect,
updatePageRows,
updatePageSelect,
updatePageFocus,
updatePageAll
} from './page';
import {
@@ -294,6 +299,12 @@ DataTable.prototype.setPages= setPages;
DataTable.prototype.hasPage= hasPage;
DataTable.prototype.clearCache= clearCache;
DataTable.prototype.cacheCurrentPage= cacheCurrentPage;
DataTable.prototype.updatePagesSelect = updatePagesSelect;
DataTable.prototype.updatePageRows = updatePageRows;
DataTable.prototype.updatePageSelect = updatePageSelect;
DataTable.prototype.updatePageFocus = updatePageFocus;
DataTable.prototype.updatePageAll = updatePageAll;
//param
DataTable.prototype.addParam= addParam;
@@ -26,7 +26,9 @@ import {
} from './page-meta';
import {
removeRowByRowId
removeRowByRowId,
updateSelectedIndices,
updateFocusIndex
} from './page-removeRow';
@@ -61,6 +63,8 @@ Page.prototype.setRowMeta= setRowMeta;
//removeRow
Page.prototype.removeRowByRowId= removeRowByRowId;
Page.prototype.updateSelectedIndices= updateSelectedIndices;
Page.prototype.updateFocusIndex= updateFocusIndex;
export {
@@ -39,7 +39,7 @@ const getSelectRows = function () {
const getRowByRowId = function (rowid) {
for (var i = 0, count = this.rows.length; i < count; i++) {
if (this.rows.rowId == rowid)
if (this.rows[i].rowId == rowid)
return this.rows[i]
}
return null
Oops, something went wrong.

0 comments on commit b464a09

Please sign in to comment.