Java Way to write JS program (demo)

Source: Internet
Author: User
Tags addall key sort
js| Program

JavaScript applications are very extensive, and very powerful, but JavaScript program is no Java code good-looking, this is good, but because the JS application is too wide, in the Web application almost inseparable from it, I am beginners js, Always imagine writing a Java program that writes JS.

One is to filter the same elements:
<script src= "Core.js" ></script>
<script src= "Collection.js" ></script>
<script>
var a= "74,77,74,74,89,45,45,55,89";
"74,77,89,45,55"
var arr=a.split (",");
var set=new hashset ();
Set.addall (Arrays.aslist (arr));
A=set.toarray ();
alert (a);
</script>

Another program is completely analog to CGI:
<script src= "Core.js" ></script>
<script src= "Collection.js" ></script>
<style>
td{
font-size:12px;
}
th{
font-size:14px;
}
</style>

<body>

<table width= "50%" align= "Center" >
<tr>
<td> User name: <input type= "text" id= "Search_name" ></td>
<td><a href= "#" > Find Users </a></td>
</tr>
</table>
<div id= "list_table" align= "Center" >

</div>

<table width= "50%" align= "Center" >
<tr>
<td> user id:<input type= "text" id= "txt_id" ></td>
<td> User name: <input type= "text" id= "Txt_name" ></td>
<td><a href= "#" > Add user </a></td>
</tr>
</table>
</body>
<script>
function User (id,name) {
This.id=id;
This.name=name;
This.equals=equals;
function equals (obj) {
return this.id==obj.id;
}
This.compareto=compareto;
function CompareTo (obj) {
return 1;
}
}

function Ascid () {
This.compare=compare;
function Compare (OBJ1,OBJ2) {
if (obj1.id>obj2.id) return 1;
else if (obj1.id==obj2.id) return 0;
else return-1;
}
}


function Descid () {
This.compare=compare;
function Compare (OBJ1,OBJ2) {
if (obj1.id>obj2.id) return-1;
else if (obj1.id==obj2.id) return 0;
else return 1;
}
}


var Table=document.getelementbyid ("List_table");
var list=new sortedlist ();
var buffer=new ArrayList ();
Buffer.add (New User ("M", "Heery"));
Buffer.add (New User ("A", "Marry"));
Buffer.add (New User ("102", "JIM"));
Buffer.add (New User ("M", "Bush"));
Buffer.add (New User ("201", "Eey"));
Buffer.add (New User ("203", "Boy"));
Buffer.add (New User ("", "Oush"));
Buffer.add (New User ("1111111", "Hu Jiangtao"));
Buffer.add (New User ("2324424", "Wen Jiabao"));
Buffer.add (New User ("24325324", "Liu Qi"));
Buffer.add (New User ("245325353", "Wu Shugen"));

List.setcomparator (New Ascid ());
List.addall (buffer);
var id_asc=true;


var pagesize=10;
var curpage=1;

function  gettotalpage () {
 return Math.ceil ((list.size ()/pagesize));
}
Function Getnavigation () {
 var nav= "<tr><td colspan=2>";
 nav+= "<a href= ' # ' > Home </a>  ";
 nav+= "<a href= ' # ' > Prev </a>  ";
 nav+= "<a href= ' # ' > Next </a>  ";
 nav+= "<a href= ' # ' > Last </a>  ";
 nav+= "Total:" +list.size ();
 nav+=   
 nav+= page: "+curpage+"/"+gettotalpage ();
 nav+= "  ";
 nav+= per page display: "+pagesize;
 nav+= "  ";
 nav+= "Current page:" +curpage;
 nav+= "</td></tr>";
 return nav;
}

function FirstPage () {
curpage=1;
Table.innerhtml=getinnerhtml (curpage,pagesize);
}
function NextPage () {
if (Curpage==gettotalpage ()) return;
Table.innerhtml=getinnerhtml (++curpage,pagesize);
}
function Prepage () {
if (curpage==1) return;
Table.innerhtml=getinnerhtml (--curpage,pagesize);
}
function LastPage () {
Curpage=gettotalpage ();
Table.innerhtml=getinnerhtml (curpage,pagesize);
}
function Refresh () {
if (Curpage>gettotalpage ()) curpage=gettotalpage ();
Table.innerhtml=getinnerhtml (curpage,pagesize);
}

function AddUser () {
var Id=document.getelementbyid ("txt_id"). Value;
var Name=document.getelementbyid ("Txt_name"). Value;
if (ID_ASC)
Buffer.add (New User (Id,name));
List.add (New User (Id,name));
Refresh ();
}

function Searchuser () {
var Key=document.getelementbyid ("Search_name"). Value;

var com=list.getcomparator ();
List=new SortedList ();
if (com!=null) list.setcomparator (COM);
For (var i=0;i<buffer.size (); i++) {
var u=buffer.get (i);
if (U.name.indexof (key)!=-1) List.add (U);
}
Refresh ();
}


function Deluser (ID) {
Buffer.remove (New User (Id,name));
List.remove (New User (Id,name));
Refresh ();
}


function getinnerhtml (pg,size) {
var s= "<table width=50% align=center><tr><th id=\" user_id\ "align=left width=40%> user Id[<a ' # ' > Descending </a>]</th><th align=left> user name </th><th></td></tr> ';

var first= (pg-1) *size;
for (Var i=0;i<size;i++) {
var index=first+i;
if (Index>=list.size ()) break;
var u=list.get (index);
s+= "<tr><td>";
S+=u.id;
s+= "</td><td>";
S+=u.name;
s+= "</td><td>";
s+= "<a href= ' onclick=\" Deluser (' ";
S+=u.id;
s+= "') \" > Delete </a> ";
s+= "</td></tr>";
}
S+=getnavigation ();
s+= "</table>";
return s;
}
Delay ()
table.innertext= "Loading";
function OneStep () {
table.innertext= "Loading ...";
}
function Twostep () {
table.innertext= "Loading ...";
}
function Threestep () {
The table.innertext= "Loading ...";
}
function Fourstep () {
table.innertext= "Loading ...... Ok! ";
}

function Descid () {
var oldlist=list;
List=collections.sort (Oldlist,new descid ());
Refresh ();
var Id=document.getelementbyid ("user_id");
Id.innerhtml= "User id[<a href= ' # ' > Ascending </a>]";

}

function Ascid () {
var oldlist=list;
List=collections.sort (Oldlist,new ascid ());
Refresh ();
}

SetTimeout ("OneStep ()", 500);
SetTimeout ("Twostep ()", 1000);
SetTimeout ("Threestep ()", 1500);
SetTimeout ("Fourstep ()", 2000);
SetTimeout ("FirstPage ()", 3000);
FirstPage ();

</script>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.