tabalinas / jsgrid

Lightweight Grid jQuery Plugin
http://js-grid.com
MIT License
1.53k stars 351 forks source link

Create fields/columns dynamically #1119

Open Yanow0 opened 6 years ago

Yanow0 commented 6 years ago

Hello, sorry for my english

I'm new to JSGrid and I wanted to know if there is a way to create dynamically a number of fields/columns ? Basically I want to create N fields with the same parameters but I don't want to write them manually as the number of fields can change.

I have tried many things like the code below to give you an idea of what i want to do.

I know there is a lot of mistakes but it gives an idea of what I am looking for

for (var nbMixer = 1; nbMixer <= nbMixerMax; nbMixer++) { { name : "Mixer " + nbMixer, type : "text", width : 10, itemTemplate : function(value, item) { return $("<input>").attr("type", "checkbox").attr( "checked", value || item.Checked).on("change", function() { item.Checked = $(this).is(":checked"); }); }, align : "center", width : 5 } } return tab;

I tried to add this code in the fields section of my jsGrid but this is stupid.

Feel free to ask if you don't understand something in my request

Thank you

Yanow0 commented 6 years ago

I found a solution to do what I want, here is the code :

$(function() {

    var mixerArray = [];

    //Init array Mixer
    for (var nbMixer = 1; nbMixer <= 64; nbMixer++) {
        mixerArray.push({
            "Mixer" : nbMixer
        });
    }

    var inputArray = [];

    //Init array Input
    inputArray.push({
        name : "Mixer",
        type : "text",
        width : 5
    })

    //Création des Inputs
    for (var nbInput = 1; nbInput <= 64; nbInput++) {
        inputArray.push({
            name : "Input-" + nbInput,
            type : "text",
            width : 5,
            itemTemplate : function(value, item) {
                return $("<input>").attr("type", "checkbox").attr("checked",
                        value || item.Checked).on("change", function() {
                    item.Checked = $(this).is(":checked");
                });
            },
            align : "center",
            width : 5,
        })
    }

    $("#jsGrid").jsGrid({
        height : "100%",
        width : "100%",
        autoload : true,
        confirmDeleting : false,
        pageLoading: true,
        paging : false,
        data : mixerArray,
        fields : inputArray
    });

    console.log(inputArray);
});