combo-tree icon indicating copy to clipboard operation
combo-tree copied to clipboard

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

Open mkmukesh892 opened this issue 4 years ago • 1 comments

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.

mkmukesh892 avatar Sep 09 '20 05:09 mkmukesh892

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;
+    }
+  };
+

nmz787 avatar Mar 15 '22 08:03 nmz787