Improved the ctreectrl Data Loading Method to reuse code
Source: Internet
Author: User
In the MFC program, I use ctreectrlex to display data in the database. to simplify the operation, I started to load only one node hroot and then add a virtual node to the newly added node, when hroot is expanded, check whether there are virtual nodes. If there are virtual nodes, the virtual nodes are automatically deleted and the SQL statements required for the hroot subnodes are dynamically calculated, then, according to this SQL statement, read data from all the sub-nodes in the database and add it to the hroot. In addition, a virtual node is automatically added under each new sub-node hchild, when this hchild is expanded in the future, the subnode SQL of hchild is automatically calculated and loaded from the database.
This method is good and efficient. Recently, we have encountered a problem, that is, we want some web programs to display a treectrl on the webpage and load data to the tree using the same algorithms. Since the function of loading data from the database has been implemented in MFC, how can we reuse the code in the MFC program?
In fact, when the ctreectrlex of MFC displays data, a list of subnodes is always obtained from the database through an automatically calculated SQL statement each time, then add the list to the hitem with virtual nodes. Therefore, I only need to add a static function in ctreectrlex:
Cmdcdatatable * getnodechilddatatable (cnodeinfo * pnodeinfo)
Then, webtreectrl on the webpage provides pnodeinfo information and calls the getnodechilddatatable function to obtain the list of subnodes of a node in webtreectrl.
In this way, I only need to encapsulate ctreectrlex In the DLL. However, because ctreectrlex is an MFC class, the webpage cannot be called directly, and the webpage is used. to this end, I must pass the getnodechilddatatable function in ctreectrlex through C ++.. Net Package once, then. net can be called directly. Of course, it is called on the server.
All data loading operations in the current ctreectrlex are related to the control itself. During the process of loading sub-nodes, the relevant nodes on the ctreectrlex are constantly scanned to read the relevant information of the current node, the parameter of the getnodechilddatatable function is pnodeinfo. Therefore, the getnodechilddatatable function does not depend on ctreectrlex itself when loading data from the database, that is, getnodechilddatatable () the pnodeinfo parameter and cmdcable able * isolate the Close coupling between data access and data display, so that the getnodechilddatatable () function can be reused on the webpage.
When you set getnodechilddatatable (cnodeinfo * pnodeinfo) to a static function, you can not create a ctreectrlex instance when calling this function. In fact, this function can be completely isolated from ctreectrlex. Because it has nothing to do with ctreectrlex.
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.