datatables.init.js 3.01 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

function coop_init_datatable(params,data, domsel, cols, action_btn) {
    var buttons = []
    var columns = []    
    var dom = '<lf<t>ip><"clear"><B>'                              
    var select = {
                    style:    'os',
                    selector: 'td:first-child'
                    
                 };
    if (action_btn) {
        buttons = [
                    {
                        extend: 'selected',
                        text: 'Supprimer les sélectionnés',
                        action: function ( e, dt, button, config ) {
                            dt.rows({selected: true}).remove().draw();
                            
                        },
                    },
                    {
                        extend: 'selected',
                        text: 'Ne garder que les sélectionnés',
                        action: function ( e, dt, button, config ) {
                            dt.rows({selected: false}).remove().draw();
                            
                        },
                    }
                  ]
    
        buttons.push(action_btn)

        columns = [ {
                        data: null,
                        defaultContent: '',
                        orderable: false,
                        className: 'select-checkbox',
                        targets:   0
                    }
                  ]
        select.style = 'multi'
       
    }
   
    
    $.each(cols,function(i,e){
            columns.push(e);
    });
    var settings = {
                            dom: dom,
                            lengthMenu : [[10,25,50,100,-1],[10,25,50,100,'Tout']],
                            buttons: buttons,
                            
                            columns: columns,
                            select: select ,
                            rowId : "id",
                            data : data,
                            language: {url : '/static/js/datatables/french.json'},
                            createdRow: function(row, rdata, index) {
                                if (coop_is_weighted_product(rdata) === true) {
                                    $(row).addClass("to_weight");
                                }
                            },
                            initComplete: function() {
                                if (! coop_is_connected())
                                    $('#main_content input[type="search"]').attr('disabled','disabled')
                                
                            }
                    }
    if (params) {
        if (params.page) {
            settings.displayStart = params.page.start
        }
        if (params.ordering) {
            settings.order = params.ordering
        }
        if (params.rowCallback) {
            settings.rowCallback = params.rowCallback
        }
        if (params.no_search) {
            settings.dom = '<l<t>ip><"clear"><B>'
        }
    }
    active_table = $('#main_content').find('table'+domsel).DataTable(settings);

    return active_table
}