I am building search functionality in multi-tab where user search criteria will be one tab, and user search results will be in other tab. When I am searching first time, I got my results with proper page navigation bar in second tab. But when user goes back to previous tab(search tab) and search again my grid results are refreshed with proper values but my navgrid is not appearing. I could not find dom for navgrid in firebug as well where initially that dom present with first time search.
Following are the code snippets.
function dispSearchResults() {
var no = document.getElementById("stNo").value;
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var dateOfBirth = document.getElementById("dateOfBirth").value;
document.getElementById("searchContainerModal").innerHTML='<table
id="searchResultsTab" style="width:100%"></table>';
jQuery("#searchResultsTab").jqGrid({
url : contextPath + '/web/getSearchViewResults.json',
datatype : "json",
mtype: "POST",
postData:{'NO': no, 'FIRST_NAME':firstName, 'LAST_NAME':lastName,
'DATE_OF_BIRTH':dateOfBirth},
colNames : [ 'no', 'First Name', 'Last Name', 'Date Of Birth'],
colModel : [ {
name : 'No',
index : 'No',
width : 100,
align : "center",
formatter: fmtStateno
}, {
name : 'firstName',
index : 'firstName',
width : 220,
align : "left",
formatter: fmFName
}, {
name : 'lastName',
index : 'lastName',
width : 220,
align : "left",
formatter: fmLName
}, {
name : 'dateOfBirth',
index : 'dateOfBirth',
width : 120,
align : "left",
formatter: fmBirthDate
}],
altRows : true,
altClass : 'oddRow',
loadComplete : function() {
document.getElementById("sResultsTab").style.visibility =
"visible";
if (jQuery("#searchResultsTab").getGridParam("records")==0) {
jQuery("#searchResultsTab").addRowData(
"blankRow", {"No":"No data was found",
"firstName":"", "lastName":"", "dateOfBirth":""}
);
}else{
$("tr.jqgrow:odd").addClass('oddRow');
}
},
onCellSelect: function(rowid,iCol,cellcontent,e) {
// alert(rowid);
cContent = cellcontent;
rId = rowid;
},
height : 240,
width : 1165,
rowNum : 25,
rowList : [25,50,75,100],
loadonce: true,
pagination:true,
pager : '#searchPagerDiv1',
sortname : 'no',
viewrecords : true,
sortorder : "desc",
caption : "Search Results",
gridview : true,
jsonReader : {
root : "rows",
page : "currentPage",
total : "totalPages",
repeatitems : false,
id : "id"
}
}).navGrid("#searchPagerDiv1",reloadAfterSubmit: false,
{edit:false,add:false,del:false});
});
$("#tabs").tabs("select", "#searchResultsTb");
}
Here are the code snippets
main jsp for tabs
------------------
<div id="mainContentDiv"<
<div id="tabs" style="background-color: transparent; height:auto"<
<ul id="Tabs1"<
<li id="sCriteriaTab"<<a href="#searchTab"<Search Criteria </a<
</li<
<li id="sResultsTab"< <a href="#searchResultsTb"<Search
Results</a< </li<
</ul<
<div id="searchTab" style="background-color: transparent" class="height250"<
<%# include file="../search/SearchCriteria.jsp" % <
</div<
<div id="searchResultsTb" style="background-color: transparent;"<
<%# include file="../search/SearchResults.jsp" %
<
</div<
SearchCriteria.jsp
-------------------
<form id="searchForm" name="searchForm"<
<div class="personDetailsDiv" id="personNameInfoDiv"<
<div style="float:right"<
<font class="standardInputTitle"<no</font<
<input id="No" name="No" class="standardInputText marginRight50" value=""
type="text"<</div< </div<
........
............
<div class="button-row"<
<a href="javascript:dispSearchResults()" class="button rounded blue effect-
3"<Search</a<
</div<
</div< </form<
SearchResults.jsp
----------------
<div id="searchContainerModal" style="width:100%"<</div<
<div id="searchPagerDiv1"<</div<
<div id="searchPagerDiv" style="width:100%"<
<div id="searchZeroRec" style="display:none;"< Zero Records Found ...
</div<
</div<
Appreciate your help !!!!
Related
I have created a kendo grid for 10 number of products in my project. I want the kendo window containing the details of the product to popup when I click on the productname displayed in the kendo grid.
I have looked into the demos of the kendo grid but I don't want the details of the product selected to be edited and also I don't want to use a separate column for details button as shown in the examples and demo.
I also looked into the music store demo of kendo ui but I couldn't understand its code as its in jQuery and I am using asp.net mvc with razor syntax for my project
Note:
I want window to appear only when I click on the name of the product and display its details.
You can use:
$('#grid').on("click", "tr.k-state-selected", function (e) {
// open window here
// you have i.e. Id here $('#grid').dataItem($('#grid').select()).Id
});
For this you must set option selectable: "row" in grid configuration.
Otherwise you can use just:
$('#grid').on("click", "tr", function (e) { ... }
You can make use of the detailTemplate for achieving it.
<script>
var wnd,
detailsTemplate;
$(document).ready(function () {
var grid = $("#grid").kendoGrid({
dataSource: {
pageSize: 20,
data: createRandomData(50)
},
pageable: true,
height: 550,
columns: [
{ field: "FirstName", title: "First Name", width: "140px" },
{ field: "LastName", title: "Last Name", width: "140px" },
{ field: "Title" },
{ command: { text: "View Details", click: showDetails }, title: " ", width: "180px" }]
}).data("kendoGrid");
wnd = $("#details")
.kendoWindow({
title: "Customer Details",
modal: true,
visible: false,
resizable: false,
width: 300
}).data("kendoWindow");
detailsTemplate = kendo.template($("#template").html());
});
function showDetails(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
wnd.content(detailsTemplate(dataItem));
wnd.center().open();
}
</script>
<script type="text/x-kendo-template" id="template">
<div id="details-container">
<h2>#= FirstName # #= LastName #</h2>
<em>#= Title #</em>
<dl>
<dt>City: #= City #</dt>
<dt>Birth Date: #= kendo.toString(BirthDate, "MM/dd/yyyy") #</dt>
</dl>
</div>
</script>
Go to this fiddle for a working demo
[UPDATE]
Here's the code snippet for showing the window while clicking on the Product Name
<script>
var wnd,
detailsTemplate;
$(document).ready(function () {
var grid = $("#grid").kendoGrid({
dataSource: {
pageSize: 20,
data: createRandomData(50)
},
pageable: true,
height: 550,
columns: [
{ field: "FirstName", title: "First Name", width: "140px",attributes: { "class": "FirstName"} },
{ field: "LastName", title: "Last Name", width: "140px" },
{ field: "Title" }]
}).data("kendoGrid");
wnd = $("#details")
.kendoWindow({
title: "Customer Details",
modal: true,
visible: false,
resizable: false,
width: 300
}).data("kendoWindow");
detailsTemplate = kendo.template($("#template").html());
$('#grid').on("click", ".FirstName", function (e) {
e.preventDefault();
var dataItem = $("#grid").getKendoGrid().dataItem($(e.currentTarget).closest("tr"));
wnd.content(detailsTemplate(dataItem));
wnd.center().open();
});
});
</script>
<script type="text/x-kendo-template" id="template">
<div id="details-container">
<h2>#= FirstName # #= LastName #</h2>
<em>#= Title #</em>
<dl>
<dt>City: #= City #</dt>
<dt>Birth Date: #= kendo.toString(BirthDate, "MM/dd/yyyy") #</dt>
</dl>
</div>
</script>
Working demo is here
I have an MVVM Grid binded to the 'LabsViewVM' ViewModel, shown below.
Column commands 'activate', 'suspend', 'abolish' are binded through the 'click' event to the LabsViewVM's transitLab method/handler, which executes just fine.
<div id="labs_view"
data-role="grid"
data-bind="source: labs, visible: isVisible, events: {edit: createLab, dataBound: dataBound, dataBinding: dataBinding}"
data-detail-init="LabsViewVM.detailInit"
data-detail-template= 'lab_details_template'
data-selectable="row"
data-scrollable= "true"
data-resizable= "true"
data-sortable= "{'allowUnsort': false}"
data-pageable="{ 'pageSizes': [5, 10, 15, 20, 25, 30, 50],
'messages': { 'display': '{0}-{1} από {2} Διατάξεις Η/Υ',
'empty': 'Δεν βρέθηκαν Διατάξεις Η/Υ',
'itemsPerPage': 'Διατάξεις Η/Υ ανά σελίδα',
'first': 'μετάβαση στην πρώτη σελίδα',
'previous': 'μετάβαση στην προηγούμενη σελίδα',
'next': 'μετάβαση στην επόμενη σελίδα',
'last': 'μετάβαση στην τελευταία σελίδα' }}"
data-editable="{ 'mode' : 'popup', 'template': $('#lab_create_template').html()}"
data-toolbar="[{ 'template' : $('#lab_toolbar_template_labs_view').html() }]"
data-columns="[{ 'field': 'lab_id', 'title':'Κωδικός Διάταξης Η/Υ', 'width':'65px', 'hidden' : true},
{ 'field': 'lab_name', 'title':'Ονομασία', 'width':'440px'},
{ 'field': 'lab_type', 'title':'Τύπος', 'width':'150px', 'hidden' : true},
{ 'field': 'lab_state', 'title':'Λειτουργική Κατάσταση', 'width':'150px'},
{ 'field': 'rating', 'title':'Αξιολόγηση', 'template' : $('#labs_view_rating_column_template').html(), 'width':'85px'},
{ 'field': 'positioning', 'title':'Τοποθεσία', 'width':'180px', 'hidden' : true},
{ 'field': 'lab_special_name', 'title':'Ειδική Ονομασία', 'width':'180px', 'hidden' : true},
{ 'field': 'creation_date', 'title':'Ημερομηνία Δημιουργίας', 'width':'150px', 'hidden' : true},
{ 'field': 'last_updated', 'title':'Τελευταία Ενημέρωση', 'width':'150px'},
{ 'field': 'created_by', 'title':'Δημιουργία από', 'width':'130px', 'hidden' : true},
{ 'command': [{'text':'Ενεργοποίηση', 'click':LabsViewVM.transitLab, 'name':'activate'},
{'text':'Αναστολή', 'click':LabsViewVM.transitLab, 'name':'suspend', },
{'text':'Κατάργηση', 'click':LabsViewVM.transitLab, 'name':'abolish'}],
'title': 'Ενέργειες', 'width':'500px', 'hidden': LabsViewVM.hideLabTransitColumn() }
]">
</div>
var LabsViewVM = kendo.observable({
transitLab: function(e){
e.preventDefault();
var parent_grid = $(e.delegateTarget).data("kendoGrid");
var transition_dialog = $("#transition_dialog").kendoWindow({
modal: true,
visible: false,
resizable: false,
width: 500,
pinned:true,
open: function(){lots of code...}
}).data("kendoWindow");
var transitTemplate = kendo.template($("#lab_transit_template").html());
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
transition_dialog.content(transitTemplate(dataItem));
transition_dialog.center().open();
}
});
I'm quoting from https://www.packtpub.com/books/content/kendo-mvvm-framework, some information for the click property just to stress the way kendo differentiates its click event from the traditional one.
"The click property binds the click event of a button to a function inside of the View-Model. It is a shortcut to the events binding that we will see later. Unlike a traditional click event wire-up, the Kendo UI framework will pass context data to the event handler to allow for a richer event-handling experience. For example, when a click event is bound within a row template, the event argument passed to the event handler will have access to the item from the source collection. This allows the event handler to operate against that Model data directly without any further DOM exploration and keeps all of the observable functionality in place."
Having that in mind, I was able to access the parent grid, inside transitLab, through the event parameter 'e'.
I then had to change my implementation and use a kendo template for the column commands, because I needed to add some logic to the appearance of the command buttons.
So i replaced
{ 'command': [{'text':'Ενεργοποίηση', 'click':LabsViewVM.transitLab, 'name':'activate'},
{'text':'Αναστολή', 'click':LabsViewVM.transitLab, 'name':'suspend', },
{'text':'Κατάργηση', 'click':LabsViewVM.transitLab, 'name':'abolish'}],
'title': 'Ενέργειες', 'width':'500px', 'hidden': LabsViewVM.hideLabTransitColumn() }
with
{ 'command': [{'name':'commands', 'template': $('#labs_grid_command_column_template').html()}], 'title': 'Ενέργειες', 'width':'270px' }
and template :
<script id="labs_grid_command_column_template" type="text/x-kendo-template">
#if( some condition ){#
<a class="k-button k-button-icontext k-grid-activate" href="\#" data-bind="click: transitLab"><i class="fa fa-check"></i> Ενεργοποίηση </a>
<a class="k-button k-button-icontext k-grid-suspend" href="\#" data-bind="click: transitLab"><i class="fa fa-clock-o"></i> Αναστολή </a>
<a class="k-button k-button-icontext k-grid-abolish" href="\#" data-bind="click: transitLab"><i class="fa fa-ban"></i> Κατάργηση </a>
#}#
</script>
but it doesn't work.
My code crashes inside transitLab handler, because its event
parameter 'e' does not get populated with the same context data as before.
For example e.delegateTarget in the first case points to the grid, whereas in the second case points to the command button itself.
I don't get it. Shouldn't these two implementations have the same effect?? Please help!
I am use jqGrid. I want to add record inline navigation
so this link http://www.trirand.com/blog/jqgrid/jqgrid.html?
when i click in ADD new row icon in navbar. Grid is like view modal view.
<div id="pager"></div>
<table id="ward"></table>
<br />
<script src="js/rowedex3.js" type="text/javascript"> </script>
<script type="text/javascript">
jQuery("#ward").jqGrid({
mtype : 'GET',
url : "listAllWards.html",
datatype : "json",
colNames : [ 'Id', 'Ward Type', 'Description'],
colModel : [ {
name : 'id',
index : 'id',
editable:true,
width : 50
}, {
name : 'name',
index : 'name',
width : 150,
editable:true,
}, {
name : 'decsription',
index : 'decsription',
width : 100,
editable:true,
}],
rowNum : 5,
rowList : [ 5, 10, 30 ],
pager : '#pager',
sortname : 'id',
viewrecords : true,
sortorder : "desc",
caption : "Ward's List",
width : 940,
cellEdit : true,
editurl: "server.html",
});
jQuery("#ward").jqGrid('navGrid', '#pager', {
edit : false,
del : false,
search :false,
});
</script>
If you want to use inline editing for add the row you should use add: false option of navGrid (together with edit: false which you already use) and you should include call of inlineNav after calling of navGrid. You can choose the buttons added by inlineNav by the usage of corresponding options of inlineNav described in the documentation.
UPDATED: You have to remove cellEdit : true option from jqGrid because the usage of both cell editing and inline editing is not supported.
Additionally you have to rename id column to any other name if you need to edit the grid. See the second part on the answer for additional details.
I recommend you to place the whole JavaScript code inside of $(function(){...}); and move it inside of <head>.
You should verify that you use current (now 4.4.1) version of jqGrid.
I have a page which load data from multiple partial views. One of the partial view has few links. say 3. Every time user clicks on the link I reload the page with a data related to clicked link. The form looks like this.
#model TestPostback.Models.HomeModel
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form id="testform" method="post" action="#Url.Action("Index","Home")" runat="server">
<div>
<table>
<tr>
<td>
This is a test page
</td>
</tr>
</table>
</div>
</form>
<div>
<table>
<tr>
<td style="vertical-align:top;">
<h2 class="expand">Test Expand</h2>
<div class="collapse">
<table id="testgrid" class="scroll" cellpadding="0" cellspacing="0"></table>
</div>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
jQuery(document).ready(function () {
var grid = jQuery("#testgrid");
grid.jqGrid({
url: 'Home/GridData/',
datatype: 'json',
mtype: 'POST',
colNames: ['Id', 'Votes', 'Title'],
colModel: [
{ name: 'Id', index: 'Id', width: 40, align: 'left' },
{ name: 'Votes', index: 'Votes', width: 40, align: 'left' },
{ name: 'Title', index: 'Title', width: 400, align: 'left'}
],
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '',
caption: 'My first grid'
});
});
</script>
Controller Code :-
public ActionResult Index(string selectedValue)
{
return View();
}
public JsonResult GridData(string sidx, string sord, int page, int rows)
{
int totalPages = 1; // we'll implement later
int pageSize = rows;
int totalRecords = 3; // implement later
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = new[]{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
}
};
return Json(jsonData);
}
Here is what I am trying to do.
When i click on Link "This is a Test Page", it calls the controller with some value but it does not load my grid. I want that value because i want to load a grid with different value based on user clicks. what are the changes I have to make for this to work?
please advice.
I'd recommend following the PRG (post redirect get) pattern: so after the post, you redirect to whichever action method is suitable (in this case the one that displays all the jqGrids). With MVC this can be achieved using a RedirectResult or the RedirectToAction method available on Controller.
Are you serializing your data when you get it back (ie serializeGridData)
Are you loading by JSON?
If so, check out
Setting the content-type of requests performed by jQuery jqGrid
How can i add tooltip to my jqgrid header cells? in case of multiple grids in the same page.
This is my code:
var initialized = [false,false,false];
$('#tabs').tabs
(
{
show: function(event, ui)
{
if(ui.index == 0 && !initialized[0])
{
init_confirm_payment();
initialized[0] = true;
}
else if(ui.index == 1 && !initialized[1])
{
init_signatory1_payment();
initialized[1] = true;
}
else if(ui.index == 2 && !initialized[2])
{
init_signatory2_payment();
initialized[2] = true;
}
}
}
);
function init_table1()
{
jQuery("#cpayment").jqGrid({
url:'loadgrid.jsp?type=1',
datatype: "xml",
loadonce:true ,
direction:"rtl",
height: '100%',
width: '100%',
headertitles: true ,
colNames:['col11','col22','col33','col44'],
colModel:[
{name:'col11',xmlmap:'col11', width:80, align:"right",sorttype:"int"},
{name:'col22',xmlmap:'col22', width:70, align:"right",sorttype:"int"},
{name:'col33',xmlmap:'col33', width:120, align:"right",sorttype:"string"},
{name:'col44:'col44', width:60, align:"right",sorttype:"float"},
],
multiselect: false,
autowidth: true,
forceFit: false,
shrinkToFit: false,
loadonce:true
});
}
function init_table2()
{
jQuery("#payment1").jqGrid({
url:'loadgrid.jsp?type=2',
datatype: "xml",
direction:"rtl",
height: '100%',
width: '100%',
headertitles: true ,
colNames:['col111','col222','col333','col444'],
colModel:[
{name:'col111',xmlmap:'col111', width:80, align:"right",sorttype:"int"},
{name:'col222',xmlmap:'col222', width:70, align:"right",sorttype:"int"},
{name:'col333',xmlmap:'col333', width:120, align:"right",sorttype:"string"},
{name:'col444',xmlmap:'col444', width:60, align:"right",sorttype:"float"},
],
multiselect: false,
autowidth: true,
forceFit: false,
shrinkToFit: false,
loadonce:true
});
}
function init_table3()
{
jQuery("#payment2").jqGrid({
url:'loadgrid.jsp?type=3',
datatype: "xml",
direction:"rtl",
height: '100%',
width: '100%',
headertitles: true ,
colNames:['col1','col2','col3','col4'],
colModel:[
{name:'col1',xmlmap:'col1', width:80, align:"right",sorttype:"int"},
{name:'col2',xmlmap:'col2', width:70, align:"right",sorttype:"int"},
{name:'col3',xmlmap:'col3', width:120, align:"right",sorttype:"string"},
{name:'col4xmlmap:'col4', width:60, align:"right",sorttype:"float"},
],
multiselect: false,
autowidth: true,
forceFit: false,
shrinkToFit: false,
loadonce:true
});
}
function test()
{
var thd = jQuery("thead:first", $("#cpayment").hdiv)[0];
jQuery("tr th:eq(" + 3 + ")", thd).attr("title", "bla bla");
var thd1 = jQuery("thead:first", $("#payment1").hdiv)[0];
jQuery("tr th:eq(" + 3 + ")", thd1).attr("title", "bla bla1");
}
</script>
</head>
<body>
<form>
<div id="tabs">
<ul>
<li> tab1 </li>
<li> tab2 </li>
<li> tab3 </li>
</ul>
<div id="tabs-1">
<table id="cpayment"></table>
</div>
<div id="tabs-2">
<table id="payment1"></table>
</div>
<div id="tabs-3">
<table id="payment2"></table>
</div>
</div>
<input type="button" onClick="test()">
</form>
</body>
Thank's In Advance.
Just include headertitles:true option in your jqGrid definition.
UPDATED: If you want set custom tooltip on a column header you can do following:
var setTooltipsOnColumnHeader = function (grid, iColumn, text) {
var thd = jQuery("thead:first", grid[0].grid.hDiv)[0];
jQuery("tr.ui-jqgrid-labels th:eq(" + iColumn + ")", thd).attr("title", text);
};
setTooltipsOnColumnHeader($("#list"), 1, "bla bla");
You should take in the consideration, that the column number iColumn is the 0-based absolute index of the column. Every from the options rownumbers:true, multiselect:true or subGrid:true include an additional column at the beginning, so the corresponding iColumn index should be increased.
UPDATED 2: For more information about the structure of dives, internal grid.hDiv elements and the classes used by jqGrid see this answer.
In my case I don't have index of the column to which I would like to set the tooltip.
I have modified the above answer by #Oleg as below.
//grid object, column id, tooltip text
var setTooltipsOnColumnHeader = function (grid, iColumn, text) {
var thd = jQuery("thead:first", grid[0].grid.hDiv)[0];
jQuery("tr.ui-jqgrid-labels", thd).find("[id='"+iColumn+"']").attr("title",text);
};
To add tooltip just call this methode on loadcomplete:
addToolTipForColumnheader('YourGridID');
function addToolTipForColumnheader(gridID){
var columnNameList=$('#'+gridID)[0].p.colNames;
for (var i = 0; i < columnNameList.length; i++){
var columnName=$('#'+gridID)[0].p.colModel[i].name;
$('#'+gridID+'_'+columnName).attr("title", $('#'+gridID)[0].p.colNames[i]);
}
}