Javascript 移动列后查找列的索引

Javascript 移动列后查找列的索引,javascript,dhtmlx,Javascript,Dhtmlx,我们使用的是DHTMLX网格。需要帮忙吗 我有一个表,每个列(有过滤器/下拉列表)都分配了一个id,例如fac、日期、sel、loc、tag。。。等 我们对列的索引进行了硬编码,以便在其他地方设置和获取cookie function doInitGrid(){ mygrid.setColumnIds("fac,date,sel,loc,tag"); //set ids mygrid.attachEvent("onFilterStart",function(ind,data)

我们使用的是DHTMLX网格。需要帮忙吗

我有一个表,每个列(有过滤器/下拉列表)都分配了一个id,例如fac、日期、sel、loc、tag。。。等

我们对列的索引进行了硬编码,以便在其他地方设置和获取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允许使用--

但是,我们有大约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中。我在想是否有更好的办法

为了更好地理解代码,我将代码的简化版本放在了小提琴上


我认为你得到了最好的答案。以循环方式进行操作,会更容易:

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;
}
var cookie\u prefix=“Fray\u过滤器”;
var cookie_dur=365;
var num_cols=dhx_grid.getColumnCount();
//将VAL筛选到Cookie

对于(var col_idx=0;col_idx,您可以在每次移动列时使用dhtmlxgrid本机事件来分配正确的id。 该事件称为onAfterCMove,您可以在此处查看文档

您可以执行以下操作:

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
}):
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
}):