Increment column number for each row by 1

Increment column number for each row by 1

orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2
edited August 2019 in General

Hello,

I uje ajax to fill my table. I want to set number for each row, 1,2,3,4,5.... but can not increment. For example here is a code of rebder
{ targets : [0], render: function ( data, type, row, meta ) { return data.meta+1; } },

Replies

  • colincolin Posts: 12,903Questions: 0Answers: 2,199

    Hi @orik3ll0 ,

    That looks about right. 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

  • kthorngrenkthorngren Posts: 14,108Questions: 25Answers: 3,326

    See if this example does what you want:
    https://datatables.net/examples/api/counter_columns.html

    Kevin

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    @colin Hi, Thank you for the response. I will do it

    @kthorngren Thank you for reply. Yes, it works, but if i jump from any page to last one, let say for example from 1 page to 63: it shows me 1-10 and on 64 page shows 11-20, but should show 631-640. That exactly what i need to fix

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    here is js code in fiddle
    https://jsfiddle.net/Orik3ll0/vdqtx764/3/

  • colincolin Posts: 12,903Questions: 0Answers: 2,199

    Hi @orik3ll0 ,

    That fiddle doesn't run. But I think you just need to use createdRow to add an incrementing number to each row.

    Cheers,

    Colin

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    I solved problem with page.info and made changes for server-side case. Here is the code

    table.on('draw.dt', function () { var info = table.page.info(); table.column(0, { search: 'applied', order: 'applied', page: 'applied' }).nodes().each(function (cell, i) { cell.innerHTML = i + 1 + info.start; }); });

  • temo18playstemo18plays Posts: 1Questions: 0Answers: 0

    I've had to do decrement column, it was a pain. There were no topics about that, I want to post code here. This is not an aswer to question but maybe it will help someone.

    It was nearly impossible to get current_page from api, because I needed page number in "columns" property. I could get it on some other places but at first in "columns" it would not be defined, so I wrote this:

    `

    var table_page_number = 0;
    $(document).on('click', ".paginate_button.page-item", function(){
        if($(this).hasClass("disabled")){
            return 0;
        }
        nth_column = 0;
        if($(this).attr("id") == "job_sheet-tbl_next"){
    
            table_page_number++;
            return 0;
        }
        if($(this).attr("id") == 'job_sheet-tbl_previous'){
            table_page_number--;
            return 0;
        }
        table_page_number = $(this).children("a").html();
        console.log($(this).children("a").html());
    });`
    

    In columns:
    select[name='job_sheet-tbl_length'] - dropdown, where you choose how many rows have to be displayed

    render: function ( data, type, row, meta ) 
    {   
        var records_on_page = $("select[name='job_sheet-tbl_length'] option:selected").val();
        var start_id = meta.settings._iDisplayStart;
        var records_total = meta.settings._iRecordsTotal;
        var return_val = (records_total - ((table_page_number-1)*records_on_page)) - parseInt(nth_column);
        nth_column++;
    
        if(nth_column == records_on_page){
            nth_column = 0;
        }
        return return_val;
    }
    

    In summary, I get entire row count, then subtract (current_page_number * records_per_page) - (column_index) to get descending order.

    If anyone knows better approach, please post it.
    p.s. I am using ssp for this.

Sign In or Register to comment.