Always shown checkbox dependant on another column

Always shown checkbox dependant on another column

rldean1rldean1 Posts: 141Questions: 66Answers: 1

I've had a great deal of success with Editor's "Always shown checkbox", by following an older blog, and the example here:
https://editor.datatables.net/examples/api/checkbox.html

Now I want to show/hide the checkbox, based on another field, and I'm trying to decide best how to approach that, and I don't necessarily have a full understanding of what's required for display on return

  1. what is the significance of the final return statement? Is the code below returning the HTML markup explicitly for display, and the raw data for other "types", like filtering and sorting?
  2. is a final return data; statement always required?
  3. I have a situation where I what to SHOW or HIDE a checkbox, based on an adjacent column, which I can access with row.TheOtherField
  4. Basically, if (row.TheOtherField == null) {return ''} -- don't show a checkbox. I don't want to show the raw data either.
  5. Is it better to HIDE the checkbox (Bootstrap's d-none): return '<input type="checkbox" class="editor-active d-none">';?
  6. Personally, I would rather "display nothing"

How do I display nothing? is it as simple as return ''? (I've had some weird results. Maybe I'm just tired.)

        {
            data:   "active",
            render: function ( data, type, row ) {
                if ( type === 'display' ) {
                    return '<input type="checkbox" class="editor-active">';
                }
                return data;
            },
            className: "dt-body-center"
        }

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    Answer ✓
    1. columns.render is called for various renderings - such as sort, display, type, etc. In your code snippet, it's doing something specific for the display type, everything else falls through and the data as-is returned.
    2. All types need a return data - so if they're not being handled in the code block, they'll need that fallthrough.
    3. row, the third argument, contains data for the entire row.
    4. OK
    5. That's down to your implementation, both would be valid solutions.
    6. OK

    that should work if you return '' - if it's weird, please post a test case or link to your page, and we can take a look

    Colin

This discussion has been closed.