Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel Table rows by date with Java?

Status
Not open for further replies.

rhrowland

Technical User
Aug 26, 2010
9
US
I have a web page with a table that has a courtesy listing of several community events listed. I would like to have a way of getting the individual rows of the table to either go blank or delete when the event is over.
I need a short code that I can put in each table row, with the date of the event in that row, so that when that event date is past, the event will disappear.
Currently there are only a couple of events, sometimes there are nearly a dozen.
I have made several javascripts already with no success and the assist of someone with more expertise would be greatly appreciated.
I have indicated the website, if you want to take a look.
[bugeyed]
 
Hi

Wrong approach. That should be solved with server-side scripts.

Anyway, you can go with JavaScript too, but I suggest to organize your data first :
[ul]
[li]Instead of "MAY 27th", "May 30, 2011" and who knows what other formats could come, use only one date format.[/li]
[li]Mark the dates distinctly, like putting them into separate column or adding a distinct [tt]class[/tt] to them.[/li]
[/ul]
( If you would use HTML 5, the [tt]time[/tt] tag would be the best. That way you could let the dates formatted as you wish. )
rhrowland said:
I have made several javascripts already with no success
Post them and we will help to correct them.


Feherke.
 
Here is the code that is in the table row.
---------------------------------------------
<tr bordercolor="#000000" bgcolor="#FFFFFF">
<td valign="top" align="left" height="30">
<script type="text/javascript">
<!--

var now = new Date();


document.write('<style type="text/css">')

if (now < "05/23/2011" ){
document.write( **************** TEST SCRIPT FOR TABLE ROW *********************** )
}

document.write('<\/style>')

//-->
</script>
</td>
</tr>
---------------------------
I have also published the page on the website as careerpathinc.com/about_us1.com if you want to actually see the page. THANKS FOR THE HELP
 
Hi

Sorry, I am not able to get the theory of that code.

Here is how I would do it :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]parentelem[/color][teal]([/teal]elem[teal],[/teal]type[teal])[/teal]
[teal]{[/teal]
  type[teal]=[/teal]type[teal].[/teal][COLOR=darkgoldenrod]toLowerCase[/color][teal]()[/teal]
  [b]while[/b] [teal]([/teal]elem[teal].[/teal]parentNode [teal]&&[/teal] elem[teal].[/teal]parentNode[teal].[/teal]nodeName[teal].[/teal][COLOR=darkgoldenrod]toLowerCase[/color][teal]()!=[/teal]type[teal])[/teal]
    elem[teal]=[/teal]elem[teal].[/teal]parentNode
  [b]return[/b] elem[teal].[/teal]parentNode
[teal]}[/teal]

window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]var[/b] table[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'upcoming'[/i][/green][teal])[/teal]
  [b]if[/b] [teal](![/teal]table[teal])[/teal] [b]return[/b]
  [b]var[/b] timelist[teal]=[/teal]table[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'time'[/i][/green][teal])[/teal]
  [b]if[/b] [teal](![/teal]timelist[teal].[/teal]length[teal])[/teal] [b]return[/b]
  [b]var[/b] now[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]timelist[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal](![/teal]timelist[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]getAttribute[/color][teal]([/teal][green][i]'datetime'[/i][/green][teal]))[/teal] [b]continue[/b]
    [b]var[/b] time[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal]timelist[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]getAttribute[/color][teal]([/teal][green][i]'datetime'[/i][/green][teal]))[/teal]
    [b]if[/b] [teal]([/teal]time[teal]>=[/teal]now[teal])[/teal] [b]continue[/b]
    [b]var[/b] parent[teal]=[/teal][COLOR=darkgoldenrod]parentelem[/color][teal]([/teal]timelist[teal][[/teal]i[teal]],[/teal][green][i]'tr'[/i][/green][teal])[/teal]
    [b]if[/b] [teal](![/teal]parent[teal])[/teal] [b]continue[/b]
    parent[teal].[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
  [teal]}[/teal]
[teal]}[/teal]
This uses the [tt]time[/tt] HTML 5 tag like below :
HTML:
[b]<table[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"upcoming"[/i][/green][b]>[/b]
[b]<tr>[/b]
[b]<th>[/b]UPCOMING SPECIAL EVENTS[b]</th>[/b]
[b]</tr>[/b]

[b]<tr>[/b]
[b]<td>[/b]
[b]<time[/b] [maroon]datetime[/maroon][teal]=[/teal][green][i]"2011-05-08"[/i][/green][b]>[/b]Second Sunday of May[b]</time>[/b] is gone.[b]<br>[/b]
[b]</td>[/b]
[b]</tr>[/b]

[b]<tr>[/b]
[b]<td>[/b]
[b]<time[/b] [maroon]datetime[/maroon][teal]=[/teal][green][i]"2011-05-27"[/i][/green][b]>[/b]MAY 27th[b]</time>[/b] is LAST DAY OF SCHOOL FOR MIDLAND PUBLIC SCHOOLS.[b]<br>[/b]
Check with your child&#146;s school for early release schedule.[b]<br>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"[URL unfurl="true"]http://www.midlandisd.net"[/URL][/i][/green] [maroon]target[/maroon][teal]=[/teal][green][i]"_blank"[/i][/green][b]>[/b]CLICK HERE to visit the MISD Website[b]</a>[/b]
[b]</td>[/b]
[b]</tr>[/b]

[b]<tr>[/b]
[b]<td>[/b]
MEMORIAL DAY IS MONDAY, [b]<time[/b] [maroon]datetime[/maroon][teal]=[/teal][green][i]"2011-05-30"[/i][/green][b]>[/b]May 30, 2011[b]</time>[/b].
[b]</td>[/b]
[b]</tr>[/b]
[b]</table>[/b]
One advice : dump that page. That mess of deprecated and never existed tags and attributes spread all over is hard to maintain and even harder to enhance.

Ps : I like your error 404 page.


Feherke.
 
by the "error 404 page" I assume you mean the "about_us1". I only put it up so I could get some help. There is no link there from the actual website.
 
Feherke, It does not seem to work. When I try to preview the page, I get the following at the top of the page........
{ type=type.toLowerCase() while (elem.parentNode && elem.parentNode.nodeName.toLowerCase()!=type) elem=elem.parentNode return elem.parentNode}window.onload=function() { var table=document.getElementById('upcoming') if (!table) return var timelist=table.getElementsByTagName('time') if (!timelist.length) return var now=new Date() for (var i=0,l=timelist.length;i=now) continue var parent=parentelem(timelist,'tr') if (!parent) continue parent.style.display='none' } }
AND.... I have tried adjusting the timestamp to a previous date and the text does not disappear.
Just to be sure on what to do. I put your first script in the header of the page, and the last in the cell of the table. Correct?
 
I just had a revelation... If I entered each row with an invisible timestamp, would it be a lot easier to write a script that would not show the cell when the time was up?
Something like when I enter the info, it would write
timestamp = expiration date to the property of the cell.
then the visit date would be less than the timestamp.
That sounds a lot easier.
What do you think?
 
Hi

rhrowland said:
by the "error 404 page" I assume you mean the "about_us1".
No. By "error 404 page" I mean the page returned by server when the requested page is not found, for example . ( As on 14 May 11 12:01 you mistyped the URL specifying about_us1.com instead of about_us1.htm, that also made the server return the error 404 page. )
rhrowland said:
It does not seem to work.
It works for me in Gecko, Presto and WebKit. As I not used brand new, barely supported or proprietary features, it should work in all modern browsers.
rhrowland said:
I get the following at the top of the page
Sounds like you not enclosed the JavaScript code between [tt]<script>[/tt] and [tt]</script>[/tt] tags.
rhrowland said:
I put your first script in the header of the page, and the last in the cell of the table. Correct?
Maybe, depending on what you mean by "the table". Normally the table I posted should be the only table in that document as you have no other tabular data.
rhrowland said:
If I entered each row with an invisible timestamp, would it be a lot easier to write a script that would not show the cell when the time was up?
That is also a possibility. But I think it would help more the human readers. For the JavaScript code would have less importance. At least for now.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top