Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, while the right tree starts empty. There are two buttons - Include and Exclude. When a child from the left tree is selected and included, it should move to the right tree. If the user decides to move it back, it must return to the same position under the same parent. Additionally, each child can only exist once to avoid duplicates when moving them to new parents.

For this task, I have the flexibility to use HTML, CSS, JavaScript, and jQuery.

Below is my current approach:

CSS

#menutree li {
       list-style: none; 
         }

       li .menu_label + input[type=checkbox] {
          opacity: 0; 
            
         } 

        li .menu_label {
          cursor: pointer;   
     }                       

      li .menu_label + input[type=checkbox] + ol > li
         {
            display: none;    
         }

  li .menu_label + input[type=checkbox]:checked + ol > li
     {
       display: block;     
     }



.selected {
     background-color:#efefef;
}

jQuery

$('#move_left').click(function() {
$('.list1').append($('.list2 .selected').removeClass('selected'));
});

$('#move_right').click(function() {
$('.list2').append($('.list1 .selected').removeClass('selected'));
});

$('body').on('click', 'li', function() {
$(this).toggleClass('selected');
});

HTML

<body>
    <ol id="menutree">
<li>

    <label class="menu_label" for="c1">Menu Gen14 Am0a1</label>
    <input type="checkbox" id="c1" />                      
<ol>
    <li>
    <ul class="list1">
        <li class="page">Page Ap1a1</li>
        <li> Page Ap1a2</li>
    </ul>


</ol>
</ol>
</body>
 <input type='button' value='<<' id='move_left'/>
    <input type='button' value='>>' id='move_right'/>      

Answer №1

An example that may not account for children ordering, but the logic presented should remain intact.

See Demo

<div class="half">
    <ol id="one">
        <li id="alaska">
            Alaska
            <ul>
                <li data-id="alaska"><input type="checkbox"> Children 1</li>
                <li data-id="alaska"><input type="checkbox"> Children 2</li>
                <li data-id="alaska"><input type="checkbox"> Children 3</li>
            </ul>
        </li>
        <li id="washington">
            Washington
            <ul>
                <li data-id="washington"><input type="checkbox"> Children 1</li>
                <li data-id="washington"><input type="checkbox"> Children 2</li>
                <li data-id="washington"><input type="checkbox"> Children 3</li>
            </ul>
        </li>
        <li id="texas">
            Texas
            <ul>
                <li data-id="texas"><input type="checkbox"> Children 1</li>
                <li data-id="texas"><input type="checkbox"> Children 2</li>
                <li data-id="texas"><input type="checkbox"> Children 3</li>
            </ul>
        </li>
    </ol>
    <button type="button" class="include">Include</button>
</div>
<div class="half">
    <ol id="two"></ol>
    <button type="button" class="exclude">Exclude</button>
</div>

$(function(){
    $('.include').click(function(e){
        var $checks = $("input:checked");
        $.each($checks, function(k,v){
            var tempid = $(this).parent().data('id');
            if( !$('#two').find('[data-id="'+tempid+'"]').length ){
                var element = $(this).parent().detach();
                $('#two').append(element);
            }
        });
    });

    $('.exclude').click(function(e){
        var $checks = $("input:checked");
        $.each($checks, function(k,v){
            var tempid = $(this).parent().data('id');
            var element = $(this).parent().detach();
            $('#one').find('#'+tempid+' ul').append(element);
        });        
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing cookies to track the read status of articles - markers for reference

Currently, I am in the process of developing a website and am interested in implementing a feature that allows users to track which articles they have read. I am considering adding a small circle next to each article heading to signify whether it has been ...

What is the reason for requiring shaders to reside in an HTML file for a WebGL program?

In a recent forum thread, an individual posed the question about how to eliminate shaders from html. The discussion revolved around finding alternatives to embedding shaders in webGL code: WebGL - is there an alternative to embedding shaders in HTML? The ...

Access-Control-Allow-Origin does not permit AngularJS Origin http://localhost:8080

I'm working on a web application using AngularJS. It's an admin interface that depends on a json-rpc API hosted on a different domain. When testing in my local environment, I encountered the error "Origin http://localhost:8080 is not allowed by ...

Unintended redirects are occurring with AJAX requests when using the send() method

After loading the web page, I utilize AJAX to populate a list asynchronously. However, when the AJAX request is sent to retrieve data for the list box, instead of receiving JSON data as expected, an unintended web page containing the list box is returned. ...

Error Message: A key is being provided to the classes property that is not implemented in the current context

Trying to customize Material-UI styles with makeStyles() as outlined in the documentation but encountering a warning when passing a classname in the parent component that is not specified in useStyles. The warning message reads: Warning: Material-UI: th ...

Utilizing a variable in a for loop with Django

I am a beginner in the world of Python and Django framework and I am encountering a small hurdle when trying to assign a variable to my for loop. Within my html file, there are buttons and a list (generated through a loop). For example: Buttons <li&g ...

Transform the JavaScript object received from an AJAX request into HTML code

Here is an example of what the result (data) looks like: <tr> <td> Something... </td> </tr> <div id="paging">1, 2, 3... </div> This is using ajax to retrieve data. ... dataType: "html", success: function( ...

Tips for hiding specific x-blade attributes from displaying in HTML

Hey everyone! Right now, I'm diving into a Laravel project, working on creating an x-blade component that dynamically renders HTML based on the attributes passed to it. However, I want to ensure that the attributes themselves do not show up in the ren ...

Altering the color upon repetition and utilizing JQuery coordinates for the dynamic movement of elements

I've been working on recreating some JQuery tutorials by myself, but I've hit a roadblock. My goal is to create an object that moves from one position to another while changing color when it repeats. I attempted to achieve this by using an array ...

Is the promises functionality respected by the Nextjs API?

Greetings, I hope all is well with you. I am currently learning NEXTJS and working with its API, but I have encountered a problem. When I click too quickly, the promises seem to get stuck or encounter issues. You can see the tests in action in this brief 3 ...

Transforming the response.render method in Express to be a promise-enabled

I have a task at hand where I need to develop a render method that constructs HTML blocks into a string. Initially, it appears to be working fine, but I ended up using the infamous "new Promise" and now I'm not sure if my approach is correct. Here&apo ...

Framework7 disables the jQuery.appear plugin

I am utilizing Framework7 and aiming to incorporate jQuery.appear for executing a script once an element becomes visible to the user. The implementation is successful when using this code in a basic HTML setup: <!DOCTYPE html> <html> < ...

AngularJS 1 - CSS glitch occurs when navigating between tabs

Upon loading my homepage, it appears as follows: https://i.sstatic.net/CTlj1.png Initially, certain rows were filled with a blue background color using ng-class, specifically "row-answered-call" as shown below: <tr ng-repeat="item in list" ng-class=" ...

Transitioning to Firebase Authentication

I made the decision to transition away from firebase authentication. To accomplish this, I exported all firebase users along with their email addresses, hashed passwords, salt keys, and other necessary information. Next, I transferred them to a database a ...

Why won't my Java servlet code execute?

included the directory structure To facilitate adding two numbers and displaying the result upon clicking a submit button, I developed a straightforward Java servlet. This servlet retrieves the necessary information when called. The development environmen ...

"Create a new row in the list by selecting an option from the drop-down

I'm experimenting with the following scenario. There is a function that reveals a hidden list based on a dropdown selection. To see it in action, please click here. What I am aiming to achieve is for Option1 to display the content of #List-Option1 ...

Issue with Nodemailer OAuth2 2LO authentication when deployed on Heroku

const { EMAIL_FROM, EMAILS_TO, USER, GMAIL_CLIENT_ID, GMAIL_PRIVATE_KEY } = process.env; let transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 465, secure: true, auth: { type: &a ...

Unable to see Primereact styles reflected on components

After some investigation, I've pinpointed the issue to be related to preflight from tailwind. Upon reviewing the documentation, I came across this helpful information: CSS Layer It seems that using Tailwind CSS with styled or unstyled modes of PrimeR ...

Refresh needed for Material UI styles to load correctly in Next JS

UPDATE: Reproducible issue https://github.com/ganavol409/next-material-ui-classes-bug Issue seems to be related to Higher Order Components and importing useStyles from Material UI Implemented Solution: https://github.com/mui-org/material-ui/blob/master/ ...

Create an input box with a designated class

When I click the button in my HTML and JavaScript code below, I am able to dynamically add a text field and radio button. This functionality is working properly. However, I also want the newly generated text field to have the same font size and appearance ...