This question is locked. New answers and comments are not allowed.
I just recently began using the TreeView MVC component and have been pleased with it so far - however I have ran into a few issues regarding serializing the data, without performing a submit and was wondering if this is possible?
Treeview Code:
I have the Tree set to automatically have all nodes initially checked - and changed the logic such that if an Instance was unchecked - it would uncheck not only the parent Series, but also the "grandparent" Study as well.
Javascript code: (To handle automatic selection - unchecking of "grandparent" nodes)
Now - all of this functionality seems to work just as intended. However - my issue is that when I attempt to perform a submit (in another section of the page) I want to serialize the values of the tree as such:
(sendStudyForm is an area above the TreeView that is encapsulated in a Form - it contains fields that relate to where all of the selected instances will be sent)
In my controller - the post hits the proper action, however instances is always Count=0 (for instances) and I am wondering if it is an issue with the $("#treeViewItems").serialize() to not POST the correct List<TreeViewItem> to my Action:
Controller :
Treeview Code:
<form id="treeViewItems"> <%= Html.Telerik().TreeView() .Name("TreeView") .ShowCheckBox(true) .BindTo(Model, mappings => { mappings.For<Study>(binding => binding .ItemDataBound((item, study) => { item.Encoded = false; item.Text = "<b>" + study.StudyDescription + "</b>"; }) .Children(study => study.Series)); mappings.For<Series>(binding => binding .ItemDataBound((item, series) => { item.Text = "Series " + series.SeriesNumber.ToString(); }) .Children(series => series.Instances)); mappings.For<Instance>(binding => binding .ItemDataBound((item, instance) => { item.Text = "Instance " + instance.InstanceNumber.ToString(); item.Value = instance.InstanceGuid.ToString(); }) .Children(series => null)); }) %></form>I have the Tree set to automatically have all nodes initially checked - and changed the logic such that if an Instance was unchecked - it would uncheck not only the parent Series, but also the "grandparent" Study as well.
Javascript code: (To handle automatic selection - unchecking of "grandparent" nodes)
<script type="text/javascript">
$('#TreeView').find("li") .find('> div > .t-checkbox :checkbox') .bind('click', function (e) { var isChecked = $(e.target).is(':checked'); var treeView = $($(e.target).closest('.t-treeview')).data('tTreeView'); var item = $(e.target).closest('.t-item'); var checkboxes = item.find('.t-checkbox :checkbox'); $.each(checkboxes, function (index, checkbox) { $(checkbox).attr('checked', isChecked ? true : false); treeView.checkboxClick(e, checkbox); }); var siblings = item.parent().find('> li .t-checkbox'); var siblingsLength = siblings.length; var checkedLength = siblings.find(':checked').length; if (siblingsLength == checkedLength)
{ var parentCheckBox = item.parent().closest('.t-item').find('> div .t-checkbox :checkbox'); var grandparentCheckBox = item.parent().parent().parent().closest('.t-item').find('> div .t-checkbox :checkbox'); parentCheckBox.attr('checked', true) grandparentCheckBox.attr('checked', true) treeView.checkboxClick(e, parentCheckBox) treeView.checkboxClick(e, grandparentCheckBox) } else { var parentCheckBox = item.parent().closest('.t-item').find('> div .t-checkbox :checkbox'); var grandparentCheckBox = item.parent().parent().parent().closest('.t-item').find('> div .t-checkbox :checkbox'); parentCheckBox.attr('checked', false) grandparentCheckBox.attr('checked', false) treeView.checkboxClick(e, parentCheckBox) treeView.checkboxClick(e, grandparentCheckBox) } }); })
</script>Now - all of this functionality seems to work just as intended. However - my issue is that when I attempt to perform a submit (in another section of the page) I want to serialize the values of the tree as such:
(sendStudyForm is an area above the TreeView that is encapsulated in a Form - it contains fields that relate to where all of the selected instances will be sent)
$("#sendStudyForm").submit(function () { var destination = $("#destination").val(); $.post("/Images/SendStudies", { instances : $("#treeViewItems").serialize(), 'destinationGuid': destination }); return false;});In my controller - the post hits the proper action, however instances is always Count=0 (for instances) and I am wondering if it is an issue with the $("#treeViewItems").serialize() to not POST the correct List<TreeViewItem> to my Action:
Controller :
[HttpPost] public EmptyResult SendStudies(List<TreeViewItem> instances, string destinationGuid) { //Test return new EmptyResult(); }