【问题标题】:Adding HTML elements in the JSON data在 JSON 数据中添加 HTML 元素
【发布时间】:2016-10-29 18:17:23
【问题描述】:

以下是我使用JSONLint 验证的 JSON 数据:

[{
"text": "Products",
"data": {},
"children": [{
    "text": "Fruit",
    "data": {
        "price": "",
        "quantity": "",
        "AddItem": "+",
        "RemoveItem": "&#215"
    },
    "children": [{
        "text": "Apple",
        "data": {
            "price": 0.1,
            "quantity": 20,
            "AddItem": "+",
            "RemoveItem": "&#215"
        }
    }, {
        "text": "Strawberry",
        "data": {
            "price": 0.15,
            "quantity": 32,
            "AddItem": "+",
            "RemoveItem": "& #215"
        }
    }],
    "state": {
        "opened": false
    }
}, {
    "text": "Vegetables",
    "data": {
        "price": "",
        "quantity": "",
        "AddItem": "&# 43;",
        "RemoveItem": "&#215"
    },
    "children": [{
        "text": "Aubergine",
        "data": {
            "price": 0.5,
            "quantity": 8,
            "AddItem": "+",
            "RemoveItem": "&#215"
        }
    }, {
        "text": "Cauliflower",
        "data": {
            "price": 0.45,
            "quantity": 18,
            "AddItem": "+",
            "RemoveItem": "&#215"
        }
    }, {
        "text": "Potato",
        "data": {
            "price": 0.2,
            "quantity": 38,
            "AddItem": "+",
            "RemoveItem": "&#215"
        }
    }]
}],
"state": {
    "opened": false
}
}]

我正在寻找的是将键 AddItemRemoveItem 的值转换为 HTML 按钮

上面的 JSON 将用于渲染 jsTree plugin 中的节点。

jsFiddle 链接我将在其中使用此 json 数据。基本上,我想用相同的按钮替换我的 +- 符号

有没有人可以解决这个问题?

编辑 1: 我在一些地方读到过,不建议直接在 JSON 数据中添加 HTML 元素,因为它可以在代码中的许多地方使用,而且每次都可能有一个不同的HTML。如果有人有更好的方法来做到这一点,那就太好了。

【问题讨论】:

标签: jquery html json treetable jstree-dnd


【解决方案1】:

我从未使用过 jsTree 插件,可能有更好的方法来解决您的问题,但这似乎可以满足您的需求。只需将其添加到代码的末尾即可。

$(document).on('click','.jstree-table-wrapper', function() {//insted of document i would used jtrees containing div
    $.each($('.jstree-table-cell span'),function(){
        if($(this).html()=='+'){      
            $(this).html('<button class="addBtn">+</button>')
        }else if($(this).html()=='×'){      
            $(this).html('<button class="removeBtn">x</button>')
        }
    });
});

$(document).on('click','.addBtn',function(){//insted of document i would used jtrees containing div
     var id = 'j1_'+$(this).parent().parent().attr('id').split('_')[2];
     demo_create(id);
     $('.jstree-table-wrapper').trigger('click');
});

$(document).on('click','.removeBtn',function(){//insted of document i would used jtrees containing div
     var id = 'j1_'+$(this).parent().parent().attr('id').split('_')[2];
     demo_delete(id);//you might need to work on your delete function
     $('.jstree-table-wrapper').trigger('click');
});

【讨论】:

  • 您的解决方案工作得非常好,而且无需在 JSON 中包含 HTML。但是,我也想知道您所说的更好的解决方案
  • 我只是假设可能有一个 jsTree 函数可以帮助您完成您的要求。并不是说使用我的解决方案有什么问题。
  • 好的。感谢您的出色工作。我可以用图标替换按钮,然后为这些图标设置点击事件吗?
  • 是的,或者您可以将图标放在按钮内$(this).html('&lt;button class="removeBtn"&gt;icon&lt;/button&gt;')
  • 使用css改变按钮的背景颜色。 stackoverflow.com/questions/5628507/…
猜你喜欢
  • 2023-03-20
  • 2018-10-22
  • 2018-08-27
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多