jeudi 3 décembre 2015

How to pass selected values to backend in checkbox table

I have a html table with checkboxes and i want to pass values of selected checkboxes back to my view.

My html table:

<table class="demo-add-niftycheck" data-toggle="table" data-toolbar="#demo-delete-row" data-search="true" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-sort-name="id" data-page-list="[25, 50, 100]" data-page-size="25" data-pagination="true" data-show-pagination-switch="true">
    <thead>
        <tr>
            <th data-checkbox="true">Order ID</th>
            <th data-align="center" data-sortable="true">Order ID</th>
            <th data-align="center" data-sortable="true">Channel</th>
            <th data-align="center" data-sortable="true">Dispatch By Date</th>
            <th data-align="center" data-sortable="true">Order Amount</th>
            <th data-align="center" data-sortable="true">Status</th>
            <th data-align="center" data-sortable="true">Product</th>
        </tr>
    </thead>
    <tbody>
        {% for oid,oiid,dbd,stts,tp,sku,qty,chnl in new_orders %}
        <tr>
            <td></td>
            <td>
                <div><input name="order_id" value="{{oid}}" style="display:none">{{oid}}</div>
                <div><input name="order_item_id" value="{{oiid}}" style="display:none">{{oiid}}</div>
            </td>
            <td><input name="channel" value="{{chnl}}" style="display:none">{{chnl}}</td>
            <td>{{dbd}}</td>
            <td>{{tp}}</td>
            .....

I want to pass value of {{oid}},{{oiid}},{{chnl}} back to my django view. The problem is when i select some rows, data from all rows is passed back to my view. How to solve this problem?

demo-add-niftcheck (table class):

$(".demo-add-niftycheck").on('post-body.bs.table', function () {
    $(this).find('input:checkbox').not('.form-checkbox input:checkbox').wrap('<label class="form-checkbox form-icon"></label>');
    $(this).find('.form-checkbox').niftyCheck();
});

I can catch data on my backend like:

order_id_list = request.POST.getlist['order_id']
order_item_id_list = request.POST.getlist['order_item_id']

table jquery:

var $table = $('#demo-custom-toolbar'), $remove = $('#demo-delete-row');

    $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
        $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
    });

    $remove.click(function () {
        var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
            return row.id
        });
        $table.bootstrapTable('remove', {
            field: 'id',
            values: ids
        });
        $remove.prop('disabled', true);
    });


});




Aucun commentaire:

Enregistrer un commentaire