gyrocode / jquery-datatables-checkboxes

Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table.
https://www.gyrocode.com/projects/jquery-datatables-checkboxes/
MIT License
150 stars 61 forks source link

Feature - provide prepopulated array of selected rows on initialization. #144

Open JBMiller opened 1 year ago

JBMiller commented 1 year ago

Related to Issue https://github.com/gyrocode/jquery-datatables-checkboxes/issues/62

mpryvkin commented 1 year ago

Thank for your submission!

In general, implementing checkboxes with server-side processing is not a trivial matter because only one page data is available at one time.

Where are you planning to get data for initialSelectionArray parameter? Since the data comes from the server, I assume you would want checked items IDs to be coming from the server as well. However in your implementation it is provided on the client-side.

JBMiller commented 1 year ago

Fair point. I'll provide more context.

I work in ASP.NET MVC. The context is that I have an admin panel form that creates/edits a thing (let's call it a Special Offer/Promotion) and that thing can be associated to multiple other things (let's say a product. There is a database table of products and this offer can apply to several of them). When loading the form to edit an Offer, it is going to also include a paginated, searchable, server-side-ajax-powered table of products that you can select from, that indicates which products the offer applies to. When editing, however, preexisting associations should be checked off initially, to give the admin user the option to dissociate (uncheck) or to simply see what is already associated while making changes.

Where are you planning to get data for initialSelectionArray parameter?

From the server side, which is passed to the front end (a cshtml file in this case) which can accept a model (a data object) from the server that aids in crafting the html, css and javascript that is ultimately rendered to the end-user as the initial page-load. In cshtml, it will look something like this:

'initialSelectionArray': [ @Html.Raw(string.Join(",", Model.PreselectedIdArray.Select(id => "'" + id + "'"))) ]

Which, given the PreselectedIdArray contained values [34,35], it would render as this:

'initialSelectionArray': [ '34', '35' ]

Here is a more conclusive example:

$('#example').DataTable({
    'columnDefs': [
        {
            'targets': 0,
            'checkboxes': {
                'selectRow': true,
                'initialSelectionArray': [ '34', '35' ]
            }
        }
    ],
    // ...
});

I believe other frameworks have this capability, however, I cannot personally attest to them as I solely program in ASP.NET MVC C#.

Since the data comes from the server, I assume you would want checked items IDs to be coming from the server as well. However in your implementation it is provided on the client-side.

My use-case does not cover the case where the paginated data from the ajax request dictates which rows should be selected. Having the server dictate which rows should be selected will clash with the client side changes the admin user would be trying to make anytime the table page is refreshed. Thus, the server-side pagination should never send back which rows should be selected. At least within my use-case, deciding which rows should be selected should be a separate data source that applies its information only on initialization.