"Troubleshooting Dojo Drag and Drop Problem: Transformation of Data into JSON Format within the Destination Container

I am working with two DND containers, SourceContainer and TargetContainer. The SourceContainer contains some JSON data as shown below:

Apple 
Orange 
Banana

This JSON is being used:

var json = {"fruits": [
           {"fruitId": 1, "fruitName": "Apple", "fruitDesc": "This is an Apple"},
           {"fruitId": 2, "fruitName": "Orange", "fruitDesc": "This is an Orange"},
           {"fruitId": 3, "fruitName": "Banana", "fruitDesc": "This is a Banana"}
        ]
    };

var results="";
   for(var i=0; i<json.fruits.length; i++){
     results += '<div class="dojoDndItem ">' + '<span style="visibility: hidden">' + json.fruits[i].fruitId + '</span>' + json.fruits[i].fruitName  + '</div>';
   }

In my SourceContainer:

<div><label>Fruits</label></div> 
<div dojoType="dojo.dnd.Source" id="source1" copyonly="false" delay="8" class="container dojoDndSource dojoDndTarget dojoDndContainer">
<div dojoType="dojo.dnd.Source" ></div> results </div>

In my TargetContainer:

<div><label>Basket</label></div>
<div dojoType="dojo.dnd.Source"  id="source2" copyonly="false" descending="true" delay="8" class="container dojoDndSource dojoDndTarget dojoDndContainer">
<div dojoType="dojo.dnd.Source" ></div>
</div>

<button id="toJsonBtn" data-dojo-type="dijit.form.Button" type="button">ToJson</button>

When I drag "Apple" and "Banana" to the TargetContainer and click the button, I need the data in TargetContainer to be converted to JSON format.

If you have any ideas on how to achieve this without using JQuery, please share. Thank you!

NOTE: Please do not use JQuery for this solution.

Answer №1

I discovered the solution to this problem and am sharing it for anyone interested;

Json Data

var json = {"fruits": [
               {"fruitId": 1, "fruitName": "Apple", "fruitDesc": "This is an Apple"},
               {"fruitId": 2, "fruitName": "Orange", "fruitDesc": "This is an Orange"},
               {"fruitId": 3, "fruitName": "Banana", "fruitDesc": "This is a Banana"}
            ]
        };

Contents of Target Container

Apple 
Orange 
Banana

Create Target Container

var fruitListContainer = new dojo.dnd.Source("fruitNode");

dojo.connect(dojo.byId('toJsonBtn'), 'onclick', function() {
  var target_container = fruitListContainer.getAllNodes();
  var fruit_arr = [];
  for(var i=0;i<target_container.length;i++){
      var fruit_results = target_container[i].childNodes[0].nodeValue;
      roles_arr.push(fruit_results);
  }
var result={};
result["fruits"]=fruit_arr;
alert(JSON.stringify(result));

The alert output will be as follows (as required):

{"fruits":["Apple","Orange","Banana"]}

Note: If further explanation is needed, feel free to ask!

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

Express-hbs: Dynamic Helper Function with Additional Features

I am currently utilizing express-hbs and Async Helpers in my project. However, I am facing an issue with passing options to the helper as async helpers do not seem to support this feature (or maybe I am unaware of how to do it correctly). In the code snipp ...

Identifying text within clicked divs using identical ids

$(document).ready(function(){ $('#peoplelayer').click(function(){ $(this).fadeOut(500); var str = $(this).text(); alert(str); }); }); This is code where I use the same id "#peoplelayer" for all the divs. When on ...

Efficiently loading images asynchronously for smooth execution of JavaScript using Splide. Resolving Largest Contentful Paint (LCP) issue specifically on mobile

My webpage features a carousel of images at the top, created with Splide. However, I have noticed that my LCP score is significantly lower on mobile devices compared to desktop. The culprit seems to be in the Load Delay section: Time to first byte (TTFB) ...

Symphony 5 and Encore: Issue with Bootstrap JS Loading

Encountering issues while trying to integrate Bootstrap's js with Symfony 5, encore, stimulus, etc... All required dependencies like jquery, popper, corejs are installed and functioning, except for Bootstrap's JS components. The compilation of ...

Jest encountered an UnhandledPromiseRejection error because the promise was unexpectedly resolved instead of being rejected

I am facing a difficult error message from Jest that I can't seem to figure out. The error message indicates that the promise is being resolved instead of rejected, causing an unhandled promise rejection. It's confusing because Jest expects an er ...

Error message: "Property undefined when Angular attempts to call a function from jQuery/JavaScript."

I'm currently attempting to invoke an angular controller from my JavaScript code. This is my first encounter with Angular and I must admit, I'm feeling a bit overwhelmed! I've been following this example: Unfortunately, when testing it out ...

Transitioning to the Bootstrap library from the jQuery library with the use of several image modals

After coming across this specific question about implementing multiple image modals on a webpage, I noticed that it primarily focused on javascript and jQuery. However, my project involves utilizing the latest version of Bootstrap, so I'm curious if t ...

``There was an issue with the connection while fetching data using Nextjs middleware

I've encountered an issue where this code works perfectly fine in dev mode but fails when switched to production mode. Can anyone help me figure out what's causing the fetch failure? export default async function middleware(req: NextRequest) { ...

encasing a snippet of code within a customized hyperlink

Here's the following "embed code" for an Instagram photo featuring Kim Kardashian. I'm curious - how can one encapsulate this embed code within an <a> tag (or another tag) so that clicking on the image will redirect to www.google.com? < ...

A guide on applying bold formatting to a specific section of text in React

I have a collection of phrases structured like so: [ { text: "This is a sentence." boldSubstrings: [ { offset: 5, length: 2 } ] } ] My goal is to display each phrase as a line using the following format: ...

Navigating additional information in D3 Node Link Force diagram

Recently delving into the world of D3, I have been pleasantly surprised by its capabilities and decided to experiment with the Directional Force Layout. My Objective Initially, I successfully created a json object using a for loop to prepare my items for ...

Tips for extracting a value from a currently active list item's anchor tag with JQuery on Mapbox API?

Currently, I am attempting to extract the value from a forward geocoder that predicts addresses while a user is typing. My goal is to then send this value to a form with an id of "pickup". However, I am encountering difficulties in capturing the li > a ...

Choose dropdown menus that are pre-filled by JSON may require a manual refresh to show the content

Occasionally, I encounter an issue with a JSON file used to populate select boxes, where the items in the drop down fail to display until the page is refreshed. Despite no errors in the console or log, the loading is seamless on most occasions, leaving me ...

I tried utilizing the useState hook to store the value, but surprisingly, it failed to update

I am brand new to Reactjs and currently working on creating an address form that includes 3 select options for country, state, and city. I have implemented React hooks in my project, where the page fetches a list of countries upon initial load. Subsequentl ...

Sending users to either Page A or Page B depending on the response received from the API

Currently facing a dilemma. Imagine having a main hub page where you can navigate to either page A or page B. On this main page, there is a list of items. The goal is to trigger a GET API call upon clicking any item in the list. Based on a boolean field i ...

Enhance the performance of your React/NextJS app by controlling the rendering of a component and focusing on updating

I'm facing an issue with my NextJS application that showcases a list of audio tracks using an <AudioTrackEntry> component. This component receives props like the Track Title and a Link to the audio file from an external data source. Within the ...

Retrieve the present value from a Selectpicker using jQuery within the Codeigniter Framework

I attempted to use DOM manipulation to retrieve the value of the currently selected option from the selectpicker. My goal was to have the value of the selectpicker id="service_provider_select" printed first. However, whenever I changed the option using the ...

Delete the span element if the password requirements are satisfied

I am implementing password rules using span elements. I aim to dynamically remove each span that displays a rule once the input conditions are met. Currently, I have succeeded in removing the span related to the minimum length requirement but I am unsure h ...

Dynamically allocate a controller to an element following the Bootstrap process

I have a unique AngularJS application that is initialized manually at a specific time. Later on, I need to display an HTML element without any ng-controller assigned to it. Is there a way to dynamically add an ng-controller to this element in order for it ...

What is the best way to compare dates in PostgreSQL timestamp with time zone?

In my PostgreSQL database, there is a field labeled as "timestamp with time zone compare" Currently, I am trying to implement a data range comparison using JavaScript var start = Date.UTC(2012,02,30);//1333065600000 var end = Date.UTC(2013,02,30); //136 ...