//create editable dataTable of buy rules var editor; $(document).ready(function() { editor = new $.fn.dataTable.Editor( { table: '#editBuyRuleTable', //needs to match container id on html page idSrc: 'StrategyFilterId', //field to serve as a unique row id fields: [ { label: "StrategyFilterId", //should match table column heading 'th' on html page name: "StrategyFilterId", //should match SQL field name type: "hidden" //don't show column when rendering table }, { label: "", name: "AndOr" }, { label: "Category / Type", name: "FilterCategoryAndTypeName" }, { label: "Rule Description", name: "FilterDescription" }, { label: "FilterElementCategoryId", name: "FilterElementCategoryId", type: "hidden" }, { label: "FilterElementTypeId", name: "FilterElementTypeId", type: "hidden" }, { label: "FilterElementId", name: "FilterElementId", type: "hidden" }, { label: "FormulaId", name: "FormulaId", type: "hidden" }, { label: "FilterCondition", name: "FilterCondition", type: "hidden" }, { label: "FilterValue", name: "FilterValue", type: "hidden" }, { label: "FilterValueType", name: "FilterValueType", type: "hidden" }, { label: "FilterGroup", name: "FilterGroup", type: "hidden" }, { label: "FilterBuySellType", name: "FilterBuySellType", type: "hidden" }, { label: "FilterElementParm1", name: "FilterElementParm1", type: "hidden" }, { label: "FilterElementParm2", name: "FilterElementParm2", type: "hidden" }, { label: "FilterElementParm3", name: "FilterElementParm3", type: "hidden" }, { label: "FilterElementParm4", name: "FilterElementParm4", type: "hidden" }, { label: "FilterElementParm5", name: "FilterElementParm5", type: "hidden" }, { label: "FilterValueParm1", name: "FilterValueParm1", type: "hidden" }, { label: "FilterValueParm2", name: "FilterValueParm2", type: "hidden" }, { label: "FilterValueParm3", name: "FilterValueParm3", type: "hidden" }, { label: "FilterValueParm4", name: "FilterValueParm4", type: "hidden" }, { label: "FilterValueParm5", name: "FilterValueParm5", type: "hidden" }, { label: "FilterUnits", name: "FilterUnits", type: "hidden" } ] }); // select fields for display 'show' on the 'create' form (i.e., add a new row) editor.on( 'initCreate', function () { editor.field( 'StrategyFilterId' ).hide(); //should be 'field name' not 'field label' editor.field( 'AndOr' ).hide(); //alt: .show() editor.field( 'FilterCategoryAndTypeName' ).hide(); editor.field( 'FilterDescription' ).hide(); editor.field( 'FilterElementCategoryId' ).hide(); editor.field( 'FilterElementTypeId' ).hide(); editor.field( 'FilterElementId' ).hide(); editor.field( 'FormulaId' ).hide(); editor.field( 'FilterCondition' ).hide(); editor.field( 'FilterValue' ).hide(); editor.field( 'FilterValueType' ).hide(); editor.field( 'FilterGroup' ).hide(); editor.field( 'FilterBuySellType' ).hide(); editor.field( 'FilterElementParm1' ).hide(); editor.field( 'FilterElementParm2' ).hide(); editor.field( 'FilterElementParm3' ).hide(); editor.field( 'FilterElementParm4' ).hide(); editor.field( 'FilterElementParm5' ).hide(); editor.field( 'FilterValueParm1' ).hide(); editor.field( 'FilterValueParm2' ).hide(); editor.field( 'FilterValueParm3' ).hide(); editor.field( 'FilterValueParm4' ).hide(); editor.field( 'FilterValueParm5' ).hide(); editor.field( 'FilterUnits' ).hide(); }); // select fields for display 'show' on the 'edit' form (i.e., edit a row) editor.on( 'initEdit', function () { editor.field( 'StrategyFilterId' ).hide(); editor.field( 'AndOr' ).hide(); editor.field( 'FilterCategoryAndTypeName' ).hide(); editor.field( 'FilterDescription' ).hide(); editor.field( 'FilterElementCategoryId' ).hide(); editor.field( 'FilterElementTypeId' ).hide(); editor.field( 'FilterElementId' ).hide(); editor.field( 'FormulaId' ).hide(); editor.field( 'FilterCondition' ).hide(); editor.field( 'FilterValue' ).hide(); editor.field( 'FilterValueType' ).hide(); editor.field( 'FilterGroup' ).hide(); editor.field( 'FilterBuySellType' ).hide(); editor.field( 'FilterElementParm1' ).hide(); editor.field( 'FilterElementParm2' ).hide(); editor.field( 'FilterElementParm3' ).hide(); editor.field( 'FilterElementParm4' ).hide(); editor.field( 'FilterElementParm5' ).hide(); editor.field( 'FilterValueParm1' ).hide(); editor.field( 'FilterValueParm2' ).hide(); editor.field( 'FilterValueParm3' ).hide(); editor.field( 'FilterValueParm4' ).hide(); editor.field( 'FilterValueParm5' ).hide(); editor.field( 'FilterUnits' ).hide(); }); var table = $('#editBuyRuleTable').DataTable( { destroy: true, responsive: true, select: true, scrollY: "300px", scrollCollapse: true, paging: false, dom: "Bfrtip", ajax: { //gather buy rule parameters & values for active strategy url: 'api/StrategyEditorStrategySelect', data: { "StrategyId": vm.StrategyId, "Function": 'Userid' }, dataSrc: function ( json ) { return json[2]; } }, columnDefs: [ { 'targets': 0, 'render': function(data, type, row, meta){ data = '' if(row.FilterCategoryAndTypeName === ''){ data = ''; } return data; }, //the intent here is to make rows without a checkbox unselectable by assigning a user defined class to the row, it doesn't seem to be working 'createdRow': function( row, data, dataIndex ) { if ( row.FilterCategoryAndTypeName === '' ) { allowEdit$(row).addClass( 'ignore' ); } } /* tried this but it didn't work either 'createdCell': function (td, cellData, rowData, row, col){ if( row.FilterCategoryAndTypeName === ''){ this.api().cell(td).checkboxes.disable(); } } */ }, { 'targets': 0, 'className': 'text-center' }, { 'targets': 1, 'className': 'text-center' }, { 'width': 10, targets: 0 }, { 'width': 20, targets: 1 }, { 'width': 15, targets: 3 }, { 'orderable': false, 'targets': [ 0, 1, 2, 3, 4, 5 ] }, { 'targets': [ 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ], 'visible': false } ], columns: [ { // Checkbox select column //column: #0 data: null, defaultContent: '', className: '' }, { // '?' column //column: #1 data: null, render: function ( data, type, row ) { if(row.AndOr === '' && row.FilterCategoryAndTypeName === ''){ return ''; } else if (row.AndOr !== '' && row.FilterCategoryAndTypeName === '') { return ''; } else { return ''; }}, defaultContent: "", className: "" }, // utilizing the columns.data method (an array of objects) { data: "StrategyFilterId" }, //column: #2 { data: "AndOr" }, //column: #3 { data: "FilterCategoryAndTypeName" }, //column: #4 { data: "FilterDescription" }, //column: #5 { data: "FilterElementCategoryId" }, //column: #6 { data: "FilterElementTypeId" }, //column: #7 { data: "FilterElementId" }, //column: #8 { data: "FormulaId" }, //column: #9 { data: "FilterCondition" }, //column: #10 { data: "FilterValue" }, //column: #11 { data: "FilterValueType" }, //column: #12 { data: "FilterGroup" }, //column: #13 { data: "FilterBuySellType" }, //column: #14 { data: "FilterElementParm1" }, //column: #15 { data: "FilterElementParm2" }, //column: #16 { data: "FilterElementParm3" }, //column: #17 { data: "FilterElementParm4" }, //column: #18 { data: "FilterElementParm5" }, //column: #19 { data: "FilterValueParm1" }, //column: #20 { data: "FilterValueParm2" }, //column: #21 { data: "FilterValueParm3" }, //column: #22 { data: "FilterValueParm4" }, //column: #23 { data: "FilterValueParm5" }, //column: #24 { data: "FilterUnits" } //column: #25 ], order: [ 13, "asc" ], select: { style: 'os', selector: 'td:first-child' }, buttons: [ /* { extend: "create", editor: editor, formTitle: "Add a buy rule to the Strategy", formButtons: [ { label: "Cancel", fn: function () { this.close(); } }, "Add Buy Rule" ] }, */ { extend: "edit", editor: editor, formTitle: function ( editor, dt ) { // Get the data for the row and use a property from it in the form title var rowData = dt.row({selected:true}).data(); return "Edit transaction data for "+rowData.tickerId; //rowData.tickerName needs to be updated }, formButtons: [ { label: "Cancel", fn: function () { this.close(); } }, "Update Buy Rule" ] }, { extend: "selected", text: "Duplicate", action: function ( e, dt, node, config ) { // Start in edit mode, and then change to create editor .edit( table.rows( {selected: true} ).indexes(), { title: "Duplicate Buy Rule", buttons: "Create a Copy" } ) .mode( "create" ); } }, { extend: "remove", editor: editor, formMessage: "Are you sure you want to delete this buy rule?", formTitle: function ( editor, dt ) { // Get the data for the row and use a property from it in the form title var rowData = dt.row({selected:true}).data(); return "Delete "+rowData.tickerName; //rowData.tickerName needs to be updated }, formButtons: [ { label: "Cancel", fn: function () { this.close(); } }, "Delete Buy Rule" ] } ], }); //the intent here is to deselect a row based on a user defined class having been attached to a row on initialization (see columnDefs: above) table.on('select', function(e, dt, type, indexes) { if (type === 'row') { var rows = table.rows(indexes).nodes().to$(); $.each(rows, function() { if ($(this).hasClass('ignore')) table.row($(this)).deselect(); }) } }); }); // end of $(document).ready