在移动列后查找列的索引
Find Index of Column(s) after it has been Moved
我们正在使用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
}):
相关文章:
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- 在从索引位置返回的字符串中查找空白
- 查找字符串及其索引中的最大数字
- 在 jquery 选项卡中查找 span 标记并返回该索引
- 给定一个带有数字的数组,我如何编写一个递归函数,当 2 个元素加起来为一个目标时,它会在数组中查找索引
- 查找数组中项目包含子字符串的索引
- 按键查找数组中对象的索引
- 查找所选元素的索引
- 通过getElementById查找所选索引是't返回正确的值
- Typescript,NodeJS,ES6-在数组上查找索引
- JavaScript DOM:在容器中查找元素索引
- 正在查找数组中第一个匹配元素的索引
- 为动态生成的表查找表行索引
- 如何查找具有行和列索引的特定单元格的内容
- 使用字符串中的开始和结束单词以及 javascript 中的子字符串索引查找子字符串
- 按行索引查找一行,然后在jquery中查找特定的td
- jQuery
- 索引查找
- 通过索引查找对象并将其替换为其他对象.(反应/回来的)
- 在Angular中基于ng-repeater索引查找数组的项
- 根据表索引查找元素