DataTables Editor: Select2 Plugin - ?initialValue:true&value=%22%22 should not be sent for a create.

DataTables Editor: Select2 Plugin - ?initialValue:true&value=%22%22 should not be sent for a create.

washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

If I understand correctly, when the editor opens (edit event, not create), we must essentially go ask the server for the dropdown item given a specific Id.

On a create however, this should not be the case. In fact, during a create this is what is fired off:

http://localhost:53112/api/Contacts?initialValue=true&value=%22%22

This means, I have to add additional code to my server to handle a blank value, even though we are saying "initialValue=true". Whats more, I am using a rest API and I really wish I could just do http://localhost:53112/api/Contacts/1234

How can we get the plugin updated to remedy this? Can I override this?

This question has an accepted answers - jump to answer

Answers

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Additionally, Each of my rows already contains the data desired by needAjax. IE each row has Contact and ContactId. Ideally, I would like to tap into the data I already have.

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2
    edited May 2018

    Sorry to keep dumping text here, but I have started to modify the select plugin to work for my special cases and I noticed set: does not give me the full row data, only the val. Why is this? How do I take what I have (conf and val) and get the table row from this?

    EDIT because I cannot delete. Define field.data and that will become val giving you access to everything needed.. I will post here shortly with my solution.

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Solution:

    Replace the ajax call if(needAjax) with this:

                                var initialValue = conf.initialValueMapper(val);
    
                                console.log(initialValue)
    
                                var addOption = function (option) {
                                    if (conf._input.find('option[value="' + option.id + '"]').length === 0) {
                                        $('<option/>')
                                            .attr('value', option.id)
                                            .text(option.text)
                                            .appendTo(conf._input);
                                    }
                                }
    
                                if ($.isArray(initialValue)) {
                                    var initialValueOptionValues = [];
                                    for (var i = 0, ien = initialValue.length; i < ien; i++) {
                                        addOption(initialValue[i]);
                                        initialValueOptionValues.push(initialValue[i].id);
                                    }
                                    conf._input.val(initialValueOptionValues);
                                }
                                else {
                                    addOption(initialValue);
                                    conf._input.val(initialValue.id);
                                }
    
                                conf._input.trigger('change', { editor: true });
    

    To use it, make sure your field has "data" defined, and also on fields define the following:

                        "initialValueMapper": function (val) { 
                            return {
                                id: val.Id,
                                text: `${val.Name} - ${val.EmailAddress}`
                            }
                        },
    

    This function lets you build the option object { id: .., text:.. } with whatever data from "data". So if your data is in the row, you have full control over how it is displayed. Not yet tested with multiple.

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    On a create however, this should not be the case. In fact, during a create this is what is fired off:

    Unless of course you have a default value defined, in which case you absolutely would need this.

    This sounds like one of those cases where it is useful to modify the code to suit your needs :).

    Allan

  • system.bonn@gmail.comsystem.bonn@gmail.com Posts: 28Questions: 4Answers: 1
    edited November 2019

    wrong post, sorry

This discussion has been closed.