在移动列后查找列的索引

Find Index of Column(s) after it has been Moved

本文关键字:索引 查找 移动      更新时间:2023-09-26

我们正在使用DHTMLX Grid。需要一些帮助,请。

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

我们已经硬编码了列的索引,以设置并在其他地方获取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");

并将这些变量放入设置/获取 cookie 中。我在想是否有更好的方法。

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

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

我认为你有最好的答案。 循环执行,这样更容易:

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;
}
您可以使用

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