I have a UI grid component and when you click on a row, I would like it to load the contents of another ui grid component on the page.
How would you write a function to tell the grid to update the second grid based on a rest api call with parameter from selected from first grid row? As an example, the first grid would be a list of states and the second grid would show all of the cities within the selected state.
Here are the steps to achieve what you are looking for.
Register the Grid API
Call modifyRows
Put your secoundary function call inside the onRowSelection call using .get selectedRows() to get all selected rows.
here is a quick example:
$scope.yourGridName.onRegisterApi = function (gridApi) {
// Set gridApi on scope, if you have more then one grid,
// Your grid api name (gApi in this case)must be unique
$scope.gApi = gridApi;
// Allow row modification
$scope.gApi.grid.modifyRows($scope.requestsGrid.data);
// On a change of selection
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
// Get current selected row, current row is an array
var currentRow = $scope.gApi.selection.getSelectedRows();
//put your secondary call for your second grid here ex:
var config = {
params : {param : currentRow[0].prop}
}
$scope.get("/your/URL", config)
.then (function (data) { //success callback })
.then (function (data, status...etc) { //error callback})
.finally( function(data,...etc) {//reset your grid})
});
};
Let me know if this resolves your issue.
http://ui-grid.info/docs/#/tutorial
Related
I want to update my data based on data received from back-end
there are two functions:
function getData(data){
Service.getRequest(url).then(function (_response) {
console.log(_response)
var res = _response
if(res.data.success) {
$scope.$watch('data.photo', function() {
alert('hey, myVar has changed!');
});
data.photo = res.data.result;// this will tell that Image received against the corresponding data
}
else {
ctrl.errorCallBack = true
ctrl.errorFunction = 5
}
$rootScope.showPreloader = false;
});
}
now I have second function that receives the selected data and called when clicked on button , now question is can I update the data inside this function when above Get request is completed:
function syncData(data) {
console.log('image received')
console.log(data)
}
First function is called n times based on number of images , now user click on second function to view Image but user don't see any Image because Image is not received yet but I want a way to update second function as Image is received
examples
ist image is loaded....user doesn't click on second function
2nd image is pending....user clicks on second function or second function is active
2nd image is received....active function image received
https://plnkr.co/edit/zKeDT84W6eF9cc3FpcmG?p=preview
If you use $scope for variables they will be automatically sync with DOM whenever it changes.
I did some changes for you in plnkr i hope solve your problem.
http:// plnkr.co/edit/Ug53a03kB7Ap1EWf2jZK?p=preview
I used angular-datatables for table, and its With ColVis Plugin (https://l-lin.github.io/angular-datatables/#/withColVis) for show/hide columns now I want to call services to maintain the column preference userwise (store column visibility state in database) so I had customized html open on click of show/hide column, I had took one button Update on click of this button I want to call web-service and after I will get response I will change the visibility of columns
But I am not able to bind the directive on update button click, once I able to bind directive I can call web service.
Can Anyone suggest whats going wrong?
Try to call your service on this state change event:
angular.module('showcase.withColVis', ['datatables', 'datatables.colvis'])
.controller('WithColVisCtrl', WithColVisCtrl);
function WithColVisCtrl(DTOptionsBuilder, DTColumnBuilder) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.fromSource('data.json')
.withPaginationType('full_numbers')
// Active ColVis plugin
.withColVis()
// Add a state change function
.withColVisStateChange(stateChange)
// Exclude the last column from the list
.withColVisOption('aiExclude', [2]);
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('firstName').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name')
];
function stateChange(iColumn, bVisible) {
console.log('The column', iColumn, ' has changed its status to', bVisible);
}
}
I need to set selection to a newly added row.
When $scope.gridApi.selection.selectRow(newObject) is entered, the grid's row model does not have the new row and thus can not select it.
After method addRow is finished, the grid shows the new row (and the data) but no selection was made.
My test code:
$scope.addRow = function() {
$scope.gridOptions.data.unshift({
// my new data object
});
var newObject = $scope.gridOptions.data[0];
$scope.gridApi.selection.selectRow(newObject);
};
How can I achieve this behavior?
Is there an event like 'rowAdded' in angularjs ui-grid I have to listen to?
Is there anywhere a comprehensive list of events fired by ui-grid?
Thanks for help!
You can event listeners on row, column and so on. The documentation is here http://ui-grid.info/docs/#/api/ui.grid.class:Grid
You can add the data listener in onRegisterApi like below,
onRegisterApi : function(gridApi)
{
if(!$scope.gridApi)
{
$scope.gridApi = gridApi;
$scope.gridApi.grid.registerDataChangeCallback(function(data)
{
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
}, [uiGridConstants.dataChange.ROW]);
}
}
and select any row you want to select.
Here is a working plnkr. http://plnkr.co/edit/NjnMb65w86L0bKmkkJp0?p=preview
I have a grid that on selection binds the record to a form.
gridSelectionChange: function (model, records) {
if (records[0]) {
this.getFormdata().getForm().loadRecord(records[0]);
}
},
Everything works fine, but if I change any field value the grid is not updating the record and neither the datastore.
I have read that I have to call:
form.updateRecord();
But how can I call it on every lost of focus of my fields (textfield, combos, etc.)?
Is there a way to do the two way binding?
You can see a working example under Ext JS 4 SDK examples/app/simple/simple.html
It binds a grid to a form inside a window.
This is the code that do the trick:
editUser: function(grid, record) { // this function fires when dblClick on itemRow
var edit = Ext.create('AM.view.user.Edit').show(); //creates a window a shows it
edit.down('form').loadRecord(record); //reference the form and load the record
},
updateUser: function(button) { //this function fires on save button of the form
var win = button.up('window'), //get a reference to the window
form = win.down('form'), // get a reference to the form
record = form.getRecord(), // get the form record
values = form.getValues(); // get the values of the form
record.set(values); //set the values to the record (same object shared with the grid)
win.close(); //close window
this.getUsersStore().sync(); // this line is only necesary if you want to synchronize with the server
}
Bottom line do:
var record = form.getRecord(),
values = form.getValues();
record.set(values);
I have a grid on my panel, named gridView, and gridView is in panel named panelMain, by dbclick listener on grid row, I load a from by doing something like this:
listeners:{
itemdblclick: function(dataview, index, item, e) {
/* I did not create new studentForm every time.*/
var editStudent = Ext.getCmp('editStudent');
if(editStudent == undefined)
editStudent = Ext.create('H.view.EditStudent');
editStudent.getForm().load({
url: 'studentDetails.php',
params: {studentId: studentId},
method:'GET',
success: function (form, action) {
var panelMain = Ext.getCmp('panelMain');
panelMain.items.clear();
panelMain.add(editStudent);
panelMain.update();
panelMain.doLayout();
},
failure: function (form, action) {
/// do nothing
}
});
}
After I edited the student I should come back to grid page, so I do something like this:
var panelMain = Ext.getCmp('panelMain');
var gridView = Ext.getCmp('gridView');
panelMain.items.clear();
panelMain.add(gridView);
panelMain.update();
panelMain.doLayout();
The problem is when I come back to the grid, it does not fire any itemdbclick event any more (it's like the grid is just an image in page, no event fires).
And sometimes when I go to edit studentForm and come back grid work, but when I go to student form again, the student page does not fire any event, when I click edit button, I do not get any answer, I cant see even on mouse hover (that causes changes on button color).
What is the problem here?
I use Extjs 4 and Extjs MVC.
I have one Controller for grid and edit student page.
I think your misunderstand the success config on form.
Try:
listeners:{
itemdblclick: function ( gridView, record, item, index, e, eOpts ) {
var editStudent = Ext.getCmp('editStudent');
if(editStudent == undefined)
editStudent = Ext.create('H.view.EditStudent');
/* Load record in the form.
Form must have on formfields property: 'name' equals to 'dataIndex' */
editStudent.getForm().loadRecord(record);
var panelMain = Ext.getCmp('panelMain');
panelMain.items.clear();
panelMain.add(editStudent);
}
success and failure are the callbacks functions for the submit function.
a) You are not using MVC pattern here. With what Sencha calls MVC, you would have all this code in a Controller instead of event listener.
b) I strongly suspect that this code causes deadlocks somewhere, with events firing so rapidly in succession that browser just freezes. You need to use debugger to see what exactly happens.