School timetable is example of how to use REDIPS.drag library. Page layout contains two tables – left table with school subjects and timetable on the right. After subject is placed to timetable, button in the same color will be displayed next to the subject (clone object).
The following code shows event.dropped() event handler (with logic for cloning DIV elements across a week).
rd.event.dropped = function () { var objOld = rd.objOld, // original object targetCell = rd.td.target, // target cell targetRow = targetCell.parentNode, // target row i, objNew; // local variables // if checkbox is checked and original element is of clone type // then clone spread subjects to the week if (document.getElementById('week').checked === true && objOld.className.indexOf('clone') > -1) { // loop through table cells for (i = 0; i < targetRow.cells.length; i++) { // skip cell if cell has some content // (first column is not empty because it contains label) if (targetRow.cells[i].childNodes.length > 0) { continue; } // clone DIV element objNew = rd.cloneObject(objOld); // append to the table cell targetRow.cells[i].appendChild(objNew); } } // print message only if target and source table cell differ if (rd.td.target !== rd.td.source) { printMessage('Content has been changed!'); } // show / hide report buttons reportButton(); };
Source code (including school timetable with save/recall table using PHP and MySQL) and detailed description of library can be found on Drag and drop table content with JavaScript.
Hi dbunic,
I need one more thing..
When i picking the left table subject , that can be drop only to the particular period. It must satisfied some constrains like below:
For example ” Art ” subject can only place in Friday not to other days .. also the mathematics period cannot be placed at the last period of a day (it must place any period expect the last period).. like that..
Can it possible ?
Pls help me
@Prabu – Table can be dynamically added to the “drag” container. After table is added, just call REDIPS.drag.init() method. Example 0: AJAX / jQuery modification shows scenario where table is loaded on button click using jQuery.
REDIPS.drag has options to define dropping rules. Please see:
Example 7: A B C D
Example 7: E F
I also wrote REDIPS.drag documentation – Appendix A where you can find explanation with examples for mark and only class name keywords.
Cheers!
dbunic,
None of them work in the example 03.
http://www.redips.net/javascript/drag-and-drop-example-3/
In example three ajax load not work
and constrains not checking..
I dont know why because its working in the link you have mentioned in the last message but not in drag-and-drop-example-3.
Thank you very much dbunic!! You´re rock!!!
Hi Dbunic! You’ve been really helpful but I still got one problem: I add some information along with the subjects like name of the professor and room number, but I don’t know how to pass this additional information to db_save and store it to timetable table. How can I do that? Thanks.
@Prabu – If you can prepare standalone example or send me URL of your online example I will gladly help you. In case of standalone example, prepare your code so I can unzip it and test with local Apache/PHP.
I suppose that problems are related to REDIPS.drag initialization but can’t be sure without peek to the code.
@David Roa – You’re welcome and thanks! ;)
@rosie – One way can be to append professor id and room number to the DIV id. In this case, DIV id can look (please note it’s used dash, not underscore):
On the server side, DIV id should be decomposed:
This is only an idea, but it should be feasible … If you will have further questions, don’t hesitate to ask. Cheers!
dbunic it’s not working. I tried to echo it, it always says undefined offset. I did it like this:
config.php
db_save.php
Where am I doing it wrong?
@rosie – My guess is that you have somewhere glitch in code and it’s not easy to fix it without having access to all parts. Anyway, I have prepared example with $prof and $room so you can try to use this modification.
Hi dbunic!
I was able to save into timetable (database table) the additional information I wanted to add, so that when printing the subjects to timetable (grid) I don’t have to refer to other table but the timetable alone and also so I could delete the list of subjects in the left. (I hope you understand what I mean). But when I clone the elements the information is deleted leaving it (DIV) blank. What I’m just trying to achieve here is to be able to delete the subjects in the left side without the subjects in the timetable being affected.
Thanks.
@rosie – I’m glad you succeeded to save additional data to the database. Maybe the problem is related to the SQL select. If subject is deleted from database, then SQL to retrieve grid table could not join redips_timetable with redips_subject data. Here is original SQL from example03:
Instead of inner join, you can try with left join:
This should retrieve data from redips_timetable regardless of state in redips_subject db table.
Hope this was your question.
Cheers!
Hi dbunic. It’s still not working.
subjects:
timetable:
I can drag the subjects just fine but when I clone it using shift key the data saved in timetable columns are deleted except the data in sub_id, tbl_id and pos columns. Is there another way to delete the subjects without affecting the timetable?
I’m sorry to bother you this much. Thanks.
@rosie – It will be much easier if you can send/show me the live example. Please try to make database dump with mysqldump utility and zip it together with Web scripts (PHP and JS files). I will prepare your example locally on my LAMP computer and try to fix the problem. Hope this will not be a problem for you. Here is how you can save database content on Linux host:
It’s very kind of you but I don’t think it’s a good idea. My codes are messy plus I already altered a lot. I just want to have an option to delete the list of subjects on the left but I think that’s impossible right now. I’ll just stick to your old code. Thank you so much. :)
@rosie – OK, but I have one more idea that may help you. Instead of deletion subject from the left table, you can hide them. Here is how:
1) Expand db table subjects with column display char(1):
All existing subject should have Y value in display column.
2) Modify subjects() function in config.php to retrieve only visible subjects:
3) Try to update subjects as hidden:
Timetable (on the right) should stay intact while subject from the left table is not shown.
Hope this will do the trick ;)
Cheers!
dbunic, is possible to resize subject instead of drop subjects
@wethuu – REDIPS.drag library allows drag and drop DIV elements across one or many tables. DIV element can be resized in a moment of dropping DIV to the table. Here is JS code where DIV element will be resized to the full width of the target cell (can be useful if table cells have different width):
Hi Bunic,
This is rami from Chennai, India.
Here we are trying to develop redips drag and drop control in mvc3 but not getting filled cell values to the controller .
Do you have any idea about this in mvc3.
Pls let me know.
nice article
@rami reddy – I’m entirely new to the ASP.Net MVC3 framework (my scripting background is PHP). Anyway, REDIPS.drag is developed in a way to be completely independent from server side. With a few customizations it should fit to the MVC3 as well.
So, if you need to send information to the server for each DIV element drop, you can write small JS code inside myhandler_drop event handler. Here code snippet from example03/ajax/script.js file:
As you can see, on each element drop REDIPS.drag can fire AJAX call to the server and save element position. It’s also possible to lock dropped element until AJAX callback unlock dropped element. Displayed code is from redips2.tar.gz package.
@upravitelj – Thanks … or I can say “Hvala” on Croatian ;)
hi,
i m unable to replace db_save.php with db_save.jsp please help me!!!