I wanted to have a checkbox tree form and I used dynatree jquery tree to do that. Then I found out dynatree doesn't use real checkboxes so I placed html checkbox inside the tree.
Its working fine until I realise when the node is expanded/collapsed it remvoe the checked (the tick) value Is there a way in javascript I can write a function to prevent this happening?
Thanks so much. Here is my code
$(function(){
$("#tree").dynatree({
// using default options
checkbox: false,
selectMode: 3,
noLink: true,
});
<div id="tree">
<ul>
<li id="key1" title="Look, a tool tip!">
<input type="checkbox" id="chb-key1" name="selected_items" value="Item 1" class="" />Item 1</li>
<li id="key2">
<input type="checkbox" id="chb-key2" name="selected_items" value="Item 2" />Item 2</li>
<li id="key3">
<input type="checkbox" id="chb-key3" name="selected_items" value="Item 3" />Folder with some children
<ul>
<li id="key31">
<input type="checkbox" id="chb-key31" name="selected_items" value="Item 3.1" />Sub-item 3.1</li>
<li id="key32" class="selected">
<input type="checkbox" id="chb-key32" name="selected_items" value="Item 3.2" />Sub-item 3.2
<ul>
<li id="key321" class="selected">
<input type="checkbox" id="chb-key321" name="selected_items" value="Item 3.2.1" />Sub-item 3.2.1</li>
<li id="key322" class="selected">
<input type="checkbox" id="chb-key322" name="selected_items" value="Item 3.2.2" />Sub-item 3.2.2</li>
</ul>
</li>
</ul>
</li>
"My suggestion is to add an example to embed it in the form without using AJAX post but instead the "normal" way of submitting forms (via POST). This should mean converting the "tree.serializeArray()" to hidden input fields and then submitting the form."
This is exactly what I wanted to achieve. Do you have any example how can I convert into
"tree.serializeArray()" to hidden input fields ?
Thanks so much!
// add an call back function on submit form event to include selected folders in form data
$("form").submit(function() {
// append Dynatree selected 'checkboxes'to form:
var folder_list_tree = $("#yourtree").dynatree("getTree") ;
var selected_folders = folder_list_tree.getSelectedNodes();
var this_form = this;
$.each(selected_folders,function(){
$('<input>').attr({
type: 'hidden',
name: 'folder_selection',
value: this.data.key
}).appendTo(this_form);
});
return true;
});
folder_selection fields will act as an <input type=checkbox name =folder_selection> tag, with as many entries as folders are selected