Is there a way to dynamically resize or adjust the timetable blocks based on the data-column value when dragging and dropping the job box using jQuery?

Once the job box is dropped onto the timetable, I need it to cover specific timetable boxes based on the data-column value of the job box. For instance, if job-A has a data-column attribute set to 4, then it should cover 4 timetable boxes, indicating that this job requires 4 hours to complete. Appreciate your help with this. https://i.sstatic.net/pfS67.png

$(function() {
  $('.job').draggable({
    revert: true
  });

  $('.container').droppable({
    hoverClass: 'active',
    drop: function(e, ui) {

      $(this).html(ui.draggable.remove().html());
      $(this).droppable('destroy');
      $(this).addClass("dropped");
    },
    over: function(e, ui) {
      $(this).addClass("dropped");
    },
    out: function(e, ui) {
      $(this).removeClass("dropped");
    }
  });
});
.job {
  width: 50px;
  height: 15px;
  padding: 2px;
  margin: 0px 5px 10px 0;
  border: 1px solid red;
  background-color: #B9CD6D;
}

.dropJob {
  display: grid;
  grid-template-columns: 12vh repeat(9, 1fr);
}

.dropJob div {
  border: 2px solid #1974a8;
  border-right: none;
  border-bottom: none;
  padding: 3px 4px;
  background: #a5d5ff;
  line-height: 25px;
}

.dropJob div:nth-of-type(10n) {
  border-right: 2px solid #1974a8;
}

.dropJob div:nth-last-child(-n + 10) {
  border-bottom: 2px solid #1974a8;
}

.dropJob div:nth-child(10n + 1) {
  background: #32a4e5;
  font-size: 12px;
  text-shadow: 1px 1px 2px #103143;
  color: #e3f5ff;
}

.dropped {
  background: green !important;
}

.timing {
  display: grid;
  grid-template-columns: 12vh repeat(9, 1fr);
}

.timing div {
  text-align: center;
  align-items: center;
  text-shadow: 1px 1px 2px #103143;
  color: #e3f5ff;
  font-size: 15px;
  background: #1974a8;
}

.scheduleContain {
  width: 100%;
  margin: 0 auto;
}
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<div class="job" data-column="4">Job-A</div>
<div class="job" data-column="1">Job-B</div>
<div class="job" data-column="2">Job-C</div>
<div class="job" data-column="7">Job-D</div>

<div class="scheduleContain">
  <div class="timing">
    <div>Name</div>
    <div>9am</div>
    <div>10am</div>
    <div>11am</div>
    <div>12am</div>
    <div>1pm</div>
    <div>2pm</div>
    <div>3pm</div>
    <div>4pm</div>
    <div>5pm</div>
  </div>

  <div class="dropJob">
    <div>John Smith</div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
  </div>
</div>

Answer №1

You might want to consider attempting it in this manner:

$(function() {
  $('.job').draggable({
    revert: true
  });

  $('.container').droppable({
    hoverClass: 'active',
    drop: function(e, ui) {

      const job = ui.draggable
            let columnSize = job.data('column')
            let tempContainer = $(this);
            let randomColor = "#" + Math.floor(Math.random()*16777215).toString(16) + " !important";
            
            for (let i = 1; i <= columnSize; i++) {
                $(tempContainer).addClass('dropped')

                $(tempContainer).css('background-color', randomColor)
                if (i > 1 && i <= columnSize) {
                    $(tempContainer).addClass('dropped-child')
                }
                $(tempContainer).data('job-id', job.text())
                tempContainer = tempContainer.next();
            }
            $(this).html(ui.draggable.remove().html());
            $(this).droppable('destroy');
            $(this).addClass("dropped");
    },
    over: function(e, ui) {
      $(this).addClass("dropped");
    },
    out: function(e, ui) {
      $(this).removeClass("dropped");
    }
  });
});
.job {
  width: 50px;
  height: 15px;
  padding: 2px;
  margin: 0px 5px 10px 0;
  border: 1px solid red;
  background-color: #B9CD6D;
}

.dropJob {
  display: grid;
  grid-template-columns: 12vh repeat(9, 1fr);
}

.dropJob div {
  border: 2px solid #1974a8;
  border-right: none;
  border-bottom: none;
  padding: 3px 4px;
  background: #a5d5ff;
  line-height: 25px;
}

.dropJob div:nth-of-type(10n) {
  border-right: 2px solid #1974a8;
}

.dropJob div:nth-last-child(-n + 10) {
  border-bottom: 2px solid #1974a8;
}

.dropJob div:nth-child(10n + 1) {
  background: #32a4e5;
  font-size: 12px;
  text-shadow: 1px 1px 2px #103143;
  color: #e3f5ff;
}

.dropped {
  background: green;
}

.timing {
  display: grid;
  grid-template-columns: 12vh repeat(9, 1fr);
}

.timing div {
  text-align: center;
  align-items: center;
  text-shadow: 1px 1px 2px #103143;
  color: #e3f5ff;
  font-size: 15px;
  background: #1974a8;
}

.scheduleContain {
  width: 100%;
  margin: 0 auto;
}

.dropped-child {
    border-left: none !important;
}
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<div class="job" data-column="4">Task-A</div>
<div class="job" data-column="1">Task-B</div>
<div class="job" data-column="2">Task-C</div>
<div class="job" data-column="7">Task-D</div>

<div class="scheduleContain">
  <div class="timing">
    <div>Name</div>
    <div>9am</div>
    <div>10am</div>
    <div>11am</div>
    <div>12am</div>
    <div>1pm</div>
    <div>2pm</div>
    <div>3pm</div>
    <div>4pm</div>
    <div>5pm</div>
  </div>

  <div class="dropJob">
    <div>John Smith</div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
  </div>
</div>

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

What is the proper way to incorporate a ref within a class component?

I am encountering an issue with my class component. I'm wondering if there is a comparable feature to useRef() in class components? Despite several attempts at researching, I have yet to find a solution. ...

Combining Objects in an Array using JavaScript

Here is an array example: let array = [ { yearBirth : 1995, name : 'daniel', }, { yearBirth : 1995, name : 'avi', }, { yearBirth : 1993, name : 'john', }, { yearBirth : 1993, n ...

"Adjust the orientation of a video and ensure it properly fills the screen with CSS styling

Below is the CSS style being used: video { position: absolute; transform: rotate(90deg); width: 100%; height: 100%; z-index: 4; visibility: visible; } This code snippet demonstrates a video element: <video id="myVideo" ...

The essence of ReactJS generics

In my application, I developed a custom hook to handle input. The custom hook utilizes generic types to define the return types. Below is the code snippet of my implementation: interface IUseInput<T, U> { (): [T, (e: ChangeEvent<HTMLInputElem ...

Using VueJS to apply filters to an object causes a decrease in my application's performance

Recently, I've been working on implementing a filter for a search bar. However, I've noticed that whenever I input something into the search bar, there is a noticeable delay as it loads the entries. I'm not sure if this issue is related to ...

Launch a new window for a div when a button is clicked

Hey everyone, I need some help with opening a div in a new window. Currently, I am using window.open and it is working fine. Here is the code snippet: <div id="pass">pass this to the new window</div> <a href="#" onclick="openWin()">clic ...

Add an image to a container

I am facing an issue with my code that fetches photos from a flickr feed using JSON and appends them to a div inside another div. Currently, all the images are being appended into one div, but I want each image to be appended to a separate div. $.getJSON( ...

Enhance PHP URL rewrite by incorporating multiple parameters

Hello, I am currently attempting to implement URL rewriting using PHP with the following code: .htaccess file <IfModule mod_rewrite.c> RewriteEngine On #prevent access to (includes) folder RewriteCond %{REQUEST_URI} !-f RewriteRule ...

How to Set Up a Simple Gulp Uglify Configuration

My objective is to compress all .js files within my project and save a minified version in the same directory. Assuming this is the structure of my project directory: project/ gulpfile.js basic.js Project/ Project.js Toolbelt. ...

Is there a way to access the value of a string variable with unicode characters from a different function than the one where it was originally defined?

Is there a way for me to access the 'result' variable outside of the initial function? The value of 'result' is a Unicode character like 'ﺏ' and I am still learning, so I could use some assistance. Thank you. Below is ...

The callback function of the $.ajax statusCode method does not receive any parameters

As per the official jQuery documentation: If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback. However, when I use th ...

Creating an Array of Images with the JavaScript Canvas Element

As I dive into learning JavaScript and experimenting without jQuery, I am interested in creating something similar to this example. However, my goal is to utilize an array of images instead of just one. This is how my image array is structured: var image ...

What steps should I take to resolve the issue of "Uncaught Error: [News] is not a <Route> component. All components within <Routes> should be either a <Route> or <React.Fragment>"?

I'm having trouble with version 6 of react-router-dom while trying to create a news app. Can anyone help me with fixing it? When I use the following code, I encounter this error: Uncaught Error: [News] is not a component. All component children of ...

Trigger an action upon checking a checkbox in an Angular2 application

As a newcomer to Angular2 and web development in general, I am looking to implement an action that will modify a parameter value of an object in the Database when a checkbox is checked or unchecked using Material-Design. I attempted to use [(ngModel)], but ...

What is the best way to transform a Bootstrap4 navbar into a mobile-friendly sidebar?

I currently have a Bootstrap 4 navbar (similar to the default navbar that collapses from top to bottom). However, I am looking to convert this navbar into a sidebar for the mobile version. Essentially, I want it to look something like this. <na ...

Position the text next to a side panel

I have been attempting to arrange some text next to a side panel that I created using Divs. My initial attempt was to float the text on the left, but unfortunately, this did not produce the desired result. Here is what I currently have: Click here And t ...

How to utilize PHP $_SESSION variables with jQuery for error handling and page redirections

I am currently working on a project where I need to access PHP session variables using jQuery in order to redirect users using jQuery instead of PHP. When a user logs in, a PHP script is executed to check their logged in and registered status in the databa ...

Challenges with dropdown menus in the sub navigation area

I am currently working on a navigation system that includes three levels of sub-navigation elements. While the functionality is fine up to the third level, it only displays one subcategory instead of multiple categories as intended. I suspect there might ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: https://i.sstatic.net/Z6URV.png Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; im ...

Sharing controller methods in Angular.js is a key aspect of enhancing

In my current project, I originally used Knockout for the CMS functionality, but decided to switch to Angular because I preferred its features. One of the key sections in the CMS is dedicated to 'Users', featuring a table where headers can be cli ...