custom_lists.js 3.98 KB
Newer Older
1 2 3 4
var shelfs_table = null,
    delete_icon = '<i class="fas fa-trash"></i>';


5 6

function init_datatable() {
7 8 9
    // For a smooth migration...
    for (const i in lists) {
        if (('partners' in lists[i]) === false) {
10
            lists[i]['partners'] = [lists[i]['partner']];
11 12
        }
    }
13

14 15 16 17 18 19 20
    return $('#lists').DataTable({
        data: lists, // data passed at page loading
        rowId: 'id',
        columns:[
            {data: "id", title:"id", "visible": false},
            {
                data:"datetime_created",
Félicie committed
21
                orderData:[0],
22
                title:"Liste",
23
                render: function (data) {
24 25 26
                    return "Liste du " + data;
                }
            },
27
            {
28 29 30 31 32 33 34
                data:"partners",
                title:"Fournisseur(s)",
                render: function (data) {
                    res = "";
                    for (const i in data) {
                        res += `${data[i]}<br/>`;
                    }
35

36 37
                    return res;
                }
38 39 40 41 42 43
            },
            {
                data:"order",
                title:"Ref."
            },

44 45 46 47 48 49
            {data:"p_nb", title:"Nombre de réfs", width: "10%", className:"dt-body-center"},
            {
                data:"inventory_status",
                title:"Inventaire à faire",
                className:"dt-body-center",
                width: "15%",
50
                render: function (data) {
51 52 53 54 55
                    if (data == '')
                        return "<button class='btn--primary do_inventory'>Inventaire en rayon</button>";
                    else
                        return "<button class='btn--success do_inventory'>Inventaire en réserve</button>";
                }
56 57 58 59 60 61 62 63
            },
            {
                data: null,
                defaultContent: delete_icon,
                orderable: false,
                width: "30px",
                className: 'action',
                targets:   0
64 65 66 67 68 69 70 71 72 73 74 75 76 77
            }
        ],
        dom: 'rtip',
        order: [
            [
                1,
                "asc"
            ]
        ],
        iDisplayLength: 25,
        language: {url : '/static/js/datatables/french.json'}
    });
}

78 79
var rowGetData = function(clicked) {
    var row = shelfs_table.row(clicked.parents('tr'));
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    return row.data();
};

function deleteRow() {
    var clicked = $(this),
        data = rowGetData(clicked);

    openModal(
        'Etes-vous sûr de vouloir supprimer la liste du ' + data.datetime_created+ ' ?',
        function() {
            post_form(
                '/inventory/delete_custom_list',
                {id: data.id},
                function(err, result) {
                    closeModal();
                    if (!err) {
                        if (typeof result.success !== "undefined" && result.success == true) {
                            clicked.parents('tr').remove();
                            alert("Liste détruite");
                        } else {
                            alert(JSON.stringify(result));
                        }

                    } else {
                        console.log(err);
                    }


                }
            );
        },
        'Oui'
    );
}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
function go_to_inventory() {
    openModal();

    var clicked = $(this);
    var row_data = shelfs_table.row(clicked.parents('tr')).data();

    // Use local storage to pass data to next page
    if (Modernizr.localstorage) {
        var stored_list = JSON.parse(localStorage.getItem('custom_list_' + row_data.id));

        // Set local storage if key doesn't exist
        if (stored_list == null) {
            localStorage.setItem("custom_list_" + row_data.id, JSON.stringify(row_data));
        }
    }

    document.location.href = "custom_list_inventory/" + row_data.id;
}

$(document).ready(function() {
    shelfs_table = init_datatable();

138 139 140
    $(document).off('.do_inv, .del_row')
        .on('click.do_inv', 'button.do_inventory', go_to_inventory)
        .on('click.del_row', '.fa-trash', deleteRow);
141
});