【问题标题】:How to access node data attribute inside jstree sort function?如何访问jstree排序函数中的节点数据属性?
【发布时间】:2019-05-04 14:49:06
【问题描述】:

简介

我正在使用jquery树插件库jstree显示树。

期待

因为我正在显示文件 - 它们有扩展名 - 所以按名称排序不是很有帮助。可以想象按类型排序是最好的。

为此,我添加了 item-sort-value 的数据属性 extension + fileName + uuid

错误

我想按此字段data-item-sort-value 对树进行排序,但在树中显示标准 文件名。但是我无法在排序函数中获得它的值

我也试过了:

  • a1.getAttribute('data-item-sort-value')...得到TypeError: a1.getAttribute is not a function
  • a1.a_attr('data-item-sort-value')...得到TypeError: a1.getAttribute is not a function
  • a1.data('item-sort-value')...得到TypeError: a1.data is not a function
  • a1.node.data('item-sort-value')...得到TypeError: a1.data is not a function

我的代码

'sort': function(a, b)
{
    let a1 = this.get_node(a);
    let b1 = this.get_node(b);

    if (a1.node.attr('item-sort-value') === b1.node.attr('data-item-sort-value'))
    {
        return (a1.node.attr('data-item-sort-value') > b1.node.attr('data-item-sort-value')) ? 1 : -1;
    }
    else
    {
        return (a1.icon > b1.icon) ? 1 : -1;
    }
}

控制台输出

【问题讨论】:

    标签: javascript jquery jstree


    【解决方案1】:

    这是使用数据属性值的工作排序函数。

    'sort' : function(a, b)
    {
        let a1 = this.get_node(a);
        let b1 = this.get_node(b);
        let isv_a1 = a1.a_attr['data-item-sort-value'];
        let isv_b1 = b1.a_attr['data-item-sort-value'];
    
        return (isv_a1 > isv_b1) ? 1 : -1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多