2013-09-26 36 views
0

我們正在使用DHTMLX網格。需要一些幫助,請。查找已移動列的索引

我有一個表,每列(過濾器/下拉列表)分配一個ID例如。 fac,date,sel,loc,tag ...等

我們已經硬編碼了列的索引來設置Cookie,並在別處獲取cookie。

function doInitGrid(){ 

mygrid.setColumnIds("fac,date,sel,loc,tag"); //set ids 
mygrid.attachEvent("onFilterStart",function(ind,data) 
          { 
setCookie("Tray_fac_filter",mygrid.getFilterElement(0).value,365); //column index 0 
setCookie("Tray_loc_filter",mygrid.getFilterElement(3).value,365);//column index 3 
setCookie("Tray_tag_filter",mygrid.getFilterElement(4).value,365); //column index 4 

mygrid.getFilterElement(0).value = getCookie("Tray_fac_filter") 
mygrid.getFilterElement(3).value = getCookie("Tray_dep_filter") 
mygrid.getFilterElement(4).value = getCookie("Tray_prg_filter") 
}); 
} 

但是當列移動時,問題就出現作爲的列變化指數但它在setCookie方法設置/ getCoookie

DHTMLX允許使用獲得的id指數 -

var colInd = grid.getColIndexById(id); 
eg: var colInd = grid.getColIndexById(date); // outputs 1. 
After moving the date column to the end -- fac, sel, loc, tag, date // it will output 4. 

然而,我們大約有14列可移動/重排,我可以用

var colInd = grid.getColIndexById(id); 15 times 

var facInd = grid.getColIndexById("fac"); 
var dateInd = grid.getColIndexById("date"); 
var selInd = grid.getColIndexById("sel"); 
var locInd = grid.getColIndexById("loc"; 
var tagInd = grid.getColIndexById("tag"); 

並將這些變量放在set/get cookie中。我在想如果有更好的方法。

爲了更好地理解代碼,我將代碼的最小化版本放在小提琴中。

http://jsfiddle.net/19eggs/s5myW/2/

回答

0

你已經得到了最好的答案,我認爲。做一個循環,它更容易:

var cookie_prefix = "Fray_filter_"; 
var cookie_dur = 365; 
var num_cols = dhx_grid.getColumnCount(); 

// filter vals to cookies 
for (var col_idx=0; col_idx<num_cols; col_idx++) { 
    var filter = mygrid.getFilterElement(col_idx) 
    if (filter) { // not all columns may have a filter 
    var col_id = dhx_grid.getColumnId(col_idx); 
    var cookie_name = cookie_prefix+col_id; 
    setCookie(cookie_name, filter.value, cookie_dur); 
    } 
} 

// cookies to filter vals 
for (var col_idx=0; col_idx<num_cols; col_idx++) { 
    var col_id = dhx_grid.getColumnId(col_idx); 
    var filter_val = getCookie(cookie_prefix+col_id); 
    var filter = mygrid.getFilterElement(col_idx) 
    filter.value = filter_val; 
} 
0

您可以使用dhtmlxgrid本地事件來分配正確的id每次移動一列。 該事件被稱爲onAfterCMove,你可以在這裏查看文檔。 onAfterCMove Event

你會做這樣的事情:

mygrid.attachEvent('onAfterCMove',function(cInd,posInd){ 
//Your processing here to change the cookies; where cInd is the index of the column moved 
//and posInd, is the position where it Was moved 
}):