Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a hidden div to each row in a table

Posted on 2016-11-21
6
Medium Priority
?
186 Views
Last Modified: 2016-11-21
Hi everybody.
I have a table built through DataTables plugin and I need to attach to each row a div in order to get an effect similar to the one you have in mobile gmail app.
More precisely, I want that when user long presses on a row, the hidden div becomes visible hiding the row and occupyig the exact same space: in the hidden div I have the button to deete that row.

This is the code to build the table:
function fillTable() {
	loaderText('Loading...');
	loader.fadeIn();
	setTimeout(function () {
		if ($.fn.dataTable.isDataTable('#grid')) {
			t = jQuery('#grid').DataTable();
		} else {
			t = jQuery('#grid').DataTable({
				"initComplete": function () {
					var api = this.api();
					if (!(api).data().count()) {
						jQuery('#grid thead tr th').addClass('not-arrow');
					} else {
						jQuery('#grid thead tr th').removeClass('not-arrow');
					}
				},
				"ajax": {
					"url": baseurl + "data.php?user_id=" + user_id
				},
				"initComplete": function () {
				// HERE I ADD THE DIV
					$('#grid tbody tr').each(function(){
						$(this).append('<div class="td-buttons" >Delete</div>');
					});
				},
				aoColumns: [
					{
						"mData": 0,
						"mRender": function (data, type, full) {
							return '<div class="user"><a class="avatar" href="#"><span><img class="img-circle" src="' + baseurl + full[0] + '" alt=""></a></span><span class="username"><a href="#">' + full[2] + ' ' + full[1] + '</a></span></div>';
						}
					},
					{
						"mData": 1,
						"mRender": function (data, type, full) {
							return full[4];
						}
					},
					{
						"mData": 2,
						"mRender": function (data, type, full) {
							var dt = full[5].split(' ');
							return dt[0] + ' at ' + dt[1];
						}
					}
				]
			});
		}

                // Here I manage the long press using hammer.js
		$('#grid').hammer({domEvents: true}).on('press', 'tr', function () {
			var user_id = $(this).find('div.user').data('id');
			console.log('pressing row ' + user_id);
			console.log('PRESSS DOWN');
		});

		jQuery('#grid_filter label').html(jQuery('#grid_filter label').find('input'));
		jQuery('#grid_filter label input').attr('placeholder', 'Search').css({
			'color': '#969BAA',
			'font-size': '15px'
		});

		jQuery('#grid_filter label input').on('keyup change', function () {
			t.search(this.value).draw();
		});
		loader.fadeOut();
	}, 500);
}

Open in new window


this produces this markup:
	<tbody>
		<tr role="row" class="odd">
			<td class="sorting_1">
				<div class="user" data-id="764">
					<a class="avatar" href="#">
						<span>
							<img class="img-circle" src="avatar.gif" alt="">
						</span>
					</a>
					<span class="username">
						<a href="#">asdg test7</a>
					</span>
				</div>
			</td>
			<td>test</td>
			<td>2016-10-06 at 13:10:40</td>
			<div class="td-buttons">Delete</div>
		</tr>
                ...
	</tbody>

Open in new window


Here the css for the div:
.td-buttons{
	position: absolute;
	width: 100%;
	left:0;
	background: #ccc;
	z-index: 3;
}

Open in new window

The only problem I get is that I can't set the div height in pixels because rows can have different heights depending on content and setting it to 100% it make it fill the page not the row

How can I fix this?
Thank you for your help
0
Comment
Question by:Marco Gasi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
F Igor earned 2000 total points
ID: 41895912
The DIV you are expecting to insert cannot be a direct child element inside a TR tag (you can create a TD container for that DIV or simply a TD with CSS formatting).

See this fiddle: https://jsfiddle.net/drL8b5g1/1/ for an example.
1
 
LVL 31

Author Comment

by:Marco Gasi
ID: 41896045
@F Igor, thank you for your answer.
You example looks good , but it doesn't fit my need. That way I can't emulate gmail mobile app behavior. In gmail you can swipe left a row to delete an email: when you swipe left a colored space is left instead of the row for some seconds then it is deleted.
Well, I dont know swipe here but press, but I need to hide the row replacing it with an element whose wdth and height be equivalent to the row ones and within this elemet I'll put a button for now, but later I'll have to put other things.
So I'm wondering how I can achieve this...
0
 
LVL 13

Expert Comment

by:F Igor
ID: 41896373
In the next example you could get some ideas for some of your needs, it contains long-press event emulation (long press on row) and swipe (if supported)   in http://jsfiddle.net/drL8b5g1/4/

The "swipe" event could be used only in mobile views if you add the jQuery Mobile plugin, does not work in desktop.

You can easily find some additional examples to get it fully working starting of this point .
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 31

Author Comment

by:Marco Gasi
ID: 41896475
Thank you: with a slight modification to your code I get something closer to what I want: https://jsfiddle.net/marqusG/e8c573fu/3/
1
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 41896477
Thank you
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 41896480
I accepted you answer before to see your last comment. I'm goin to check it right now. Thank you again :)
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question