Ways to dynamically add columns to JavaScript _javascript tips

Source: Internet
Author: User

The examples in this article describe how JavaScript dynamically adds columns. Share to everyone for your reference. The implementation method is as follows:

/*** * Dynamically add Table column * @param result/function AddRow (Resultjson) {/* var temp = []; 
temp = $.grep (arr, function (val, key) {if (Val.indexof (' C ')!=-1) return true; 
 If the [invert] parameter is not given or false, $.grep collects only the array elements that the callback function returns True, or the [invert] argument is true, $.grep the array element} that collects the callback function false, false); 
 Console.dir (temp); 
   */if (resultjson!= "") {var cv_arr=new Array (); 
var Date_arr = new Array (); Parses the data source and resolves into a program-recognized date and result $.each (Resultjson, function (Index, VAL) {if (val[' Parser_schedule ']!=null) {C 
     V_arr.push (val[' parser_schedule ')); 
     var temp_data =val[' Parser_schedule '].split (","); 
      for (i=0;i<temp_data.length;i++) {var temp = Temp_data[i].split (":"); 
     Date_arr.push (Temp[0]); 
   } 
    } 
   }); 
  Date_arr = unique (Date_arr);
   Date_arr.in_arr = function (value) {var a = this; In order to increase the adaptability of the method extension. 
   I have slightly modified the next for (var i = 0; i < a.length i++) {if (a[i] = = value) return i; }//arr the subscript value starting at 0 Var old_td_n = $ ("#mytable thead tr"). Find ("th"). Length; Get an existing th//set TH information for (i=0;i<date_arr.length;i++) {//note th and TD, Style is different $ ("#mytable thead tr"). Append ("<th& gt; " 
   +date_arr[i]+ "</th>"); 
  $ ("#mytable tbody tr"). Append ("<td>-</td>"); 
  var rownum_arr=new Array (); Gets a total number of rows for (i=0;i<$ ("#mytable"). Find ("tr"). length;i++) {//Get the value of the third column in each row, the first two columns are hidden, and the third column begins to determine whether there is data var str=$ ("#my 
   Table Tbody "). Find (" tr "). EQ (i); 
   var std=str.find ("TD"). EQ (2); 
   When no value is recorded, gets the line number if ($.trim (Std.text ()). Length <1) {Rownum_arr.push (i); 
   }else{//$ (STD). html (""); }//Set TD information for (i=0;i<cv_arr.length;i++) {//Get Date data in Result_arr var temp_str = Cv_arr[i];//ok//Will Day Period data with "," divided into groups of var Temp_arr = Temp_str.split (",");//ok [03-11:10,03-12:9,03-13:8,03-14:15] for (J=0;j<temp_arr.len 
    gth;j++) {//Get DataSet for string such as 03-11:10 var temp_result = temp_arr[j]; /* Split the dataset into an array [03-11:10] arr[0] 03-11 arr[1]A/var Temp_result_arr = Temp_result.split (":"); Sets the contents of the TR in line I ("#mytable tbody"). Find ("tr"). EQ (Rownum_arr[i]). FIND ("TD"). EQ (Old_td_n+date_arr.in_arr temp_  
    RESULT_ARR[0]). HTML (temp_result_arr[1]); 
    Apply Style $ ("#tb Tr:even TD"). ADDCLASS ("alt");//Row Color $ ("#tb tr"). Find ("Td:eq (2)"). AddClass ("spec"); 
   $ ("#tb Tr:even"). Find ("Td:eq (2)"). AddClass ("Specalt");

 } 
  } 
 } 
}

The

wants this article to help you with your JavaScript programming.

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.