Add checkbox in UltraGrid - ultrawingrid

How to dynamically add checkbox in infragistics UltraGrid

Just make sure that the data-type of the column you are binding is of type bool. It will automatically create checkbox for that column.

try the following
//get the data from db
var ds = GetDataFromDatabase();
ds.Tables[0].Columns.Add("Check", typeof(bool)); //this will create checkbox col
foreach(Datarow row in ds.Tables[0].Rows)
{
row["Check"] = true; // make all rows checked just to see it works
}
DataView dv = ds.Tables[0].DefaultView; //set it as a dataview
ultraGrid1.DataSource = dv; //set the dataview as the datasource for your grid

Make sure the column data type is bool (True/False or 0/1) then set:
grid.DisplayLayout.Bands[0].Columns["column_name"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
That should work.

While binding data to the grid, you can call the collection of a datatable through the query below:
"Select Convert(bit,0) as IsChecked, [OTHER_COLUMNS] from [TABLE_NAME]"
This will return a datatable with a first column of checkboxes.
Bind it with your grid using datasource.

Related

Fetch dropdown values or id from an inline grid

I want to fetch dropdown values or IDs. I am using a dropdown control in an inline editing kendo grid. I'm trying to obtain the values using the change function. My demo code is:
function gridEdit(e)
{
griddata = $('#CodeConfiguration').data("kendoGrid");
codeType = e.container.find(":input[name=CodeType_032]");
fixedCode = e.container.find(":input[name=FixedCode]");
alert(codeType.id);
codeType.change(function()
{
if(codeType.val() == 273)
{
fixedCode.attr('disabled',true);
}
});
}
I want to fetch CodeType dropdown values. Please suggest an appropriate solution.
Basically if your DropDownList is bound to field from the Grid model, then you can directly get the value from the Model.
e.g.
function onEdit(e) {
var codetype = e.model.CodeType;
}

Disable specific rows in a SlickGrid table

This is how my table looks:
a checkBox column
a name column
The table contains 5 rows.
What I want to achieve is to make the last two rows of the table unselectable (the respective checkboxes in the checkBox column should also disappear).
I managed to do this with jQuery after the table was rendered. Does the SlickGrid table allow me to perform the above mentioned scenario?
You can add a function for returning getItemMetadata() which is demonstrated in this example.
Here's a simplified version:
function getItemMetaData(row){
if (row >= view.getLength() - 2){ //only on last two rows.
return { selectable: false };
} else {
return {};
}
}
var view = new Slick.Data.DataView();
view.getItemMetadata = getItemMetaData;
var grid = new Slick.Grid(selector, view, cols, opts);
This method is then called in canCellBeSelected at line 2944 in slick.grid.js

Slickgrid add new row on demand

Is there a way to add a new row to a slickgrid on demand? For example, have a button on the page that shows the new row when clicked.
It can be done easily using dataView.addItem(params), just replace params with your parameters..
function addNewRow(){
var item = { "id": "new_" + (Math.round(Math.random() * 10000)), "Controller": tempcont, "SrNo1": tempsrno };
dataView.addItem(item);
}
here id, Controller, SrNo1 are ids of colunm
You can add a row dynamically, such as with a button click, by subscribing the "onAddNewRow" listener to your grid object. As many have suggested, it is best to use the dataView plugin to interface with your data. Note that you'll want the "enableAddRow" option set to "true".
Firstly, DataView requires that a unique row "id" be set when adding a new row. The best way to calculate this is by using dataView.getLength() which will give you the number of rows that currently exist in you grid (which we'll use as the id for the new row). The DataView addItem function expects an item object. So we create an empty item object and append the id to it.
Secondly, you'll be only focusing a single cell when you add your data. After you've entered that data (by blurring that cell), DataView will notice that you have not entered data for any of the remaining cells in the row (of course) and will default their values to "undefined". This is not really a desired effect. So what you can do is loop through the columns you've explicitly set and append them to our item object with a value of "" (empty string).
Thirdly, we don't want all the cells to be blank. Actually, we want the original cell's entered data to appear. We have that data so we can set it last so that it will overwrite the "" (empty string) value we previously set.
Lastly, we'll want to update the grid such that it displays all of these changes.
grid.onAddNewRow.subscribe(function (e, args) {
var input = args.item;
var col = Object.keys(input)[0]
var cellVal = input[Object.keys(input)[0]]
// firstly
var item = {};
item.id = dataView.getLength();
// secondly
$.each(columns, function(count, value) {
colName = value.name
console.log(colName)
item[colName] = ""
})
// thirdly
item[col] = cellVal
dataView.addItem(item);
// lastly
grid.invalidateRows(args.rows);
grid.updateRowCount();
grid.render();
grid.resizeCanvas();
});
I hope this helps. Cheers!
You can always add new rows to the grid. All you need to do is add the data object for the row to your grid data.
Assume you create your grid from a data source - myGridData (which is an array of objects).
Just push the new row object to this array. Call the invalidate method on the grid.
Done :)
Beside creating the new row, I needed it to be shown, so that the user can start writing on it. I'm using pagination, so here is what I did:
//get pagination info
var pageInfo = dataView.getPagingInfo();
//add new row
dataView.addItem({id: pageInfo.totalRows});
//got to last page
dataView.setPagingOptions({pageNum: pageInfo.totalPages});
//got to first cell of new row
var aux = totalRows/ pageInfo.pageSize;
var row = Math.round((aux - Math.floor(aux)) * pageInfo.pageSize, 0);
grid.gotoCell(row, 0 ,true);
Before I use pagination, I just needed this:
var newId = dataView.getLength();
dataView.addItem({id: newId});
grid.gotoCell(newId, 0 ,true);
var newRow = {col1: "col1", col2: "col2"};
var rowData = grid.getData();
rowData.splice(0, 0, newRow);
grid.setData(rowData);
grid.render();
grid.scrollRowIntoView(0, false);
Working fine for me.

How to append new records to existing records in a jqGrid?

I am populating Grid2 from the records selected from Grid1. however The records added are getting replaced by the new set of records from Grid1 whenever i select and add again. below is my code. This is only for the UI. I thought of appending the new records as below. Please guide with the correct code
function StuffData(data) {
var g = jQuery('#Grid2');
var usersList = data;
var totalRecords= jQuery('#Grid2').jqGrid('getGridParam','records');
alert('Grid records' +totalRecords);
if (usersList!=null) {
g.jqGrid('clearGridData',false);
for(var i=0;i<=usersList.length;i++){
// g.jqGrid('addRowData',i+1,usersList[i]);
g.jqGrid('addRowData',totalRecords+1,usersList[i]);
totalRecords += 1;
// g.jqGrid('addRowData',0,usersList);
}
}
}
The call to clearGridData is removing the old rows from the grid. From the jqGrid documentation:
clearGridData
Clears the currently loaded data from grid. If the clearfooter parameter is set to true, the method clears the data placed on the footer row.
If you only want to append data, you should be able to just remove this line of code. Or am I missing something?

How to set default field values for add form in jqgrid from current row

Add toolbar button is used to add new row to jqgrid.
Add form which appears contains all filed vlaues empty.
How to set add form field values from column values from row which was current/selected when add command was issued ?
json remote data is used. Or if this is simpler, how to call server method passing current/selected row to retrieve default values for add form from server ?
jqgrid contains also hidden columns. If possible values from hidden columns from current row should also sent to add controller if add form is saved.
Update
I tried to use Oleg great suggestion by using
afterShowForm: function(formID) {
var selRowData,
rowid = grid.jqGrid('getGridParam', 'selrow');
if (rowid === null) {
// todo: how to cancel add command here
alert('Please select row');
return;
}
selRowData = grid.jqGrid('getRowData', rowid);
if (selRowData === null) {
alert('something unexpected happened');
return;
}
$('#' + 'Baas' + '.FormElement', formID).val(selRowData.Baas);
}
Application keeps add form open after saving. After first save Baas field is empty. It looks like afterShowForm event runs only once, not after every save. How to fix this so that multiple rows with default values can added without closing add form?
How to cancel or not allow Add command if there is no selected row ?
If you need to make some initialization actions only for Add form you can use at least two approaches:
the usage of defaultValue property as function inside of editoptions. The callback function defaultValue can provide the value for the corresponding field of the Add form based of the data from selected row. For optimization purpose you can read the data from the current selected row once in the beforeInitData callback/event. You can just read the data which you need or make an synchronous call to the server to get the information which you need. The only disadvantage of the usage of defaultValue property is that it uses jQuery.val method to set the default value for all fields of Add form with exception 'checkbox' edittype. For the checkboxs jqGrid set checked property of the checkbox of false, 0, no, off or undefined are not found in the value returned by defaultValue property. So the approach will not work for other edittypes. For example it can fail for the custom edittype.
the usage of beforeShowForm or afterShowForm. Inside of the callback functions you can set any value of the form. You can find the filed of the corresponding column of the grid by id of the field which is the same as the value of name property of the corresponding column.
Like you already knows you can get the id of the current selected row with respect of getGridParam and get the data from the selected row
var selRowData, rowid = grid.jqGrid('getGridParam', 'selrow');
if (rowid !== null) {
// a row of grid is selected and we can get the data from the row
// which includes the data from all visible and hidden columns
selRowData= grid.jqGrid('getGridParam', 'selrow');
}
where grid is jQuery object like $('#list') which selects the main <table> element of the grid.
In the demo you can see how works the first approach described above.

Resources