mardi 2 avril 2019

Jquery trigger select-checkboxes where rows in hidden column matches list

I have a table of >400 items for my users to choose from. There are a few sets of approximately 200 items that are fairly common to be selected together depending on the user. So I want to make some buttons that automatically select the bulk of those items for the user.

In this case, I want to make the select default library button trigger the selection of the rows with ID 4, 7, and 8.

I have added a hidden ID row, but I am not sure how to trigger the select function for a list of ID's.

Fiddle: https://jsfiddle.net/crashvector/b9r0Lpvg/2/

Jquery code:

$(document).ready( function () {    
var srcData = [
    {
        "ID": "1",
        "Name": "01_p11017",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "2",
        "Name": "02_p21017",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "3",
        "Name": "03_p31017",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "4",
        "Name": "04_p41017",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "5",
        "Name": "05_p11117",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "6",
        "Name": "06_p21117",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "7",
        "Name": "07_p31117",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    },
    {
        "ID": "8",
        "Name": "08_p41117",
        "Region": "GoM",
        "Class": "unknown",
        "Category": "unknown",
        "QC_flag": "0",
        "QC_comment": "\r"
    }];
  var table = $('#samples').DataTable( { 
    "processing": true,
    "serverSide": false,
    "pageLength": -1,
    "lengthMenu": [[100, 250, 500, -1], [100, 250, 500, "All"]],
data: srcData,    
    columns: [
      {
        data: '',
        defaultContent: '0',
        visible: false
      },
      {
        data: '',
        defaultContent: '',
        orderable: false,
        className: 'select-checkbox',
        targets: 1,
        orderData: [0]
      },
      {
        title: 'ID',
        'className': 'dt-left',
        'visible': false,
        data: 'ID'
      },
      {
        title: 'Name',
        'className': 'dt-left',
        data: 'Name'
      },
      {
        title: 'Region/Program',
        'className': 'dt-left',
        data: 'Region'
      },
      {
        title: 'Class',
        'className': 'dt-left',
        data: 'Class'
      },
      {
        title: 'Category',
        'className': 'dt-left',
        data: 'Category'
      },
      {
        title: 'QC Concerns',
        'className': 'dt-left',
        data: 'QC_comment'
      }
    ],
    select: {
        style:    'multi',
        },
    order: ([[ 4, 'asc' ], [ 5, 'asc' ], [ 3, 'asc' ]]),
    orderFixed: [0, 'desc'],
    dom: 'Bfrtip',
    buttons: [
     {
            extend: 'csv',
            fieldBoundary: '',
            text: '<span class="fa fa-file-excel-o"></span> Download (ALL) or (SELECTED)',
            exportOptions: {
                columns: [ 5, 2 ],
                modifier: {
                    search: 'applied',
                    order: 'applied'
                }
            }
        },
{
      text: 'Select Default Library 1',
      action: function (e, dt, node, conf) {
        alert('This button will automatically check all rows that match predefined list 1 using the hidden ID column. (IE ID 4, 7, 8)');
      }
    }

     ]
  } );

  table.on( 'select', function ( e, dt, type, indexes ) {
    if ( type === 'row' ) {
        var row = table.row( dt );
        table.cell({ row: row.index(), column: 0 } ).data( '1' );
        table.draw();
    }
  });

  table.on( 'deselect', function ( e, dt, type, indexes ) {
    if ( type === 'row' ) {
        var row = table.row( dt );
        table.cell({ row: row.index(), column: 0 } ).data( '0' );
        table.draw();
    }
  });


} );




Aucun commentaire:

Enregistrer un commentaire