A variety of operations in the store in ExtJS __js

Source: Internet
Author: User

First a statement, look good, posted over, not all tested.


Store.getcount () returns all the data records in the store, and then uses the for loop to traverse the entire store to get each record.

In addition to using the GetCount () method, you can also use the each () function, as shown in the following code.

1. Store.each (function (record) {

2. Alert (Record.get (' name '));

3.});


Each () can take a function as a parameter, traverse the internal record, and pass each record as a parameter to function () processing.

If you want to stop the traversal, you can have the function () return FALSE.

You can also use the GetRange () function to get more than one record in a row, just specify the index value for the start and end positions, as shown in the following code.

1. var records = Store.getrange (0, 1);

2.

3. for (var i = 0; i < records.length; i++) {

4. var record = records[i];

5. Alert (record.get (' name '));

6.}


If you do not know the ID of the record, you can also obtain the corresponding record from the store based on the ID of the record itself, as shown in the following code.

1. Store.getbyid (1001). Get (' name ')


Ext also provides function find () and FindBy (), which can be used to search for data in the store, as shown in the following code.

1. Find (String, string/regexp value, [number StartIndex], [Boolean Anymatch],[boolean casesensitive])


Of these 5 parameters, only the first two are required.

The first parameter property represents the field name of the search;

The second parameter value is matched with a string or regular expression;

The third parameter, startindex, indicates that the search starts at the first line;

When the fourth argument AnyMatch is true, there is no need to start a match from scratch;

When the fifth argument casesensitive is true, it is case-sensitive.

As shown in the following code:

1. var index = store.find (' name ', ' G ');

2.

3. Alert (Store.getat (index). Get (' name '));


The FindBy () function that corresponds to the find () function is defined in the following format:

1. FindBy (Function FN, [Object scope], [number StartIndex]): number


The FindBy () function allows users to search for internal data using custom functions.

When FN returns true, the lookup succeeds, stopping the traversal and returning the line number.

When FN returns false, the lookup fails (that is, it is not found) and continues traversing, as shown in the following code.

1. Index = Store.findby (function (record, id) {

2. Return record.get (' name ') = = ' Girl ' && record.get (' sex ') = = 1;

3.});

4.

5. Alert (Store.getat (index). Get (' name '));


With the FindBy () function, we can simultaneously judge multiple fields in a record and implement complex logic in a function.

We can also query the data in the store using the query and QUERYBY functions.

Unlike find and FindBy, query and Queryby return a Mixcollection object that contains data from the search, as shown in the following code.

1. Alert (store.query (' name ', ' Boy '));

2.

3. Alert (store.queryby function (record) {

4. Return record.get (' name ') = = ' Girl ' && record.get (' sex ') = = 1;

5.}));


Ext.data.Store update the data in the store

You can use Add (ext.data.record[] records) to add one or more record to the end of the store, using a parameter that can be a record instance, as shown in the following code.

1. Store.add (new Personrecord ({

2. Name: ' Other ',

3. sex:0

4.}));


Add () can also add a record array, as shown in the following code:

1. Store.add ([New Personrecord ({

2. Name: ' Other1 ',

3. sex:0

4.}), new Personrecord ({

5. Name: ' Other2 ',

6. sex:0

7.}));


The Add () function adds new data to the end of the store each time, which can break the store's original sort. You can use the addsorted () function if you want to insert new data into the corresponding location based on the original sort style of the store. It sorts the store immediately after adding new data, which ensures that the data in the store is displayed in an orderly manner, as shown in the following code.

1. store.addsorted (new Personrecord ({

2. Name: ' Lili ',

3. Sex:1

4.}));


The store finds the index position that the record should be inserted based on the sort information, and then inserts the data based on the resulting index position, which enables you to sort the whole. This function requires a local sort to be set in advance for the store, otherwise it will not work.

You can use the Insert () function if you want to specify the index position of the data insertion. Its first parameter represents the index position of the inserted data, which can be used as a parameter by using an array of record instances or record instances, after which the subsequent data is automatically moved back, as shown in the following code.

1. Store.insert (3, new Personrecord ({

2. Name: ' Other ',

3. sex:0

4.}));

5.

6. Store.insert (3, [new Personrecord ({

7. Name: ' Other1 ',

8. sex:0

9.}), new Personrecord ({

Name: ' Other2 ',

sex:0.

12.}));


Delete operations can use the Remove () and RemoveAll () functions, which can delete the specified record and empty the entire store, as shown in the following code.

1. Store.remove (store.getat (0));

2. Store.removeall ();


The store does not specifically provide an operation to modify a row of record, we need to get a record from the store first. Changes to the record's internal data are reflected directly to the store, as shown in the following code.

1. Store.getat (0). Set (' name ', ' xxxx ');


After you modify the internal data for a record, you have two options: Perform RejectChanges () Undo all modifications, restore the modified record to its original state, and perform commitchanges () submit data modifications. Before performing a undo and commit operation, you can use Getmodifiedrecords () to obtain a modified record array in the store.

The parameter associated with modifying the data is prunemodifiedrecords, and if you set it to true, all modifications are emptied every time a delete or reload operation is performed. This way, after each delete or reload operation, Getmodifiedrecords () returns an empty array, or it still gets the last modified record


Ext.data.Store Loading and displaying data

After the store is created, you need to call the load () function to load the data, and the load will be successful before you can manipulate the data in the store. The complete procedure for the load () call is shown in the following code.

1. Store.load ({

2. Params: {start:0,limit:20},

3. Callback:function (Records, options, success) {

4. Ext.Msg.alert (' info ', ' loading complete ');

5.},

6. Scope:store,

7. Add:true

8.});


1. Params is an additional parameter that is sent when the store is loaded.

2. Callback is a callback function executed at the time of loading, which contains 3 parameters: the records parameter represents the obtained data, the options represent the parameters passed when the load () is executed, and the success indicates whether the load was successful.

3. Scope is used to specify the scope of the callback function when it is executed.

4. When add is true, the data obtained by load () is added to the end of the original store data, otherwise the previous data will be purged before the resulting data is added to the store.

In general, to initialize the data in the store, the load () function only needs to be executed once. If you specify the parameters that you want to use with the params parameter, the store automatically uses the params parameter content that was included in the last load () when you later perform reload () reload data.

If you have some parameters that require a fixed pass, you can also use the Baseparams parameter, which is a JSON object in which the data is sent as a parameter to the background processing, as shown in the following code.

1. Store.baseParams.start = 0;

2. store.baseParams.limit = 20;


After loading the data for the store, you sometimes do not need to display all the data, and you can use function filter and Filterby to filter the data in the store, showing only the qualifying parts, as shown in the following code.

1. Filter (String field, String/regexp value, [Boolean AnyMatch], [Boolean casesensitive]): void


The use of the filter () function is similar to the Find () mentioned earlier, as shown in the following code.

1. Store.filter (' name ', ' Boy ');


The corresponding Filterby () is similar to FindBy () and can also implement complex judgments in custom functions, as shown in the following code.

1. Store.filterby (function (record) {

2. Return record.get (' name ') = = ' Girl ' && record.get (' sex ') = = 1;

3.});


If you want to cancel filtering and display all the data, you can call the Clearfilter () function, as shown in the following code.

1. Store.clearfilter ();


If you want to know if a filter is set up in the store, you can use the isfiltered () function to determine.


Ext.data.Store Other Features

In addition to the above mentioned data acquisition, sorting, updating, display and other functions, the store also provides some other functional functions.

1. Collect (String dataindex, [Boolean Allownull], [Boolean bypassfilter]): Array


The Collect function obtains data from the column corresponding to the specified dataindex, and when the Allownull argument is true, the returned result may contain null, undefined, or an empty string, otherwise the Collect function automatically filters out the empty data. When the Bypassfilter argument is true, the result of the collect is not affected by the query criteria, regardless of what the query condition is, the returned information is all data, as shown in the following code.

1. Alert (store.collect (' name '));


This gets the value of all the name columns, and the example returns an array that contains ' boy ' and ' girl '.

Gettotalcount () is used to get the total number of data that is passed in the background when the page is turned. If the page is not set, the result of Get-totalcount () is the same as GetCount (), which returns the total number of current data, as shown in the following code.

1. Alert (Store.gettotalcount ());


The IndexOf (Ext.data.Record record) and Indexofid (String ID) functions obtain the line number corresponding to the record based on the ID of the record or record, as shown in the following code.

1. Alert (Store.indexof (Store.getat (1)));

2. Alert (Store.indexofid (1001));


LoadData (Object data, [Boolean append]) reads data from a local JavaScript variable, and when Append is true, the read data is appended to the original data, otherwise the overall update is performed, as shown in the following code.

1. Store.loaddata (data, true);


SUM (String, number start, number end): number is used to calculate the sum of a column from start to end, as shown in the following code.

1. Alert (store.sum (' sex '));


If you omit the argument start and end, the sum of all the data is computed.

The store also provides a series of events (see the table below), allowing us to set the action function for the corresponding operation.

Table Store -provided events

Event name

Parameter

Add

(Stor E this, ext.data.record[] records, number index

beforeload

(St Ore this, Object options)

Clear-

(Store this)

datachanged

(Store this)

Load

(Store this, ext.data.record[] records, Object options)

Loadexception

()

Metachange

( Store this, Object meta.

Remove

(Store This, Ext.data.Record record, number index)

Update

(Store This, Ext.data.Record record, String operation)


var storecddm=New Ext.data.Store ({

Url:getapppath () + '/BMCDXX00.DO?METHOD=QUERYDATABYYYDWDM ',

baseparams:{},

Reader:new Ext.data.JsonReader ({

Totalproperty: ' Totalproperty ',

Root: ' Rows ',

ID: ' BH0000 '

},[

' BH0000 ',

' MC0000 ',

' YYDWDM '

])

});

var request = {strYydwdm:Ext.getCmp (' Yydwdms '). GetValue ()};

Storecddm.reload ({params:request});

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.