Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal of setSelection method when parent and child have the same id for dynamic data? #41

Open
mkmukesh892 opened this issue Sep 9, 2020 · 1 comment

Comments

@mkmukesh892
Copy link

Please see the attached sample data for the understanding purpose:-

var SampleJSONData = [
    {
        id: 0,
        title: 'Horse'
    }, {
        id: 1,
        title: 'Birds',
		isSelectable: false,
        subs: [
            {
                id: 10,
                title: 'Pigeon',
				isSelectable: false
            }, {
                id: 1,
                title: 'Parrot'
            }, {
                id: 12,
                title: 'Owl'
            }, {
                id: 13,
                title: 'Falcon'
            }
        ]
    }];

Here you can see that for time being if id:1 with the title: Birds, and again their children with id:1 with the title 'Parrot', I have done manually since In my project I am using dynamic data that is coming from the backend. And If I am giving setSelection(['1']), then it is only selected the parent but In my case, I want to select always their child. And Parent is no more selected.

How I can achieve this? It is a great help.

@nmz787
Copy link

nmz787 commented Mar 15, 2022

I had a similar situation, except I had no data IDs, so I just created this setSelectionByText method, which takes a list of the text strings you would find in each level of hierarchy. So in your case, to select Parrot you would pass setSelectionByText(['Birds', 'Parrot'])

I have only been testing this for an hour or so... so it might have bugs or corner-cases I didn't code for.

Here is the patch to the JS file:

 
+  ComboTree.prototype.setSelectionByText = function(listOfTexts){
+    let results = this.findSelectionByText(this._elemDropDownContainer, listOfTexts);
+    if (results.length==1) {
+        this.singleItemClick(results[0].children('span')[0]);
+    }
+  };
+
+  ComboTree.prototype.findSelectionByText = function(container, listOfTexts){
+    let _this = this;
+    let text_of_interest = listOfTexts[0];
+    listOfTexts = [...listOfTexts];
+    listOfTexts.shift();
+    let results = [];
+    let kids = $(container).children('ul').children('li:contains("' + text_of_interest + '")');
+    if (listOfTexts.length>0) {
+        kids.each(function(index) {
+            results.push(_this.findSelectionByText(this, listOfTexts));
+        });
+        return results;
+    } else if (kids.length==1){
+        return kids;
+    }
+  };
+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants