3

Following is my JSON Data validated using JSONLint:

[{
"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
}
}]

What I am looking for is to convert the value for the keys AddItem and RemoveItem to HTML buttons.

The above JSON will be used to render nodes in a jsTree plugin.

jsFiddle link where I will be using this json data. Basically, I want to replace my + and - sign with a button for the same

Does anyone has a work-around for this ?

Edit 1: I have read at a few places that adding HTML elements directly in your JSON data is not advisable as it can be used at a number of places in your code and each time it may have a different HTML for it. If someone has a better way to do it, that would be great.

9
  • This might be helpful, though it uses right clicks instead of buttons. I think that if you could somehow add a class attribute or something, you could use jQuery's onClick event. Commented Jun 27, 2016 at 16:51
  • What do you want to add when the add button is clicked just an item or a folder, and the same for the remove button? Commented Jun 27, 2016 at 16:59
  • 1
    You can try add the code html in your json add class for get event on click jsfiddle.net/x9wd6k3x/14 Commented Jun 27, 2016 at 17:03
  • 1
    You can add listenner to buttons for example: $(document).on("click", ".remove", function(){ alert('remove clicked');}); and call function to add or remove jsfiddle.net/x9wd6k3x/15 Commented Jun 27, 2016 at 17:24
  • 1
    Its working for you?? You want add or remove from buttons?? jsfiddle.net/x9wd6k3x/16 Commented Jun 27, 2016 at 17:43

1 Answer 1

2

I have never used the jsTree plugin and there is probably a better way to solve your problem, but this seems to do what i think your looking for. Just add this to the end of your code.

$(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');
});
Sign up to request clarification or add additional context in comments.

6 Comments

Your solution is working absolutely fine and also without having to include HTML in JSON. However, I would like to know the better solution that you are talking about as well
I just assumed that maybe there is a jsTree function that would help to do what you are asking. Not that there is anything wrong with using my solution.
Ok. Thanks for this amazing piece of work. Can I replace buttons with icons and then have a click event for those icons ?
Yeah or you could put the icon inside the button $(this).html('<button class="removeBtn">icon</button>')
use css to change the background color of the button. stackoverflow.com/questions/5628507/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.