Dynamics CRM. Delete row from subgrid using JS - dynamics-crm-2013

How can I delete a row from a subgrid using Javascript?
var grid = Xrm.Page.getControl("Produktrader").getGrid();
grid.getRows().forEach(function (row) {
var rowData = row.getData();
if (rowData != null) {
//delete row here
}
}
}

If you want to delete the record in JavaScript you will need to make a REST call to delete the record from the database, and then refresh the page. You can't just delete the record from the subgrid.
Sample: Create, retrieve, update, and delete using the OData endpoint with JavaScript and jQuery

I'll add to James' answer in that after you perform the Delete, you should trigger a subgrid refresh via javascript
Xrm.Page.getControl("Produktrader").refresh();

Related

How with cypress get ID of inserted row in CRUD testing?

Using cypress("#vue/cli-plugin-e2e-cypress": "~4.2.0") in #vue/cli 4.0.5 app
I make CRUD opearitions and adding new category :
cy.get('#parent_id').select('1')
cy.get('section.submit_form_category')
.get('button.submit_button_category')
.click() // by submitting new category was added on server
cy.url()
.should('include', '/admin/categories/edit/')
cy.contains('Edit category')
cy.get('section.submit_form_category')
.get('a.cancel_button_category')
.click()
cy.url()
.should('include', '/admin/categories')
cy.contains('Categories') // opened listing of categories
Next I need to delete this row and for this I need to get ID of a new category created above.
If there is a way to make it ?
Thanks!
I found a decision with wait method and parsing response url
which must be like /admin/categories/edit/52 :
cy.get('section.submit_form_category')
.get('button.submit_button_category')
.click() // by clicking on submit button form is validated and new categoty inserted
cy.wait(1000)
cy.url()
.should('include', '/admin/categories/edit/') // newly inserted row must be redirected to the editor in edit mode with ID in url
.then((response) => {
var valuesArray = response.split('/admin/categories/edit/'); // get ID of newly inserted category
if (valuesArray.length == 2) {
let new_category_id = valuesArray[1]
// alert('new_category_id::' + new_category_id)
}
})
cy.contains('Edit category')
That works for me, though maybe there is a better solution?

How to delete a row from datatables and mysql in the same time?

I am using Datatables to list some data into a table. I figured out how to make the delete row work in the Datatables but I can not figure out how to make delete at the same time from the MySQL database. Not being deleted from there, on refresh will get the JSON file again that will populate my table. This is my code so far: (I am trying to get the ID of the row I am pressing the delete from, match the id with the one in the database and delete the one from the database)
This function executes when the delete button is pressed:
// Add an action delete record
$('#example tbody').on( 'click', 'button.delete', function () {
table.row( $(this).parents('tr') ).remove().draw();
// AJAX Request
$.ajax({
url: 'app/getcv.php',
type: 'POST',
data: { "data": "id" },
success: function(data){
alert("jjjjj");
// table.row($(this).parents('tr')).remove().draw();
}
})
} );
on Click Call your Delete Methods as soon as it clicked Delete the record from the database then romove the list

Set deleted records = 1 without actually deleting the records

How can i set the records i want to delete to 1 instead of deleting the record itself? I am using Yii framework right now and i am new to it. I have the CGridview with update, view and delete. On the delete i want the user to set the records which they want to delete to 1, instead of deleting the entire record. Thank you in advance for any help you may provide!
Just update your actionDelete in the controller:
public function actionDelete($id)
{
$model = $this->loadModel($id);
$model->is_deleted = "1"; //assuming is_deleted is the column name
$model->update();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
and your model search function to remove those items which have is_deleted = 1, you can:
//.... rest of your code above
$criteria->addCondition("is_deleted != 1");
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));

ASP MVC 4 Ajax.ActionLink for delete using AntiForgeryToken

I have a number of index pages where the row has an actionlink like so
#Ajax.ActionLink("Delete", "Delete", "AdverseEvent", new { id = Model.AdverseEventId }, new AjaxOptions { HttpMethod = "Post",OnSuccess = "rowDeleted", LoadingElementId="ajaxRequest_processing", Confirm = String.Format("Are you sure you want to delete adverse event for participant {0} at {1} ?", Model.ParticipantId, Model.EventTime) }, new { #class = "deleteAction" })
An actionlink is a great way to use progressive enhancement, because of course there is also a delete action, with get and post methods to perform the delete for those with javascript disabled.
I need to add an AntiForgeryToken. For an Ajax.BeginForm helper, Jon White's code works beautifully:
$.ajaxPrefilter(function (options, localOptions, jqXHR) {
var type = options.type.toLowerCase();
if (type === 'post') {
var token = GetAntiForgeryToken();
jqXHR.setRequestHeader(token.name, token.value);
}
});
When this gets executed within an actionlink, I assume because the index table is not wrapped in a form, I get the error message:
The required anti-forgery form field "__RequestVerificationToken" is not present
So i could wrap the whole table in a form posting back to the delete action, but this is then not very neat if I want to use other ajax.actionlinks to different actions within the table. I could wrap each actionlink in its own form, each with its own antiforgery token, but this is a significant amount of extra markup, and will leave dozens of elements on the page with identical values and name. The other option would be to use the ActionLink OnBegin method to wrap the button in a form, but the unobtrusive ajax library does not seem to pass any reference to the element causing the ajax get/post (foolishly in my opinion - you can upvote this issue on codeplex).
Any thoughts on a neat solution? Thank you.
You can add the token into the page and then use Ajax to send the field over in another call.
see How to include the #Html.AntiForgeryToken() when deleting an object using a Delete link

How to refresh datagrid

I create dojox.grid.datagrid and I fill content from array like on example last example on page. During time, I change value of that array in code. How to refresh content of that grid ? How to load new data from changed array ?
To change values in the grid, you will need to change the value in the grid's store. The grid data is bound to the store data, and the grid will update itself as needed.
So the key is to understand Dojo's data api and how stores work in Dojo. Rather than manipulating the data directly in the grid, manipulate it in the store.
Ideally, the store is your array that you manipulate as the application runs and you should not be needing to sync the array to the grid. Just use the ItemFileWriteStore as your data holder unless thats not possible.
Also, using the dojo data identity api makes it much simple to find items in the grid if that is possible. Assuming you know when an item is updated, deleted, or changed in your application you should be able to modify the grid store as needed when the action happens. This is definitely the preferred approach. If you can't do that you will have to do a general fetch and use the onComplete callback to manually sync your arrays which will be very slow and won't scale well, in which case you may as well just create a new store all together and assign it to the grid with grid.setStore(myNewStore)
Here is a fiddle with a basic create, update, and delete operation: http://jsfiddle.net/BC7yT/11/
These examples all take advantage of declaring an identity when creating the store.
var store = new dojo.data.ItemFileWriteStore({
data: {
identifier : 'planet',
items: itemList
}
});
UPDATE AN EXISITNG ITEM:
//If the store is not in your scope you can get it from the grid
var store = grid.store;
//fetchItemByIdentity would be faster here, but this uses query just to show
//it is also possible
store.fetch({query : {planet : 'Zoron'},
onItem : function (item ) {
var humans = store.getValue(item, 'humanPop');
humans += 200;
store.setValue(item, 'humanPop', humans);
}
});
INSERT A NEW ITEM:
store.newItem({planet: 'Endron', humanPop : 40000, alienPop : 9000});
} catch (e) {
//An item with the same identity already exists
}
DELETE AN ITEM:
store.fetchItemByIdentity({ 'identity' : 'Gaxula', onItem : function (item ) {
if(item == null) {
//Item does not exist
} else {
store.deleteItem(item);
}
}});
The following code snippet can be used to update the grid:
var newStore = new dojo.data.ItemFileReadStore({data: {... some new data ...});
var grid = dijit.byId("gridId");
grid.setStore(newStore);
EDIT:
Dogo data grid reference guide (add/remove rows example, updating grid data examples )
(I suppose you already have a working grid and you want to completely change the grid's store)
Create a new datastore with your new value :
dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) });
(data is the reponse from an ajax request for me)
Change your grid's store with the new one :
grid.store = dataStore;
Render :
grid.render();
This Will update Grid Store and refresh the View of the Grid in latest Version of Dojo 1.9
grid.store = store;
grid._refresh();
I had a server-side filtered EnhancedGrid, which was refreshing happily by changing the store, and shown in the other answers.
However I had another EnhancedGrid that would not refresh when a filter was applied. It may have been to do with the fact it was filtered client side (but data still coming from server using JsonRest store), but I don't really know the cause. Eitherway, the solution was to refresh with the following code:
grid.setFilter(grid.getFilter());
It's hacky and strange, but if it all else fails...
with this i can update a specifi row. this example is for a treegrid.
var idx = this.treeGrid.getItemIndex(item);
if(typeof idx == "string"){
this.treeGrid.updateRow(idx.split('/')[0]);
}else if(idx > -1){
this.treeGrid.updateRow(idx);
}