Dynamic columns

Dynamic columns

richhouderichhoude Posts: 2Questions: 0Answers: 0
edited February 2010 in General
Is there a way to dynamical y add columns?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    No I'm afraid not. DataTables currently does not support dynamically adding and removing columns. Sorry. It's on the roadmap for a future version - although not exactly sure when yet!

    Allan
  • richhouderichhoude Posts: 2Questions: 0Answers: 0
    Thank you much.
  • rhylockrhylock Posts: 1Questions: 0Answers: 0
    Here's a basic java bean and jsp page that will do what you ask. The bean creates two lists: one with the column names and the other with a list of data. Obviously, this can be augmented to support any form of data you'd like. The jsp renders the table. Once it's completed (which is vital as the table structure needs to exist in the source on the client side) the datatables instance is defined. Hope this helps. Sorry about the condensed nature of the code, I ran out of characters.

    DataTablesBean.java
    [code]
    package beans;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    public class DataTablesBean {
    List columns = new ArrayList();
    List data = new ArrayList();
    public void setData(int number) {
    this.columns.add("patient d");
    this.columns.add("first name");
    this.columns.add("last name");
    this.columns.add("date of birth");
    Random r = new Random();
    for (int i = 1; i <= number; i++) {
    List tuple = new ArrayList();
    tuple.add("" + i);
    tuple.add("fname" + i);
    tuple.add("lname" + i);
    int month = r.nextInt(11) + 1;
    int day = 0;
    if(month == 2){ // ignore leap year
    day = r.nextInt(27) + 1;
    } else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
    day = r.nextInt(30) + 1;
    } else {
    day = r.nextInt(29) + 1;
    }
    int year = r.nextInt(100) + 1900; // between 1900 - 2000
    tuple.add(month + "/" + day + "/" + year);
    this.data.add(tuple);
    }
    }
    public List getListHeader() {
    return this.columns;
    }
    public List getListData() {
    return this.data;
    }
    }
    [/code]

    dataTables.jsp
    [code]
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



    DataTables Example Using a Java Bean

    @import "media/css/demo_page.css";
    @import "media/css/demo_table.css";
    @import "media/css/demo_table_jui.css";
    @import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";




    function setDataTable(){
    // the show MUST go first in order for the columns to correctly render
    $('#example').show();
    $('#example').dataTable( {"bProcessing": true, "sScrollX": "100%", "sScrollXInner": "110%", "aaSorting": [[ 0, "asc" ]], "bJQueryUI": true, "sPaginationType": "full_numbers"} );
    $('#loadingData').hide();
    };



    DataTables Example Using a Java Bean
    This example creates a DataTables instance with dynamic columns and data retrieved from a Java bean.


    <% obj.setData(10000); %>
    Please wait while your data loads...




    ${h}



    ${data}



    ${h}




    <!-- once the data is rendered, call this setDataTable function -->
    this.setDataTable();


    [/code]
This discussion has been closed.