What is the code to programmatically select a checkbox in an ExtJS treepanel when a button is clicked?

I am working with an Extjs treepanel that has several layers added to it. Additionally, I have a search panel with a search button. Clicking on the search button displays the results in an extjs gridview.

My goal is to show layers by clicking on a row in the gridview and dynamically or programmatically set the checkbox of the treepanel to true. Here is how I added the tree panel:

var layerTree = new Ext.tree.TreePanel({
    border: true,
    root: layerList,
    split: true,
    containerScroll: true,
    collapseMode: "mini",
    autoScroll: true,
    loader: new Ext.tree.TreeLoader({
        applyLoader: false,
        uiProviders: {
            "layernodeui": LayerNodeUI
        }
    }),
    root: {
        nodeType: "async",
        children: Ext.decode(treeConfig)
    },
    listeners: {
        "radiochange": function(node) {}
    },
    rootVisible: false,
    lines: false

});

I want to achieve something like layernode.checked=true

Thank you Akatum for your suggestion:

In your grid configuration, you need to set up a listener for the itemclick event, which is triggered when a user clicks on a row in the grid. In this listener, you can retrieve information about the layer from the row record. Then, you can locate the layer node in the tree using the getNodeById method from Ext.data.TreeStore and set its checked attribute to true.

So, in your grid configuration, you would have:

listeners: {
    itemclick: function(c, record) {
        // get layer id from clicked row record
        var layerId = record.get('layerId');

        // find layer node in tree by layer id
        var node = layerTree.getStore().getNodeById(layerId);

        // set node checked attribute to true
        node.set('checked', true);
    }
}

I attempted to implement your code, but it doesn't seem to be working. I integrated your suggested code into my existing code like this:

var grid = new Ext.grid.GridPanel({
     store: new Ext.data.JsonStore({
         fields: action.result.fields,
         listeners: {
             load: function() {
                 setTimeout(function() {
                     grid.getSelectionModel().selectFirstRow();
                 }, 100);
             }
         }
     }),
     frame: true,
     columns: action.result.columns,
     stripeRows: true,
     listeners: {
         itemclick: function(c, record) {
             var layerId = record.get('layerId');
             var node = layerTree.getStore().getNodeById(layerId);
             node.set('checked', true);
         }
     }
 });

Answer №1

To properly configure your grid, make sure to incorporate a listener for the itemclick event. This event triggers when a user clicks on a row within the grid. Within this listener, extract information about the layer from the row record. Subsequently, locate the layer node in the tree using the Ext.data.TreeStore getNodeById method and update its checked attribute to true.

Your grid configuration should include:

listeners: {
    itemclick: function(c, record) {
        // Obtain the layer id from the clicked row record
        var layerId = record.get('layerId');

        // Locate the layer node in the tree based on the layer id
        var node = layerTree.getStore().getNodeById(layerId);

        // Set the node's checked attribute to true
        node.set('checked', true);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Where am I going wrong in my attempts to use a callback function?

I am currently attempting to implement a callback function for this particular JavaScript function. function Filtering_GetSite(siteElement) { $.ajax({ type: "POST", url: "samle.asmx/f1", data: "", contentType: "application/json; charset= ...

Using Browserify to fetch external JSON data

I need some advice on the most effective method for retrieving external JSON data. Currently, I am using browserify and importing JSON data like this: const data = require('mydata.json'). However, I want to avoid having to recompile the browser ...

Tips for invoking a JavaScript function script through PHP

<?php echo "<script type='text/javascript'>error_warning('Error!'); </script>"; ?> <!DOCTYPE html> <html stuff> <div id="alert_box" class="alert"></div> < ...

Working on rectifying the Chat Engine API code that was causing a 403 Status Code to be generated

Encountering a status code 403 while attempting to create a chat engine IO page, even though all authentication headers are believed to be accurate. Double-checked for typos but still unable to identify the issue. Despite console logging the user correctly ...

Get early glimpses at alterations to ASP.NET Core websites using the IIS application preview

I'm currently working on a personal project using ASP.NET Core and utilizing IIS for easier testing and making quick adjustments by simply refreshing the page. However, I'm having trouble finding a guide online that covers the development process ...

Steps for choosing the nth HTML row with jQuery

I'm facing a situation where I need to be able to select the nth row of an HTML table based solely on the id of the selected row. You can see exactly what I mean by checking out this JSFiddle Demo <table class="mytable1"> <tr><td i ...

Issue with infoWindow flickering and closing automatically on Google Maps API due to a mouseout event-triggered action

Currently, I am working on a project where I am creating polygons. The issue I am facing is that whenever the infoWindow is hovered over, the mouseout event on the polygon is triggered. I would like to prevent the mouseout event from firing unless the mo ...

Upgrade the jQuery code from version 1.4.2 min to version 1.10.2 min

Hey there, I'm looking for assistance with updating the code below to use jQuery 1.10.2. The backslashes are present because I am using it with PHP and need to escape special characters. I'm not very familiar with JavaScript and unsure of the ch ...

Populating inputs dynamically in React.js to receive values

Having recently transitioned to React from React Native, I am faced with a new challenge. I have an array of objects, let's say it contains 5 items. I used the map function to populate views based on the amount of objects, resulting in 5 headings and ...

The script generated by document.write is failing to function as expected

A completed JQuery script has been created to enable the movement of a 360° object (picture) based on mouse actions. The code is functional: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> ...

What steps should I take to have a button initiate an AJAX request?

One of the tasks on my list involves a textbox and a button for interaction. Once text is entered into the textbox, I intend to trigger an AJAX request by clicking the button. The purpose of this AJAX call is to extract the text input and incorporate it i ...

Having trouble figuring out how to load images into a div based on the current page location?

Looking for a solution to my problem - I have a navigation bar with a fixed div (A) on the far right side. The nav bar remains at the top of the page. There are 6 sections in the body of the page, each being 1200px tall divs. What I want is for a different ...

Adding text chips to a text field in Vuetify - A simple guide

I have successfully integrated a text field with vuetify and now I am looking to incorporate chips into it. Currently, chips are only added if the entered text matches a specific pattern (such as starting with '{' and ending with '}'). ...

Ways to determine the presence of an alert using JavaScript or Selenium

I am currently developing a bot that performs constant searches. However, I have run into an issue where sometimes an alert requesting access to the user's location pops up. Is there a way for me to detect the presence of this alert or even disable it ...

Can I upload both a file and additional data using jQuery's Ajax post method?

Below are the necessary form data that needs to be posted using an AJAX request in order to receive a JSON response. <textarea type='text' id="newStatusBox">Your Status here...</textarea> Link:<input type="text" id="newStatusLink" ...

State update failing to modify arrays

Shown below is an array that contains boolean values: const [state, setState] = React.useState({ [`${"checkedA"+index}`]: false, [`${"checkedB"+index}`]: false, [`${"checkedC"+index}`]: false, [`${"checkedD"+index}`]: false, }); ...

A JavaScript promise that triggers the "then" function right away

As I work on developing a single-page app, JavaScript promises have become a critical component. In specific scenarios, I require the "then" method to execute synchronously if the promise is already resolved. To address this need, I have created a custom p ...

Unable to activate focus() on a specific text field

It's quite peculiar. I'm working with a Sammy.js application, and my goal is to set the focus on a text field as soon as the HTML loads. Here's the CoffeeScript code snippet I've written: this.partial('templates/my-template.jqt&ap ...

Delving Into the Mysteries of Bootstrap Modal Fading

After updating our Bootstrap plugins file to version 2.2.1, I encountered an issue with adding the 'fade' class to modals. Previously, we had been using data-* api references for modals but decided to switch over. However, when I tried to incorpo ...

Is there a way to adjust the width of the datepicker on my device?

I've been attempting to adjust the width of the date picker, but I'm having trouble achieving it. Below is my code: import React from "react"; import ReactDOM from "react-dom"; import { DatePicker, RangeDatePicker } from &qu ...