update property columnDefs. 'orderData'

update property columnDefs. 'orderData'

slowlyDudeslowlyDude Posts: 3Questions: 1Answers: 0
edited March 2020 in Free community support

Hello, i newbie with datatables

function setDataToTable(data){
    const columns = [];
    const columnDefs = [];
    let dateColumnSortedQuery = 4;

    const updateData = data.map(obj=>{
        obj.year = getYear(obj);
        obj.months = getMonths(obj);
        return obj;
    });

    const captions = Object.keys(updateData[0]);

    captions.forEach((caption, index)=>{
        columns.push({
            data: caption
        });

        columnDefs.push({
            targets: index,
            title: captions[index]
        });

    });

    if(trigger === 'months'){
        dateColumnSortedQuery  = 7;
    }

    columnDefs.push({
        'orderData':[dateColumnSortedQuery],
        'targets': [4]
    });

    columnDefs[7].visible = false;
    columnDefs[6].visible = false;

    return {
        data: updateData,
        columns,
        columnDefs,
        paging: false,
        retrieve: true,
    }
}

let table = $('#example').DataTable(setDataToTable(dataArr));

here i set initial data to table ,

next i change trigger on select

function addEvent () {
    const select = $('select[name="query-select"]');

    select.change(function (e) {
        if(e.target.value === 'months'){
            table.destroy();
            trigger = 'months';
            table = $('#example').DataTable(setDataToTable(dataArr));
        }
        if(e.target.value === 'year'){
            table.destroy();
            trigger = 'year';
            table = $('#example').DataTable(setDataToTable(dataArr));
        }
    });
}

but this don't like me becouse every time i destroy table he redraw to initial data new table, how i can update table without destroy()

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • slowlyDudeslowlyDude Posts: 3Questions: 1Answers: 0

    colin sorry , my problem can i update property columnDefs in select ?

    https://jsfiddle.net/e3tjd6gh/1/

    my task sort colum "birthday_contact" sorted by select query months / year , so i update data + 2 new months , years and months and hide him , so when select months update property columnDefs:[
    {
    dorderData: [7],
    targets: [4],
    aDataSort: [7]
    }],
    }), when select year again overwrite, etc...

    but i don't know this table can listenm this property or not.

    previos version destroy and create new table can work correct for me

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    For those properties, aDataSort, isn't valid - see options here. Also, instead of dorderData, use columns.orderData.

    Thanks for the test case, but I'm not clear what you're trying to do - please could you give step by step instructions on what to do with the test case, what is happening, and what you would prefer to happen.

    Colin

  • slowlyDudeslowlyDude Posts: 3Questions: 1Answers: 0

    Chek my code in jsfiddle , select must be just trigger wich query sorted colum "birthday_contact" , for default table sorted by year, try click header this column ,

    when i swicth select by months , i want sort column "birthday_contact" not by year first 4 number , i want sorted by month "2004-10-20" second two numbers 10

    so i create function get months fromn this string "2004-10-20" and after create new column months and hide him , in this option
    columDefs.push({ dorderData: [7], targets: [4], })
    can tell which colum sorted like onother column , so 4 column "birthday_contact" sorted like 7 hidden months

    but this object push in columnDefs need do not in intiial option , after i swicth select months

    i dont know i can change this property or not

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I'm very sorry, but I'm really not following. The "birthday_contact" column looks like it's ordering correctly to me - is this the problem?

    Please can you just say, "1. press this, 2. do that, 3. this happened but I expected this". That would make it much clearer.

    Colin

This discussion has been closed.