Combining selected boxes through merging

Looking to create a simple webpage with the following requirements:

  • There should be 10 rows and 3 boxes in each row.
  • If I select 2 or more boxes or drag a box, they should merge together.

For example, if my initial screen looks like this: and then I select two boxes, the result would be:

Check out this Fiddle for reference.

Currently, it only works the first time. I want to save the state so that when I click the close button, it can roll back to previous states.

Any assistance would be greatly appreciated.

Answer №1

Take note that you are utilizing the merge function provided by me.

As I mentioned before, the code is simply meant to demonstrate the general approach to follow.

Don't forget to invoke this function in order to make the new element droppable:

var modifyAsDroppable = function(target) {
    var what = "<div class=\"handle ui-selectee\"><span class=\"ui-icon ui-icon-carat-2-n-s ui-selectee\"></span></div>";
    target.prepend(what);
}

In this version, each task is divided into separate functions, making merge() like this:

var merge = function() {
    /* contains: top left bottom right */
    var properties = calculateSize();
    var $merged = createNewDiv();
    insert($merged, searchWhereInsert(properties));
    modifyAsDroppable($merged);
    setSize($merged, properties);
    removeMerged();
}

If you want it to work not just the first time, you need to ensure the correct implementation of how you populate the selected array.

UPDATE

In this demo, I have resolved the issue of "one-time-working" and now it works consistently. There was a flaw in the creation of the selected array, which has been corrected to:

if(index >= 0) {
  selected.push(index);
}

Making progress, now it's up to you to implement the "undo" functionality on your own.

Update + rollBack

In this version, there is a rollBack feature available, although it may have some bugs (not functioning in certain scenarios).
Make enhancements!

Update + rollBack fixed && working

Just for personal satisfaction, check out HERE where the rollBack operation is now fully operational. (exciting!)

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

The callback information is not being properly identified

I need help with deleting an entire row when the Delete button is clicked using jQuery. Here is my current approach: UPDATE: I have made final adjustments to the click function: $(document).on('click', '.delete-assignment',function () ...

Stopping multiple queries in Node.js can be achieved by using proper error handling

Upon verifying the existence of the name, the program continues to the subsequent query and proceeds with inserting data, which results in an error due to multiple responses being sent. How can I modify it to halt execution after checking if (results.row ...

What is the best method for generating a vertical tab using JavaScript in a paragraph format?

Struggling to create a menu similar to this using bootstrap 4. Despite my efforts, I keep encountering various issues. Is there someone who can help me solve this problem? Remember, I am using bootstrap 4. Here is a demo: <!doctype html> <html ...

Changing TypeScript Enum from String to Number in Angular

Does anyone know how to convert a Typescript enum value to a number for display in a dropdown but passing the numeric value on submit? Here is how the enum is currently set up: I am currently able to output the string key of the object when it is emitted ...

Using a string as a query parameter in Javascript/AngularJS

Hey there! I have a username that I'm passing to retrieve a record from the database. Here's what I have in my controller: $scope.team=function(data) team_factory.getMember.query({}, data.username, function(data){ $scope.team=data; }); And ...

Unselect all checkboxes except for the one that was clicked

In a project, I have 3 checkboxes that are interconnected and when one is clicked or checked, I want the others to be cleared while keeping the clicked checkbox checked. This behavior is similar to radio buttons but I cannot use radio buttons due to client ...

How can I show the post JSON response that was retrieved in ReactJS on the same form component that was used to submit the value?

Thank you for your assistance in advance. I am currently developing a web crawler that will operate in the following steps: The user inputs the seed URL (front-end) The user clicks the submit button (front-end) The seed URL is processed by the backend usi ...

Using Ajax with Angular services

Recently, I decided to explore Angular JS and Async calls as an alternative to $JQuery. However, I encountered a peculiar issue while making ajax calls. To adhere to 'best practices', I shifted my ajax requests to a service and then initiated th ...

Displaying a video in fullscreen on a webpage

Struggling to achieve the desired result, seeking advice from experts. Looking to create a classic example like this: http://codepen.io/anon/pen/rWwaRE But want it embedded within blocks of text and possibly triggered by scrolling over it similar to this ...

Troubleshooting Typescript app compilation problem in a Docker environment

I am encountering a challenge while trying to build my typescript Express app using Docker. Surprisingly, the build works perfectly fine outside of Docker! Below is the content of my Dockerfile: FROM node:14-slim WORKDIR /app COPY package.json ./ COPY yarn ...

Update the content on the webpage to display the SQL data generated by selecting options from various dropdown

My database table is structured like this: Name │ Favorite Color │ Age │ Pet ────────┼────────────────┼───────┼─────── Rupert │ Green │ 21 │ ...

Module not found (Error: Module not found for './models/campground')

Here is the code snippet I am working with: var express = require("express"), app = express(), bodyParser = require("body-parser"), mongoose = require("mongoose"), Campground = require("./models/campground"), Comment = require("./mode ...

The issue of `for` loop and `forEach` loop not functioning in EJS

I'm currently developing a To-Do App and facing difficulty with the POST request functionality. I've attempted using both a for loop and a forEach loop but none seem to be working. <% todos.forEach(item => { %> <li><%= item ...

Is there a quicker method to update the state of an array of objects?

Here is my React state example: const [questionList, setQuestionList] = useState([ { _type: "radio", answer: "", point: "", question: "", options: ["A", "B"], ...

modify the calculation in real-time by updating the text box with ajax

In this scenario, the input and display page are dynamically generated from PHP code. enter code here <div>Aeroplane</div> <input id="Item556" type='number' onchange="funCal('Item556', this.value, 'Aeroplane&apos ...

Margins and borders with a negative impact

I have a collection of images that I am trying to stack using negative margins. However, because stacking them without defined boundaries can lead to visual confusion, I thought about adding borders around each image. Surprisingly, while the images are sta ...

Loading screen displayed while transitioning between routes within Angular

I have been struggling to implement a loading spinner in my project. How can I display a loading screen when changing routes in Angular? Here is the HTML code snippet: <div *ngIf="showLoadingIndicator" class="spinner"></div> ...

How to make an input blur in Angular 2 when a button is clicked?

Is there a way to blur an input field by pressing the return button on a mobile native keyboard? Here is an example: <input type="text" #search> this.search.blur() //-- unfocus and hide keyboard ...

Discover how to use your own search engine suggestions within the search bar of Google Chrome

I recently created a search engine on my website, and everything seems to be working perfectly when I visit the search page. However, I wanted to streamline the process by searching directly from the search box in Chrome. After adjusting the settings to ...

Whenever I attempt to include state in a React class that is declared as a constant, I consistently encounter the error message: "TypeError:

Apologies for the redundancy, but as a newcomer to React, I previously inquired about a similar issue and have since modified my code. My current challenge involves trying to access a state value within a const React class. Below is the excerpt from my Ar ...