Refreshing Treeview Control for dynamic data loading (4)

Source: Internet
Author: User
We showed the Treeview three days ago, but it is obviously not enough. The UI of Treeview also needs to have different effect changes based on the user's various operations and different attribute settings, such as actived, selected, and checked. The following describes how to design and implement the UI dynamic update of Treeview.

For control development, whether on the Web or winform, use the mouse and keyboard to trigger UI update and useProgramCodeTo trigger updates. Therefore, we 'd better not directly update the control UI in the event processing function in the design. Why? Because such an update is a one-way service (although HTML element Event Callback can be called in a program, the syntax is too poor), when we need to update the control UI in the program, for example, to set the selected status, you need to write a set of code to update the attributes of the UI element from the program perspective. In this case, we will find that the previous one-way service code can be fully included by the latter (functional. Continue with selected. When we click treenode with the mouse, we can get the event directly from the event response code. srcelement (this is the HTML element for displaying treenode). If we modify the attribute of this element to display the selected status at this time, it is very easy. However, if we still need to call treenode. setselected (true) in the program to obtain the selected effect, the code of the one-way service mentioned above will be included by the latter (setselected.

Oh, then we will implement n methods, such as setactived (bool), setselected (bool), and setchecked (bool). In these methods, we will modify the control UI element attributes separately. Because we need to reference and search back and forth between the script object and the dhmtl object in the JS control, in addition, the UI display in some States still has a priority problem (for example, both checked and selected require text color change, but the selected priority is higher than checked ). Therefore, we do not update the attributes of the UI element in every setxxx function. Instead, we use a unified function to process the UI update of the entire control. Therefore, we define an applyuichange () function to process all attributes related to the UI rendering and modify the attributes of the UI element.

The setxxx method is implemented as follows:

Treenode. Prototype. setchecked =   Function (Ischeck)
{
VaR Innercache =   This . M_tree.m_innercache;
If (Ischeck)
{
If ( ! Innercache. m_checkeds.contains ( This ))
{
Innercache. m_checkeds.add (
This );
}
}
Else
{
Innercache. m_checkeds.remove (
This );
}
This . M_checked = Ischeck;
This . Applyuichange ();
};

Treenode. Prototype. setselected
=   Function (Isselected)
{
VaR Innercache =   This . M_tree.m_innercache;
If (Isselected)
{
If ( ! Innercache. m_selecteds.contains ( This ))
{
Innercache. m_selecteds.add (
This );
}
}
Else
{
Innercache. m_selecteds.remove (
This );
}
This . M_selected = Isselected;
This . Applyuichange ();
};

They are only responsible for maintenanceControl ObjectThis. applyuichange () processes all controls UI-related operations. This design is similar to the design of invalidate () called for display after modifying properties in the winform control. All UI updates are implemented in onpaint. Innercache-related operations are not concerned here. They are used to record the checked and selected nodes in the Treeview instance.

The applyuichange code is implemented as follows:

Treenodebase. Prototype. applyuichange =   Function ()
{
If ( This . M_element.checkbox)
{
This . M_element.checkbox.checked =   This . M_checked;
}
VaR Elmtnode =   This . M_element.content;
If ( This . M_selected)
{
Elmtnode. style. Color
=   This . Styles ('selectedforecolor ');
Elmtnode. style. Background
=   This . Styles ('selectedbackcolor ');
}
Else
{
If ( This . M_checked)
{
Elmtnode. style. Color
=   This . Styles ('checkedforecolor ');
Elmtnode. style. Background
=   This . Styles ('checkedbackcolor ');
}
Else
{
Elmtnode. style. Color
=   This . Styles ('normalforecolor ');
Elmtnode. style. Background
=   This . Styles ('normalbackcolor ');
}
}
If ( This . M_isactive)
{
Elmtnode. runtimestyle. textdecoration
= 'Underline ';
}
Else
{
Elmtnode. runtimestyle. textdecoration
= '';
}
};

In this method, it only cares about the priority between attributes and the modification of the UI element attributes. It is also the only place in the program to modify the HTML element attributes. However, the expand and collapse UI element property updates of the node subtree in this control are not put here, in this case, it is also very clear in their independent methods (there is no problem with other UI display conflicts ). That is to say, although we have design principles, sometimes we don't have to stick to them when we can use other simple methods. Design itself is a compromise, and what we actually need is simple and clear implementation, rather than the principle of perfection.

See the Treeview style changes in each section:

To be continued...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.