Unable to retrieve child node

Asked 1 years ago, Updated 1 years ago, 60 views

We have modified the jquery Bonsai plug-in to create the original jquery plug-in.

https://github.com/14021939/dropdownCheckCombobox

dropdownCheckCombobox.js (line 96) cannot retrieve this.bonsai child node (Object[] in console output).

I'm really sorry for the round throw, but I don't know why at all.Help me.

javascript jquery

2022-09-29 22:24

1 Answers

When I set the breakpoint in the browser developer tool, I saw the following:

ss

There is no problem with how to retrieve it, and there is actually no child element of this.bonsai at that time.However, once you have run the script, you can see that lists and check boxes are generated there, which means that the problem is that the above action is done before the check box is generated.

DropdownCheckCombobox.prototype.createBonsai is as follows:

this.bonsai=listTag.jsonList({
  url —self.options.dataUrl,
  onListItem: function(event, listItem, data, isGroup) {
    if(!isGroup) {listItem.attr('data-value', data.id);}
  },
  onSuccess:function(event,jsonList){
      return$(this.el).find('>ol').bonsai({
        checkboxes:true,
        createInputs: 'checkbox',
        handleDuplicateCheckboxes: true,
        expandAll —true
      });
    }
});
This.handleCombobox();

jsonList() starts loading url asynchronously and returns itself immediately.Then, when the loading is complete, onSuccess is performed.Therefore, $(this.el).find('>ol').bonsai() runs after proper loading, while this.handleCombobox() runs without waiting for loading.

Also, the return value of onSuccess does not seem to be used, so there is no point in returning bonsai().

In summary, bonsai() must be run in jsonList() onSuccess and then handleCombobox().Is it like this?

this.bonsai=listTag.jsonList({
  url —self.options.dataUrl,
  onListItem: function(event, listItem, data, isGroup) {
    if(!isGroup) {listItem.attr('data-value', data.id);}
  },
  onSuccess:function(event,jsonList){
      $(this.el).find('>ol').bonsai({
        checkboxes:true,
        createInputs: 'checkbox',
        handleDuplicateCheckboxes: true,
        expandAll —true
      });
      self.handleCombobox();
    }
});

Note that onSuccess uses self because this is different.


2022-09-29 22:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.