Skip to content

Help or Feature request: user defined pass through variable for onChange #749

Open
@brucejo75

Description

@brucejo75

I have been looking through the documentation and have not found a way that I can declare a pass through variable that will be passed to the onChanged callback during a change event. (maybe it is there?)

The interface assumes that there will be a separate onChanged callback instanced for each call. Adding a pass through variable would allow a single onChanged callback to service multiple multiselect instances.

Scenario / Suggestion

I have a table of users, for each user I want to display a multiselect that has the same option values for each user.

I can set up a single callback that works for all users. But I need some user information in the callback (e.g. user ID). It would be nice to have an optional pass through variable I can set at config time.

Here is some pseudo demonstrating the idea:


for Each user {
  let configOptions = {
        'onChangePassthru': {'_id': user._id},
        'onChange': callBack
  };

  $('select').multiselect(configOptions);

  function callBack(option, checked, onChangePassthru) {
    // do stuff with onChangePassthru, e.g. set this users checkbox state in a DB
    Users.update({'_id': onChangePassthru._id}, ... "set values")
  }

This way I can use one instance of the onChange callback.

Workaround

I make this work today by instancing an onChange callback for each user and using closure I can set variables within the functions. Something like this:

function () {
    // userId will be available in the onChange callback
    let userId = this._id;
    callBacks[userId] = {};
    callBacks[userId]['function'] = function onChange(option, checked) {
      let index = $(option).val();
      console.log('Changed option ' + index + ' checked: ' + checked +
        ' _id ' + userId);
    };
}

for Each user {
  let configOptions = {
        'onChange': callBack[this._id].function
  };

  $('select').multiselect(configOptions);

This works fine at the expense of instantiating a copy of the onChange callBack for each instance. It would be simpler and more efficient to support a pass through variable that the onChange callBack can reference.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions