I want to how can we undo the specific being added in the grid before actually saving it .
Please have a look at this
I 've added two rows but since for eg: for some validations failures the second row is not valid so I want to undo the second row but keeping the first dirty for the saving of that data to the db.
What i was trying is I was pushing all the dirty rows to an array and then updating in the database. But I don't know how to undo or delete the invalid row being added. Without reloading the grid
Example for reference
Live example
You shouldn't have to push the rows into an array for synchronization. What you really want to use is the methods available on the underlying store and/or model.
To reject the second model and update all other ones in the database, two lines suffice. The exact code may differ depending on the Ext version; in ExtJS 6.2.1, it would be:
grid.getStore().getAt(1).reject() (or .drop())
grid.getStore().sync()
while the generalized approach would be to reject all invalid models:
var store = grid.getStore(),
invalidRecords = store.query(function(record) {
return !record.getValidation().isValid();
});
invalidRecords.each(function(record) {
record.reject();
})
store.sync();
Related
I reconfigure my store and add new fields to it and then load its corresponding grid. I need to edit grid cells and save the whole modified grid rows in one step at the end.
The problem is that when I call this code, I get all rows in the grid even if I haven't edited any cell or row in the grid maybe because I have reconfigured the store.
But in fact nothing has been changed and new fields in the store are just for view.
How can I get the rows which their cell values has been changed ?
// returns all store records
Ext.ComponentQuery.query("documentgrid")[0].getStore().getModifiedRecords();
Your issue is not described quite clearly.
With a little guess,give you this answer,I hope it would help.
The getModifiedRecords() is not for that use.
Subscribe the store's "update" event instead.It can hook into the modification of model fields in the store.
Try code snippets:
{
//your grid config goes here
listeners:{
update:function(me, record){
//TODO save the modified record somewhere for your save button to pickup.
}
}
}
I was getting all the records because I hadn't call commitChanges on the store after reconfigurer.
I have two stores:
FAQs - contains a lot of models of my items
FAQ - contains one model.
In view mode I work with FAQs (to see all items) and in edit mode I work with FAQ just to work with one item and not to load all of them.
After finishing editing and saving FAQ I need to find that item in FAQs and make changes there as I've made in FAQ. I don't use network for it.
I know two ways:
1) find needed record in FAQs and replace it there
_updateFaqsStore: function() {
var faqsStore = Ext.data.StoreManager.lookup("faqs.FAQs");
var activeRec = this.activeRecord;
var index = faqsStore.indexOf( faqsStore.findRecord('id',activeRec.get('id')) ); // index in faqsStore of activeRec
faqsStore.remove(faqsStore.findRecord('id',activeRec.get('id'))); // remove old rec
faqsStore.insert(index, activeRec); // insert new - activeRec
but the object structure is not the same (though I use the same model)
2) find needed record in FAQs and set there every field
var faqsItem = faqsStore.findRecord('id', activeRec.get('id')); // find same item in FAQs store
faqsItem.set("myField", activeRec.get('myField')); // make changes in FAQs as in FAQ
but I need to enumerate all fields.
Maybe, there is some other way out? Please, help me!
You should not use two stores in this case. Why aren't you just using one store FAQs ? You then select the record, and you edit it directly.
For your use case there is no need at all to use two stores.
Also, you say you need to minimize the number of requests to the server. There is no problem for this. Just edit all the records you need to, and the at the end, you save all to the server at once.
Look at this example : it shows how to edit a record in a separate form, how to configure your requests (autoSync and batch buttons). You will deactivate autoSync and use batches to minimize roundtrips to the server.
Look also at rowediting example. This allows you to edit the data even easier.
Hi I'm using Drupal 7 and Views 3. I have a view (named 'export') that generates a csv export of selected node entities. However, I've put some custom code in that displays all the fields contained within that selected node entity, and allows the user to select fields (via checkboxes) that they do not want to include in the export.
I've tried unsetting the selected fields within hook_views_query_alter like so:
function mymodule_views_query_alter (&$view, &$query) {
if ($view->name == "export") {
unset($query->fields['field_data_field_description_node_entity_type']);
}
}
While that does unset that part of the fields array, I still get the description field populated in the csv export. I'm just not familiar enough with the views object structure to fully understand how to remove a given field from the view. I've searched the web for literally hours trying to find a post to shed some light on this. While I've found plenty of examples for using hook_views_query_alter to add filters or alter the WHERE statement of a query object, I haven't found anything having to do with removing the columns that a view query returns. Any advice on this would be very much appreciated!
Thanks,
axl
I was able to remove views fields for a CSV export by unsetting the field in hook_views_pre_build() in my custom module.:
function mymodule_views_pre_build(&$view) {
if ($view->name == 'campaign_report'
&& $view->current_display == 'views_data_export_1') {
// You'll have your own list of fields to remove that you create somehow...
$fields_to_remove = array('field_name_to_remove_1','field_name_to_remove_2');
foreach ($fields_to_remove as $field_name) {
unset($view->field[$field_name]);
unset($view->display_handler->handlers['field'][$field_name]);
}
}
}
This seems to work great for me, and is performed earlier in the views life cycle, before the query is even built. In fact I started using it for my table display view as well as my CSV export, since it seems more efficient than using the "hide if empty" column checkbox on the Views table settings (which must iterate over every row in the result set to see if it's empty in order to hide the column heading). If you wish to do that too, you will need to change the if() statement at the top so it only checks $view->name. Then the fields will be removed from all displays in that view (not just the views_data_export_1 display).
Try removing the column from the $view object.
unset($view->field['field_name'];
I have a button that when clicked, will create a JSONstore using a url provided. The store is then loaded into a grid. If the button is clicked again, it adds all the information again (so it is listed twice). I want it so that when the user clicks the button, it clears the store/grid and then adds everything.
Any ideas on how to accomplish this?
Thanks,
EDIT
ExtJS 3
datasetStore.removeAll();
datasetStore.loadData(datasetsArray);
It will be useful to see some code examples (and extjs version), but you can simply use loadRecords method (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.JsonStore-method-loadRecords):
grid.store.loadRecords([array of your new records here], {addRecords: false});
{addRecords: false} indicates that existing records will be removed first.
for ExtJs4: simply doe store.loadRecords([ array ]). In version 4.2.2 the store.proxy has NO clear() method so that doesn't work (was proposed in other examples elsewhere...)
If you want to to totally clear store and proxy, pass an empty array. This is handy because in some cases you want to clear the store and removeAll only moves the data to an array managed internally in the store so when later doing a sync on a gridStore which shows only 1 record, you may see your controller flooded with a bunch of records marked for removal!!
I am having problems saving database records using Linq in visual studio 2010 and sql server 2008.
My problem is that when I am editing some records I sometimes check the original database record for validation purposes, only the original entry seems to be updated in real time - I.e. it is already exactly the same as the edited record, before I have submitted the changes!
Could anyone suggest an effective method of coping with this? I have tried using a 2nd database connection or a 2nd data repository to call the original record from the db but it appears to be already changed when I debug it.
public void SaveobjectEdit(object objectToEdit)
{
object originalObject = GetobjectById(objectToEdit.Id);
if (originalObject.objectStatus != objectToEdit.objectStatus)
{
originalObject.objectStatus = objectToEdit.objectStatus;
}
SaveChanges();
}
The save changes just calls _db.SubmitChanges(); by the way
Has no one got any ideas for the above question?
I hope I was clear - for validation purposes I would like to compare an original database record with one that I am editing. The problem is that when I edit a record and then attempt to retrieve the original record before saving - the original record is exactly the same as the edited record.
If you're trying to retrieve the original record in code, from the same 'context' using the same access method, then it will contain the updated object. Rather than ask why you're doing this or what you're trying to achieve, I'll instead explain how I understand the data context / object context to work (in a very loose and vague fashion).
The context is something like an in-memory representation of your database, where everything is lazy-loaded. When you instantiate the context you're given an object which represents your data model (of course it may not be a 1-1 representation, and can contain various abstractions). Nothing is loaded into the context until necessary; any queries you write stay as queries until you peer in their results. When you access an item (e.g. GetobjectById(objectToEdit.Id)) the item is loaded into the context from the database and you can get and set its properties at your leisure.
Now, the important part: When you access an item, if it has already been loaded into the context then that in-memory object is returned. The context doesn't care about checking changes made; the changes won't be persisted to the database until you submit, but they remain in memory.
The way to refresh the in-memory objects is to call the Refresh method on the context. Try this test:
using (var db = new MyObjectContext())
{
var item = db.Items.First();
item.Name = "testing this thing";
Console.WriteLine(db.Shifts.First().Name);
db.Refresh(System.Data.Objects.RefreshMode.StoreWins, db.Items);
Console.WriteLine(db.Shifts.First().Name);
}
I believe this pattern makes a lot of sense and I'm not sure it could work any other way. Consider this:
foreach (var item in db.Items)
{
item.Name = "test";
}
Assert(db.Items.All(item => item.Name == "test"));
Would you want the Assert to fail? Should those items be reloaded? I don't believe so. I'm looking at the items in my context, not in the database. I'm not checking whether items in the database have been updated, but instead that I've updated all the items in the context of my code.
This is a good reason why I don't use MyObjectContext db - it is not a 'db' or a database connection. It's a context within which I can change whatever I want, so I name it such: MyObjectContext context.