简体   繁体   English

ExtJs 树面板 如何更改节点复选框的背景图像?

[英]ExtJs Tree Panel how do I change a node's checkbox's background image?

First of all I'm using Sencha v4.0.0.首先我使用的是 Sencha v4.0.0。

I have a tree panel with two checkboxes (a parent and a child) as you can see in the code snipet below:我有一个带有两个复选框(父级和子级)的树形面板,您可以在下面的代码片段中看到:

{
            xtype: 'treepanel',
            id: 'treepanelId',
            name: 'treepanelName',
            cls: 'myCustomCSS',
            width: 200,
            height: 49,
            rootVisible: false,
            store: {
                root: {
                    expanded: true,
                    children: [{
                        text: "Check1",
                        name: "check1",
                        id: "check1Id",
                        checked: true,
                        expanded: true,
                        children: [{
                            text: "Check2",
                            name: "check2",
                            id: "check2Id",
                            checked: false,
                            leaf: true
                        }]
                    }]
                }
            }
        }

My school assignment app has two kinds of checkbox images that I have to be able to change depending on whether a checkbox is checked or not.我的学校作业应用程序有两种复选框图像,我必须能够根据是否选中复选框来更改它们。 Let's say that the checked image has a blue border and the other a red border.假设检查的图像有一个蓝色边框,另一个是红色边框。

This is the default css class that I use in the cls option in the tree panel:这是我在树面板的cls选项中使用的默认 css class:

.myCustomCSS{
    .x-tree-checkbox {
        background-image: url(../path/checkbox-blue.png) !important;
    }
}

The problem with my implementation is that the way it currently is, for both checkbox states (checked or unchecked) only the image with the blue border is shown because the cls option is setting the background image for all checkboxes in the tree panel.我的实现的问题在于它当前的方式,对于两种复选框状态(选中或未选中),仅显示带有蓝色边框的图像,因为cls选项正在为树面板中的所有复选框设置背景图像。

I've tried setting an afterrender listener to the tree panel but neither of my tree nodes have the addClass or removeClass methods that would otherwise allow me to dynamically change the css class.我尝试为树面板设置afterrender侦听器,但我的树节点都没有 addClass 或 removeClass 方法,否则我可以动态更改 css class。

I've also tried to set the following option in the tree panel:我还尝试在树面板中设置以下选项:

viewConfig: {
   getRowClass:function(record) {
      return "myCustomCSS-Red";
   }
}

But also with no success...但也没有成功...

Does anyone know how I can change a single node's checkbox background image dynamically?有谁知道我如何动态更改单个节点的复选框背景图像?

If you want to do this using CSS you can use the selector 'tree-checkbox-checked'.如果您想使用 CSS 执行此操作,您可以使用选择器“tree-checkbox-checked”。

So a CSS could look like this所以 CSS 可能看起来像这样

.x-tree-checkbox {
    background-image: url("https://findicons.com/files/icons/2711/free_icons_for_windows8_metro/256/unchecked_checkbox.png");
    background-size: cover;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: right;
    background-color:#db7b8c;
}

.x-tree-checkbox-checked {
    background-image: url("https://findicons.com/files/icons/2711/free_icons_for_windows8_metro/256/checked_checkbox.png");
    background-size: cover;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: right;
    background-color:#b7f29b;
}

Hint: I separated them completely because I didn't managed to use nested selectors in my fiddle (I'm used to SCSS).提示:我将它们完全分开是因为我没有设法在我的小提琴中使用嵌套选择器(我习惯于 SCSS)。

I created a little fiddle for this.我为此创造了一个小小提琴。 Hopefully this answers your question.希望这能回答你的问题。

https://fiddle.sencha.com/#view/editor&fiddle/3cmc https://fiddle.sencha.com/#view/editor&fiddle/3cmc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM