Combining BootstrapTable with KnockoutJS to implement addition, deletion, modification and query functions [1]_javascript skills
PHP中文网
Release: 2019-07-11 11:33:02
Original
3964 people have browsed it
bootstrap is a front-end framework that liberates good things for web developers. the ui it presents is very high-end and classy. in theory, there is no need to write a line of css. just add the appropriate attributes to the tag.
knockoutjs is an mvvm framework implemented in javascript. very cool. for example, after the list data items are added or deleted, there is no need to refresh the entire control fragment or write js to add or delete nodes by yourself. you only need to pre-define the template and attributes that conform to its syntax definition. simply put, we only need to focus on data access.
1. introduction to knockout.js
1. knockout.js and mvvm
nowadays, various front-end frameworks are overwhelming and dazzling. don’t lament that being a programmer is really hard. there are always endless techniques to learn, and when will it be the end, unless you transform! the sea of suffering is boundless, it’s up to you to decide whether you want to turn back or go to the shore!
knockout.js is a lightweight front-end framework based on the mvvm pattern. how lightweight is it? according to the latest version v3.4.0 shown on the official website, it is only 22kb. it can handle the binding of data model and interface dom in a friendly manner. the most important thing is that its binding is two-way. that is to say, if the data model changes, the data on the interface dom will also change accordingly. in turn, the interface dom will change accordingly. if the data on the database changes, the data model will also respond to this change. this can greatly reduce the amount of our front-end code and make our interface easy to maintain. we no longer need to write a lot of event monitoring data models and interface dom changes. the blogger below will illustrate these two points based on a usage example.
knockout.js official website: http://knockoutjs.com
knockout.js open source address: https://github.com/knockout/knockout
mvvm pattern: this is a design pattern for creating user interfaces. mvvm splits it into three parts: model, view, and viewmodel. model is the data model, view is our view, and viewmodel is a view model. used to bind the data model and dom elements on the view. if you have used wpf and silverlight, understanding this shouldn't be a problem; if you haven't, it doesn't matter. after reading this article, you will have a general understanding.
2. the simplest example
generally speaking, if you start using knockout.js from scratch, you need to do at least the following four steps
2.1. go to the official website to download the knockout.js file, and then quote it go to the view page.
note: knockout.js does not require the support of jquery. if your project needs to use jquery related operations, please quote jquery; otherwise just reference the above files.
2.2. definition of viewmodel
what is viewmodel? in fact, in js, it looks like a json object. we define a viewmodel:
var myviewmodel = {
name: "lilei",
profession: "软件工程师",
};
Copy after login
2.3, define the label binding data-bind in the view view
note: the text corresponding to the input tag needs to use textinput, while the text of the ordinary tag can be text.
2.4. activate binding
after completing the above three steps, you also need to activate the knockout binding
ko.applybindings(myviewmodel);
Copy after login
by doing these four steps, the simplest viewmodel data binding is basically realized.
if you are careful enough, you will find that the ko.applybindings() method has two parameters. the first one is the viewmodel we need to bind. what is the second one? from ko.applybindings(myviewmodel); we can see that the second parameter is an optional parameter, which represents the scope of the label bound to the viewmodel. for example, let's change the above code:
it can be seen that the second parameter limits the scope of myviewmodel, that is to say, it will only take effect if it is bound to the label with id="lb_name". the second parameter is a container tag such as a div, which indicates that the scope of the binding is all sub-tags under the div.
3. monitoring attributes
up to the above four steps , we can't see any effect, all we see is binding the data of a json object to the html tag. what's the point of doing this? doesn't it complicate a simple problem? don't worry, witness the miracle now! as mentioned above, the most important significance of knockout is two-way binding, so how to achieve our two-way binding? the answer is monitoring properties.
in knockout, there are three core monitoring attributes: observables, dependentobservables, and observablearray. the meaning of observe is translated as observation. it doesn’t feel right to call it observation attribute or observation attribute. it’s so appropriate, let’s call it monitoring attribute for now.
ko.observable("lilei") the meaning of this sentence is to add the name attribute of the viewmodel as a monitoring attribute. when the name attribute becomes a monitoring attribute, something magical will happen. let's take a look when we write myviewmodel. when:
name changes from the original attribute to a method. that is to say, once ko.observable() is added, the corresponding attribute will become a method. , then the value acquisition and assignment of name need to be processed using myviewmodel.name().
code explanation: obviously myviewmodel.name($(this).val()); this sentence assigns the value of the current text box to the name attribute, because the interface is bound to the name attribute, so the value in the label also changes accordingly. or you may say that this can also be done using the textchange event. as long as the value of the current text box is assigned to the label label, this effect can also be achieved. this is nothing. indeed, your way of writing can achieve the goal, but the significance of our monitoring attributes is that if the value of name is changed anywhere, the interface will change accordingly. instead of assigning values to label tags everywhere, js only needs to pay attention to just click on the method myviewmodel.name(). isn’t it awesome~~
code explanation: by adding monitoring dependent attribute ko.dependentobservable() can monitor the value of the des attribute to changes in both name and profession at the same time. if any one of them changes, the label bound to des will trigger the change. the biggest advantage of this is that it avoids the need for our js to go the trouble of operating dom is a bit interesting.
3.3. observablearray; monitoring array
in addition to the above two, ko also supports monitoring of array objects. let’s take a look at an example:
code explanation: the above is through ko.observablearray() the method adds monitoring of the array object, that is to say, as long as the array change is made to the deptarr array object anywhere in js, the ui will be triggered to give a corresponding response. one thing to note is that the monitored array is actually the monitored array object itself. changes in the properties of the sub-objects in the array object cannot be monitored. for example, we change the click event to this:
this shows that array monitoring actually monitors the array object itself, and does not monitor attribute changes of elements in the array. if you really need to monitor the attribute changes of the objects in the data, you need to use ko.observable() on the object attributes in the data, and use the two together. those who are interested can try it.
4. common data-bind attributes in ko
above, we used multiple data-bind attributes, so how many such data-bind attributes are there in knockout. here we list some commonly used properties.
4.1, text and inputtext
text, as the name suggests, means text. this binding attribute is generally used for
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn