This post shows how to shift table content using JavaScript. REDIPS.shift library contains four methods to shift table content in any direction (up, right, down and left). By default, content bunching is disabled. Please try to click on links and move content around table.
up | up | up | up | ||
left | right | ||||
---|---|---|---|---|---|
left |
A
|
right | |||
left | right | ||||
left |
B
|
right | |||
left |
C
|
right | |||
down | down | down | down |
redips5.tar.gz package contains this demo (it’s free to download). Source code is written in script.js file (about 150 lines – well commented) and is verified with jslint. Table cells marked with class name “mark” are considered as boundary. Here is JS code snippet (shift content to the left):
/// shift row content left left = function (element) { let tr, // current table row cellSource, // source cell cellTarget; // target cell // find row where cell belongs tr = findParent('TR', element); // loop through cells for (let i = 1; i < tr.cells.length; i++) { // define source and target cell cellSource = tr.cells[i]; cellTarget = tr.cells[i - 1]; // move cells relocate(cellSource, cellTarget); } };
Hope this small JavaScript library will be useful for table content shifting.
I need source of this script code. Thanks!
Source code is prepared in redips5.tar.gz package and can be downloaded from this page. Just click on the download link below post title.
Cheers!
This JavaScript code displays the data in a table with reference to an XML file below. My requirement s i have to show the data in 2pages with next and previous button, please help with this.
XML File
@Gayathri – If you have static XML file then you can split XML to cd_catalog1.xml and cd_catalog2.xml – with this scenario, load first or second file to display on first or second page. On the other hand, if XML file is created dynamically then you will have to send pageno parameter as well. In MySQL is very easy to paginate record set using LIMIT clause. Here is simple SQL example:
Database will return records for requested page. Offset parameter can be calculated on the following way:
As you can see, pagination is mostly handled on server side. It wouldn’t be so efficient to load whole XML file and in “for loop” (on client side) display active page and discard unused rows. Anyway, I hope this few tips will give you some hints of how to approach and solve pagination problem.
Hi
is there a way of stopping it bunching up in 1 cell, so each element stays in a row.
I have got a row of elements that I drag on to a seperate table underneath.
I want the row to bunch up automatically filling the empty spaces and leaving all the spaces at the end.
I was thinking of running this script when the element is dropped.
Cheers
Andrew
@andy – REDIPS.shift (as well this post) is upgraded. Content bunching is now optional and is disabled by default. Please try this demo now. Cheers!
Thanks for that, could you give me any pointers how to impliment this:-
I currently use the drag and drop from 1 table to another,
Table A’ has 1 row Table B is a 10 x 10
I would like it so when I drag an element from ‘A’ and drop it in ‘B’, all the elements in ‘A’ shuffle up filling the empty space leaving the empty cell at the end.
Cheers
Andrew
@andy – I have prepared new example in redips2.tar.gz package. Please see Example 21: Simple shift. Here is event.dropped() JS code:
Source code is located in example21/simple folder of redips2 package.