How to dynamically add columns in JavaScript, javascript dynamic

Source: Internet
Author: User

How to dynamically add columns in JavaScript, javascript dynamic

This article describes how to dynamically add columns in JavaScript. Share it with you for your reference. The specific implementation method is as follows:

/***** Dynamically Add the 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 set or is false, $. grep only collects the array elements returned by the callback function. // otherwise, the [invert] parameter is true, $. the grep collection callback function returns an array element of false}, false); console. dir (temp); */if (resultJson! = "") {Var cv_arr = new Array (); var date_arr = new Array (); // parse the data source and parse it into the date and result $ that can be recognized by the program. each (resultJson, function (index, val) {if (val ['parser _ schedule']! = Null) {cv_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; // to increase the scalability of the method. I modified for (var I = 0; I <. length; I ++) {if (a [I] = value) return I ;}} // The ARR value starts from 0 var old_td_n = $ ("# mytable thead tr "). find ("th "). length; // get existing TH // set TH information for (I = 0; I <date_arr.length; I ++) {// pay attention to TH and TD, different styles $ ("# mytable thead tr "). append ("<th>" + date_arr [I] + "</th>"); $ ("# mytable tbody tr "). append ("<td>-</td>");} var rowNum_arr = new Array (); // obtain the total number of rows for (I = 0; I <$ ("# mytable "). find ("tr "). length; I ++) {// obtain the value of the third column in each row. The first two columns of the table are hidden, the third column starts to judge whether there is data var str =$ ("# mytable tbody "). find ("tr "). eq (I); var std = str. find ("td "). eq (2); // record when no value exists and obtain the row 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 ++) {// obtain the date data var temp_str = cv_arr [I] In RESULT_ARR; // OK // use the date data ", "split into arrays var temp_arr = temp_str.split (", "); // OK [03-, 03-, 03-, 03-, 03-] for (j = 0; j <temp_arr.length; j ++) {// obtain the dataset as a string, for example, 03-11: 10 var temp_result = temp_arr [j]; /* divide the dataset into arrays [03-11: 10] ARR [0] 03-11 ARR [1] 10 */var temp_result_arr = temp_result.split (":"); // set the content in TR of row I $ ("# mytable tbody "). find ("tr "). eq (rowNum_arr [I]). find ("td" ).eq(old_td_n1_date_arr.in_arr(temp_result_arr1_01_00000000.html (temp_result_arr [1]); // apply the style $ ("# tb tr: even td "). addClass ("alt"); // the color of the row $ ("# tb tr "). find ("td: eq (2 )"). addClass ("spec"); $ ("# tb tr: even "). find ("td: eq (2 )"). addClass ("specalt ");}}}}

I hope this article will help you design javascript programs.

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.