Multi-item editing - How to limit selectable (and editable) columns?
Multi-item editing - How to limit selectable (and editable) columns?
 dlanz38            
            
                Posts: 17Questions: 4Answers: 0
dlanz38            
            
                Posts: 17Questions: 4Answers: 0            
            Link to test case:
http://live.datatables.net/zesokoko/1/edit
Modified from:
https://editor.datatables.net/examples/advanced/multiItem.html
Debugger code (debug.datatables.net):
N/A
Error messages shown:
https://datatables.net/manual/tech-notes/11
Description of problem:
When using multi-item editing, how would you limit an entire column from being selectable (and subsequently editable)? For example, if I didn't want "Salary" to be editable in the example? Main focus is on preventing the editing of the column, by extension I'd also be fine with preventing the selection of the column. Selection is fine, so long as the user can't edit.
I realize that if selecting a row(s) to edit, I can achieve the behavior by omitting the "Salary" column in the editor.fields definition. Like this:
editor = new $.fn.dataTable.Editor( {
        table: "#example",
        fields: [ {
                label: "Name:",
                name: "name"
            }, {
                label: "Position:",
                name: "position"
            }, {
                label: "Office:",
                name: "office"
            }, {
                label: "Age:",
                name: "age"
            }, {
                label: "Start date:",
                name: "start_date",
                type: "datetime"
            }
           /* No entry for salary */
        ],
        formOptions: {
            main: {
                scope: 'cell' // Allow multi-row editing with cell selection
            }
        }
    } );
However, with this configuration, if I select by column and chose the "Salary" column - this error occurs: https://datatables.net/manual/tech-notes/11
Which makes sense, as the field doesn't exist in the editor.
I couldn't find a built in way to enable/disable "editability" or "selectability" though, the best I could come up with was hooking into the onSelect event outlined here: https://datatables.net/extensions/select/examples/api/events.html
Is that the best (only) way to achieve this behavior?
Many thanks, and much appreciation!
Answers
Note: I realize the "edit" functionality doesn't work at all on the custom example due to there being no
DT_RowIdspecified for any of the rows. The only difference with the on site example is that there is nofieldselement for "Salary".Also came across these two, which seem to do the job as well.
https://editor.datatables.net/reference/option/fields.submit
https://editor.datatables.net/reference/option/fields.multiEditable
So I think I answered my own question. But if there is another method, I sure am interested!
Thanks you all!
Though, using the
submitandmultiEditableoptions are a little misleading to the user. It appears as if they can edit these fields, but behind the scenes nothing is changing.Hooking into the onSelect event might be best. But like I said, I'm open ears for other methods.
https://datatables.net/extensions/select/examples/api/events.html
You can use
select.selectorto specify what can, and cannot, be selected. The last example on that page shows how the last column can't be selected.Colin
Thank you @colin, that's a much better approach. However, I noticed when selecting by row, the user must select by one of the fields specified by
select.selector. This makes sense given the purpose ofselect.selector. But it might be a little confusing to the user when their selecting by row doesn't always work, but other times it does. But to be honest, the best I can come up with to address this is to allow an instance ofselect.selectorto be specified for each selection type (Row, Column, Cell). And I'm not even sure how practical that is.I'll just try to add some clarification elsewhere in my current application to shore this up for now. Many Thanks!
I just want to add to this to help someone in the future.
The method I mentioned of not defining the non-editable fields in the
editorinstance is not the correct way to go about it. There is thereadonlyfield type which should be used instead. And if necessary, you can usefields.submitto prevent the field from being passed for update.Example: