Exclude column for row selection

Exclude column for row selection

jigar311982jigar311982 Posts: 70Questions: 31Answers: 0

Hello,

I have below code to get value of row and then perform some AJAX on this, works perfectly fine,


$(document).on('click', '#expensesTable tbody tr', function () { var rowdata = $('#expensesTable').DataTable().row(this).data(); jQuery.ajax({ url: "/purchases/edit_expense/" + rowdata.expense_id, type: "POST", //data: {expense_id:rowdata.expense_id}, success:function(data){ $data = $(data); // the HTML content that controller has produced //. This will open data container. } }); });

Same row have first column as check box,

            headerCallback: function(thead, data, start, end, display) {
                thead.getElementsByTagName('th')[0].innerHTML = `
                    <label class="checkbox checkbox-single checkbox-solid checkbox-primary mb-0">
                        <input type="checkbox" value="" class="group-checkable"/>
                        <span></span>
                    </label>`;
            },
        table.on('change', '.group-checkable', function() {
            var set = $(this).closest('table').find('td:first-child .checkable');
            var checked = $(this).is(':checked');

            $(set).each(function() {
                if (checked) {
                    $(this).prop('checked', true);
                    table.rows($(this).closest('tr')).select();
                }
                else {
                    $(this).prop('checked', false);
                    table.rows($(this).closest('tr')).deselect();
                }
            });
        });

Now, what I want is, clicking on row run AJAX as I am doing, and selecting checkbox to run some another function,
Currently even selecting Checkbox, it runs this AJAX two times,
So in short, how can i exclude specific checkbox column for row click?

Answers

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

    You could make the selector more specific in the first code block to only trigger on the td, or possibly use a global variable to determine if the selection was caused by the final code block, and therefore don't trigger the ajax in that first block,

    Colin

  • jigar311982jigar311982 Posts: 70Questions: 31Answers: 0
    edited January 2021

    Thanks Colin for hints,
    Adjusted code with below and worked,

    $(document).on('click', '#expensesTable tbody td:not(:first-child)', function () {
    
    var rowdata = $('#expensesTable').DataTable().row($(this).parents('tr') ).data();
    
    
This discussion has been closed.