Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29898805
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-15 16:07:45

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="" xml:lang="en" lang="en">
  3. <head>
  4. <title>Checkboxes</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

  6. <style type="text/css">

  7. .table-wapper {padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; width:100%; border:1px solid #676767; height: 200px}
  8. .create-list-table {padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; width:100%;}
  9. .create-list-table th {font-family:arial,sans-serif;font-size:85%;padding:5px 4px; font-weight:normal; border-bottom:1px solid #676767; background-color:#F0F0F0;}
  10. .create-list-table td {font-family:arial,sans-serif;font-size:85%;padding:5px 4px;}
  11. .create-list-table .align-center{text-align:center}
  12. .create-list-table .align-right{text-align:right}
  13. .create-list-table .total-cell {padding-right:295px;text-align:right;}
  14. .create-list-table .alt-row {background-color:#ECF1F7;}
  15. .create-list-table .bottom-border {border-bottom:1px solid #D1DAE3;}
  16. .create-new-list {padding:1px 0px 0px 0px;}
  17. .create-new-list .left-section {width:286px; float:left; padding:0px 0px 0px 0px;}
  18. .create-new-list .middle-section {width:100px; float:left; padding:60px 0px 0px 0px;}
  19. .create-new-list .middle-section input {width:75px;}
  20. .create-new-list .right-section {width:286px; float:left;}
  21. .create-new-list .button {font-size:85%; width:58px; margin:6px 0px 6px 8px;}
  22. .create-new-list h3 {font-family:arial,sans-serif;font-size:93%; font-weight:bold;padding:0px 0px 3px 0px;}
  23. </style>

  24. </head>
  25. <body>    
  26.             
  27. <div class="create-new-list">
  28.     <div class="left-section">
  29.         <div class="table-wapper">
  30.             <table class="create-list-table" border="0" cellpadding="0" cellspacing="0" summary="Summary" id="allPositionsTable">
  31.                 <thead>
  32.                     <tr>
  33.                         <th scope="col" class="align-center"><label class="off-screen" for="checkAllPositions"></label><input type="checkbox" name="position" id="allPositionsTableCheckbox" value="" class="check" onclick="selectAllDomain('allPositionsTableCheckbox','allPositionsTableItem')"/>
  34.                         </th>
  35.                         <th scope="col">Description
  36.                         </th>
  37.                         <th scope="col" class="align-right">Quantity
  38.                         </th>
  39.                     </tr>
  40.                 </thead>
  41.                 <tbody>
  42.                     
  43.                 </tbody>
  44.             </table>
  45.         </div>
  46.     </div>
  47.     <div class="middle-section">
  48.         <input type="submit" class="button" value="Add>>" id="add" onclick="selectPositions()"/>
  49.         <input type="submit" class="button" value="<<Remove" id="remove" onclick="removePositions()"/>
  50.     </div>
  51.     <div class="right-section">
  52.         <div class="table-wapper">
  53.             <table class="create-list-table" border="0" cellpadding="0" cellspacing="0" summary="Summary" id="selectedPositionsTable">
  54.                 <thead>
  55.                     <tr>
  56.                         <th scope="col" class="align-center"><label class="off-screen" for="selectedPositions"></label><input type="checkbox" name="position" id="selectedPositionsTableCheckbox" value="" class="check" onclick="selectAllDomain('selectedPositionsTableCheckbox','selectedPositionsTableItem')"/>
  57.                         </th>
  58.                         <th scope="col">Description
  59.                         </th>
  60.                         <th scope="col" class="align-right">Quantity
  61.                         </th>
  62.                     </tr>
  63.                 </thead>
  64.                 <tbody>
  65.                     
  66.                 </tbody>
  67.             </table>
  68.         </div>
  69.     </div>
  70. </div>

  71. <script type="text/javascript">

  72. //----------------------------------------------------------------------------------------------------------------------------------------
  73. // Data model
  74. qbq = {};


  75. qbq.CusipList = function(){
  76.     var listName;
  77.     var positionsList;
  78. };


  79. qbq.Position = function(newId, newDes, newQty){
  80.     var id;
  81.     var description;
  82.     var quantity;
  83.         
  84.     this.id = newId;
  85.     this.description = newDes;
  86.     this.quantity = newQty;
  87. };
  88. //----------------------------------------------------------------------------------------------------------------------------------------

  89. //----------------------------------------------------------------------------------------------------------------------------------------
  90. // Data instance
  91. var allPositions = new Array(
  92.         new qbq.Position('1001', 'Des1001', 1001),
  93.         new qbq.Position('1002', 'Des1002', 1002),
  94.         new qbq.Position('1003', 'Des1003', 1003),
  95.         new qbq.Position('1004', 'Des1004', 1004),
  96.         new qbq.Position('1005', 'Des1005', 1005),
  97.         new qbq.Position('1006', 'Des1006', 1006)
  98. );

  99. var cusipList = new qbq.CusipList();
  100. cusipList.positionsList = new Array(1);
  101. //----------------------------------------------------------------------------------------------------------------------------------------

  102. // Add positions to table
  103. function addPositionToTable(tableId, positionSet) {

  104.     var positionsTable = document.getElementById(tableId);
  105.     var trName = tableId + "Item";
  106.     
  107.     if (positionSet.length > 0 && typeof(positionSet[0]) != "undefined") {
  108.         for(var i = 0; i < positionSet.length; i++) {
  109.         
  110.             var positionTr = positionsTable.children[1].insertRow(-1);
  111.             positionTr.id = tableId + 'tr' + positionSet[i].id;
  112.             
  113.             var positionTd = new Array(3);
  114.             for(var j = 0; j < positionTd.length; j++) {
  115.                 positionTd[j] = positionTr.insertCell(-1);
  116.             }
  117.             positionTd[0].className = 'align-center';
  118.             positionTd[0].innerHTML = '+ positionSet[i].id + '" id="' + tableId + 'cb' + positionSet[i].id + '" name="'+ trName + '" onclick="changeSelect(\'' + tableId + 'Checkbox' + '\',\''+ trName +'\')" />';
  119.             //positionTd[1].innerHTML = positionTr.rowIndex;
  120.             positionTd[1].innerHTML = positionSet[i].description;
  121.             positionTd[2].className = 'align-right';
  122.             positionTd[2].innerHTML = positionSet[i].quantity;
  123.         }
  124.     }
  125. }

  126. // Remove positions to table
  127. function removePositionToTable(tableId, positionSet) {

  128.     var positionsTable = document.getElementById(tableId);
  129.     
  130.     if (positionSet.length > 0 && typeof(positionSet[0]) != "undefined") {
  131.         for(var i = positionSet.length - 1; i >= 0; i--) {
  132.         
  133.             var trId = tableId + 'tr' + positionSet[i].id;
  134.             var tr = document.getElementById(trId);
  135.             positionsTable.deleteRow(tr.rowIndex);
  136.             
  137.         }
  138.     }
  139. }

  140. // Select all Checkbox
  141. function selectAllDomain(selectAllId,selectedDomainsName){
  142.     var selectAll = document.getElementById(selectAllId);
  143.     var selectedDomains = document.getElementsByName(selectedDomainsName);

  144.     if(typeof(selectedDomains)==null || typeof(selectedDomains)== "undefined") {
  145.         return;
  146.     }

  147.     if(selectAll.checked==true) {

  148.         if(typeof(selectedDomains[0])=="undefined"){
  149.             selectedDomains.checked=true;
  150.         }else{
  151.             for(i=0;i<selectedDomains.length;i++){
  152.                 selectedDomains[i].checked=true;
  153.             }
  154.         }

  155.     }else{

  156.         if(typeof(selectedDomains[0])=="undefined"){
  157.             selectedDomains.checked=false;
  158.         }else{
  159.             for(i=0;i<selectedDomains.length;i++){
  160.                 selectedDomains[i].checked=false;
  161.             }
  162.         }

  163.     }
  164.  
  165. }

  166. // Change Checkbox selected status
  167. function changeSelect(selectAllId,selectedDomainsName){

  168.     var selectAll = document.getElementById(selectAllId);
  169.     var selectedDomains = document.getElementsByName(selectedDomainsName);

  170.     if(typeof(selectedDomains[0])=="undefined"){
  171.         se=selectedDomains.checked;
  172.         if(se==false) {
  173.             selectAll.checked=false;
  174.         }
  175.     }else{

  176.         for(i=0;i<selectedDomains.length;i++){
  177.             se=selectedDomains[i].checked;
  178.             if(se==false){
  179.                 selectAll.checked=false;
  180.                 break;
  181.             }
  182.         }
  183.     }

  184.     if(se==true) {
  185.         selectAll.checked=true;
  186.     }
  187. }

  188. // Find position by id
  189. function findPositionById(id , collection){
  190.     for(var i=0;i < collection.length; i++){
  191.         var position = collection[i];

  192.         if(id==position.id){
  193.             return position;
  194.         }

  195.     }
  196. }

  197. // Get selected positions from Checkbox collection
  198. function getSelectedPositions(itemsName) {
  199.     var selectedPositions = new Array(1);
  200.     var checkPositionItems = document.getElementsByName(itemsName);
  201.     for(var i = 0; i < checkPositionItems.length;i++){

  202.         if(checkPositionItems[i].checked == true){
  203.             var position = findPositionById(checkPositionItems[i].value, allPositions);

  204.             if (selectedPositions.length == 1 && typeof(selectedPositions[0]) == "undefined") {
  205.                 selectedPositions[0] = position;
  206.             } else {
  207.                 var tempPositions = new Array(1);                
  208.                 tempPositions[0] = position;
  209.                 selectedPositions = selectedPositions.concat(tempPositions);
  210.             }
  211.      }
  212.     }
  213.     return selectedPositions;
  214. }

  215. // Select positions
  216. function selectPositions() {
  217.     document.getElementById('allPositionsTableCheckbox').checked = false;
  218.     document.getElementById('selectedPositionsTableCheckbox').checked = false;
  219.     
  220.     var tempPositions = getSelectedPositions("allPositionsTableItem");
  221.     if (typeof(tempPositions[0]) == "undefined") {
  222.         return;
  223.     }
  224.     
  225.     if (cusipList.positionsList.length == 1 && typeof(cusipList.positionsList[0]) == "undefined") {
  226.         cusipList.positionsList = tempPositions;
  227.     } else {
  228.         cusipList.positionsList = cusipList.positionsList.concat(tempPositions);
  229.     }
  230.     
  231.     removePositionToTable('allPositionsTable', tempPositions);
  232.     addPositionToTable('selectedPositionsTable', tempPositions);
  233. }

  234. // Remove positions from selected positions
  235. function removePositions() {
  236.     document.getElementById('allPositionsTableCheckbox').checked = false;
  237.     document.getElementById('selectedPositionsTableCheckbox').checked = false;
  238.     
  239.     var selectedPositions = getSelectedPositions("selectedPositionsTableItem");
  240.     if (typeof(selectedPositions[0]) == "undefined") {
  241.         return;
  242.     }
  243.     
  244.     // remove from cusipList.positionsList
  245.     for (var i = 0; i < selectedPositions.length; i++) {
  246.     
  247.         for(var j=0;j < cusipList.positionsList.length; j++){

  248.             if(cusipList.positionsList[j].id == selectedPositions[i].id){
  249.                 cusipList.positionsList.splice(j,1);
  250.             }

  251.         }
  252.     }
  253.     
  254.     removePositionToTable('selectedPositionsTable', selectedPositions);
  255.     addPositionToTable('allPositionsTable', selectedPositions);
  256. }

  257. addPositionToTable('allPositionsTable', allPositions);
  258. </script>
  259. </body>
  260. </html>
阅读(1753) | 评论(0) | 转发(0) |
0

上一篇:jQuery Ajax

下一篇:Google Analytics

给主人留下些什么吧!~~