`
xf986321
  • 浏览: 163886 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Add jQuery datagrids to your Rails applications

阅读更多

Add jQuery datagrids to your Rails applications



Update : added support for subgrids. Have a look at the last example!

The jQuery grid plugin is an amazing Javascript project providing multi-functions Ajax datagrids for your web applications. With the 2dcJqgrid Rails plugin , you can now add these datagrids to your Ruby on Rails applications with just a few lines of code.

If you don't like the look & feel of this demo, you can easily customize it using jQuery themes .

Communications between your grids and the server will use the JSON format to exchange data.

The source code of this demo application is available on GitHub.

This solution is compatible with most of web browsers (even Internet Explorer 6 !!).

If you have any comments or suggestions, please post them here .


Installation


 

Inside your Rails application :

$ ./script/plugin install git://github.com/ahe/2dc_jqgrid.git
$ ./script/plugin install git://github.com/thoughtbot/squirrel.git

./script/generate scaffold user pseudo:string firstname:string lastname:string email:string role:string


Add some data to your migration file, you can use this one .
Run the migration :

$ rake db:migrate


Open the default layout created for you by the scaffold script (layouts/users.html.erb) and add the required JS & CSS in the header :

<%= jqgrid_stylesheets %>
<%= jqgrid_javascripts %>


Replace the index method in your controller by this one :

def 
index

  users
 =
 User
.
find
(
:all
)
 do

    if
 params
[
:_search
]
 ==
 "
true
"

      pseudo
    =~
 "
%#{params[:pseudo]}%
"
 if
 params
[
:pseudo
].
present?

      firstname
 =~
 "
%#{params[:firstname]}%
"
 if
 params
[
:firstname
].
present?

      lastname
  =~
 "
%#{params[:lastname]}%
"
 if
 params
[
:lastname
].
present?
                
      email
     =~
 "
%#{params[:email]}%
"
 if
 params
[
:email
].
present?

      role
      =~
 "
%#{params[:role]}%
"
 if
 params
[
:role
].
present?
        
    end

    paginate
 :page
 =>
 params
[
:page
],
 :per_page
 =>
 params
[
:rows
]
      
    order_by
 "
#{params[:sidx]} #{params[:sord]}
"

  end


  respond_to
 do
 |
format
|

    format
.
html

    format
.
json
 {
 render
 :json
 =>
 users
.
to_jqgrid_json
([
:id
,
:pseudo
,
:firstname
,
:lastname
,
:email
,
:role
],
 
                                                       params
[
:page
],
 params
[
:rows
],
 users
.
total_entries
)
 }

  end

end


The query has been added into the controller for clarity purposes in this demo. It's of course a better idea to create a method in your User class. Notice how the squirrel plugin makes it easy to add filters and pagination to our finder.

Also notice the to_jqgrid_json method, it will generate the required JSON for you. The order of the fields in the first parameter matters, it should be the same than the display order in your datagrid.
You are now ready to create datagrids.

Simple DataGrid with search, pagination & sorting



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo'},{name:'firstname', index:'firstname'},{name:'lastname', index:'lastname'},{name:'email', index:'email'},{name:'role', index:'role'}], pager: jQuery('#players_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players&quot;).height(25).hide().filterGrid(&quot;players&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players&quot;).navGrid('#players_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>



The code used to create this grid is :

<%= jqgrid
("
Football Players
",
 "
players
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
"
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
"
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
"
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
"
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
"
 }
				
	]

)
 %>


The first argument of the jqgrid helper is the title of your grid.
The second one is his DOM ID.
The third one is the URL used to retrieve data.
Finally, it takes an array of hashes to configure columns.



Simple DataGrid with selection link/button



<script type="text/javascript"> function handleSelection(id) { alert('ID selected : ' + id); } </script><script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_2&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo'},{name:'firstname', index:'firstname'},{name:'lastname', index:'lastname'},{name:'email', index:'email'},{name:'role', index:'role'}], pager: jQuery('#players_2_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_2&quot;).height(25).hide().filterGrid(&quot;players_2&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_2_select_button&quot;).click( function(){ var id = jQuery(&quot;#players_2&quot;).getGridParam('selrow'); if (id) { handleSelection(id); } else { alert(&quot;Please select a row&quot;); } return false; }); jQuery(&quot;#players_2&quot;).navGrid('#players_2_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_2_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_2&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_2&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_2&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>

Get ID of selected row

In this case, we added a "Get ID of selected row" link. When this link is clicked, the Javascript method defined by :selection_handler is called, with the ID of the row as a parameter :

<script type="text/javascript">
function handleSelection(id) {
	alert('ID selected : ' + id);
}
</script>
<%=jqgrid
("
Football Players
",
 "
players_2
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
"
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
"
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
"
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
"
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
"
 }

	],

	{
 :selection_handler
 =>
 "
handleSelection
"
 }

)
%>
<a href="#" id="players_2_select_button">Get ID of selected row</a>


The ID of this link is very important, it must be the ID of the jqgrid + "_select_button". You can use a button instead of a link if you want.



Simple DataGrid with direct selection



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_3&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo'},{name:'firstname', index:'firstname'},{name:'lastname', index:'lastname'},{name:'email', index:'email'},{name:'role', index:'role'}], pager: jQuery('#players_3_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', onSelectRow: function(id){ if(id){ handleSelection(id); } }, subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_3&quot;).height(25).hide().filterGrid(&quot;players_3&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_3&quot;).navGrid('#players_3_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_3_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_3&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_3&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_3&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>



If you want to call the handler directly when you select a row instead of clicking on a link/button, use the following options. Of course, you also need the Javascript method "handleSelection" defined in the previous section.

<%=jqgrid
("
Football Players
",
 "
players_3
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
"
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
"
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
"
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
"
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
"
 }

	],

	{
 :selection_handler
 =>
 "
handleSelection
",
 :direct_selection
 =>
 true
 }

)
%>

 



Simple DataGrid with multiple selections



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_4&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo'},{name:'firstname', index:'firstname'},{name:'lastname', index:'lastname'},{name:'email', index:'email'},{name:'role', index:'role'}], pager: jQuery('#players_4_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', multiselect: true, subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_4&quot;).height(25).hide().filterGrid(&quot;players_4&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_4_select_button&quot;).click( function() { var s; s = jQuery(&quot;#players_4&quot;).getGridParam('selarrrow'); handleSelection(s); return false; }); jQuery(&quot;#players_4&quot;).navGrid('#players_4_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_4_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_4&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_4&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_4&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>

Get IDs of selected rows

>%=jqgrid
("
Football Players
",
 "
players_4
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
"
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
"
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
"
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
"
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
"
 }

	],

	{
 :selection_handler
 =>
 "
handleSelection
",
 :multi_selection
 =>
 true
 }

)
%>
<a href="#" id="players_4_select_button">Get IDs of selected rows</a>

 



Simple DataGrid with master details



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_5&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo'},{name:'firstname', index:'firstname'},{name:'lastname', index:'lastname'},{name:'email', index:'email'},{name:'role', index:'role'}], pager: jQuery('#players_5_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', onSelectRow: function(ids) { if(ids == null) { ids=0; if(jQuery(&quot;#players_5_details&quot;).getGridParam('records') &gt;0 ) { jQuery(&quot;#players_5_details&quot;).setGridParam({url:&quot;/users/pets?q=1&amp;id=&quot;+ids,page:1}) .setCaption(&quot;Pets: &quot;+ids) .trigger('reloadGrid'); } } else { jQuery(&quot;#players_5_details&quot;).setGridParam({url:&quot;/users/pets?q=1&amp;id=&quot;+ids,page:1}) .setCaption(&quot;Pets : &quot;+ids) .trigger('reloadGrid'); } }, subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_5&quot;).height(25).hide().filterGrid(&quot;players_5&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_5&quot;).navGrid('#players_5_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_5_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_5&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_5&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_5&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>


<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_5_details&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users/pets?nd='+new Date().getTime(), editurl:'', datatype: &quot;json&quot;, colNames:['ID','Name'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'name', index:'name',align:'center',width:500}], pager: jQuery('#players_5_details_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', subGrid:false, caption: &quot;Pets&quot; }); jQuery(&quot;#t_players_5_details&quot;).height(25).hide().filterGrid(&quot;players_5_details&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_5_details&quot;).navGrid('#players_5_details_pager',{edit:false,add:false,del:false,search:false,refresh:true}) .navButtonAdd(&quot;#players_5_details_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_5_details&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_5_details&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_5_details&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>



We need associated data to create this master-details grid :

$ ./script/generate model pet name:string user_id:integer


Add a relationship in your User (has_many :pets) and Pet (belongs_to :user) models.
Then add data to your migration file : you can use this one .

Create the table :

$ rake db:migrate


And add the pets method in your users controller :

def 
pets

  if
 params
[
:id
].
present?

    pets
 =
 User
.
find
(
params
[
:id
]).
pets
.
find
(
:all
)
 do

      paginate
 :page
 =>
 params
[
:page
],
 :per_page
 =>
 params
[
:rows
]
      
      order_by
 "
#{params[:sidx]} #{params[:sord]}
"
        
    end

    total_entries
 =
 pets
.
total_entries

  else

    pets
 =
 []

    total_entries
 =
 0

  end

  respond_to
 do
 |
format
|

    format
.
json
 {
 render
 :json
 =>
 pets
.
to_jqgrid_json
([
:id
,
:name
],
 params
[
:page
],
 params
[
:rows
],
 total_entries
)
 }

  end

end


Don't forget to edit your routes and restart the server :

map.resources :users, :collection => { :pets => :get }


You can finally add your grids :

<%=jqgrid
("
Football Players
",
 "
players_5
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
"
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
"
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
"
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
"
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
"
 }

	],

	{
 :master_details
 =>
 true
,
 :details_url
 =>
 "
/users/pets
",
 :details_caption
 =>
 "
Pets
"
 }

)
%>
<br/>
<%=jqgrid
("
Pets
",
 "
players_5_details
",
 "
/users/pets
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
name
",
 :label
 =>
 "
Name
",
 :width
 =>
 500
,
 :align
 =>
 '
center
'
 }

	]

)
%>


The DOM ID of your details grid is important, it must be the ID of the master grid + "_details".



For evident reasons, data manipulation has been disabled in this demo

Data manipulation with inline editing



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_6&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'/users/post_data', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo',editable:true},{name:'firstname', index:'firstname',editable:true},{name:'lastname', index:'lastname',editable:true},{name:'email', index:'email',editable:true},{name:'role', index:'role',editable:true}], pager: jQuery('#players_6_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', onSelectRow: function(id){ if(id &amp;&amp; id!==lastsel){ jQuery('#players_6').restoreRow(lastsel); jQuery('#players_6').editRow(id,true); lastsel=id; } }, subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_6&quot;).height(25).hide().filterGrid(&quot;players_6&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_6&quot;).navGrid('#players_6_pager',{edit:false,add:true,del:true,search:false,refresh:true}) .navButtonAdd(&quot;#players_6_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_6&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_6&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_6&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>



We need one last method in our controller to handle data manipulation.
Create the post_data method in your users controller :

def 
post_data

  if
 params
[
:oper
]
 ==
 "
del
"

    User
.
find
(
params
[
:id
]).
destroy

  else

    user_params
 =
 {
 :pseudo
 =>
 params
[
:pseudo
],
 :firstname
 =>
 params
[
:firstname
],
 :lastname
 =>
 params
[
:lastname
],
 
                    :email
 =>
 params
[
:email
],
 :role
 =>
 params
[
:role
]
 }

    if
 params
[
:id
]
 ==
 "
_empty
"

      User
.
create
(
user_params
)

    else

      User
.
find
(
params
[
:id
]).
update_attributes
(
user_params
)

    end

  end

  render
 :nothing
 =>
 true

end


It's of course your role to add security & validation rules.

If protect_from_forgery is on, disable it for this action :

protect_from_forgery :except => [:post_data]


Edit your routes and restart the server :

map.resources :users, :collection => { :pets => :get, :post_data => :post }


You can now add the grid :

<%=jqgrid
("
Football Players
",
 "
players_6
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
",
 :editable
 =>
 true
 }

	],

	{
 :add
 =>
 true
,
 :edit
 =>
 true
,
 :inline_edit
 =>
 true
,
 :delete
 =>
 true
,
 :edit_url
 =>
 "
/users/post_data
"
 }

)
%>

 



Data manipulation with modal editing (+ navigation)



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_7&quot;).jqGrid({ // adding ?nd='+new Date().getTime() prevent IE caching url:'/users?nd='+new Date().getTime(), editurl:'/users/post_data', datatype: &quot;json&quot;, colNames:['ID','Pseudo','Firstname','Lastname','Email','Role'], colModel:[{name:'id', index:'id',width:35,resizable:false},{name:'pseudo', index:'pseudo',editable:true},{name:'firstname', index:'firstname',editable:true},{name:'lastname', index:'lastname',editable:true},{name:'email', index:'email',editable:true},{name:'role', index:'role',editable:true}], pager: jQuery('#players_7_pager'), rowNum:10, rowList:[10,25,50,100], imgpath: '/images/themes/lightness/images', sortname: 'id', viewrecords: true, toolbar : [true,&quot;top&quot;], sortorder: 'asc', subGrid:false, caption: &quot;Football Players&quot; }); jQuery(&quot;#t_players_7&quot;).height(25).hide().filterGrid(&quot;players_7&quot;,{gridModel:true,gridToolbar:true}); jQuery(&quot;#players_7&quot;).navGrid('#players_7_pager',{edit:true,add:true,del:true,search:false,refresh:true}) .navButtonAdd(&quot;#players_7_pager&quot;,{caption:&quot;Search&quot;,title:&quot;Toggle Search&quot;,buttonimg:'/images/jqgrid/search.png', onClickButton:function(){ if(jQuery(&quot;#t_players_7&quot;).css(&quot;display&quot;)==&quot;none&quot;) { jQuery(&quot;#t_players_7&quot;).css(&quot;display&quot;,&quot;&quot;); } else { jQuery(&quot;#t_players_7&quot;).css(&quot;display&quot;,&quot;none&quot;); } } }); }); </script>

 

<%=jqgrid
("
Football Players
",
 "
players_7
",
 "
/users
",

	[

		{
 :field
 =>
 "
id
",
 :label
 =>
 "
ID
",
 :width
 =>
 35
,
 :resizable
 =>
 false
 },

		{
 :field
 =>
 "
pseudo
",
 :label
 =>
 "
Pseudo
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
firstname
",
 :label
 =>
 "
Firstname
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
lastname
",
 :label
 =>
 "
Lastname
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
email
",
 :label
 =>
 "
Email
",
 :editable
 =>
 true
 },

		{
 :field
 =>
 "
role
",
 :label
 =>
 "
Role
",
 :editable
 =>
 true
 }

	],

	{
 :add
 =>
 true
,
 :edit
 =>
 true
,
 :inline_edit
 =>
 false
,
 :delete
 =>
 true
,
 :edit_url
 =>
 "
/users/post_data
"
 }

)
%>

 



Data manipulation with various input types



<script type="text/javascript"> var lastsel; jQuery(document).ready(function(){ jQuery(&quot;#players_8&quo

分享到:
评论

相关推荐

    ASP.NET Core Recipes, 2nd Edition

    ASP.NET Core Recipes is a practical guide for developers creating modern web applications, cutting through the complexities of ASP.NET, jQuery, React, and HTML5 to provide straightforward solutions to...

    ASP.NET MVC 4 Recipes: A Problem-Solution Approach

    •migrating a project from ASP.NET web forms to the MVC 4 including recipes for converting DataGrids, Forms, Web Parts, Master Pages and navigation controls •Client side data binding and templating ...

    ComponentOne Studio for WPF2012 v3 2/3

    Studio for WPF features all of the rich data visualization and LOB controls you need to shorten your development time and create compelling Windows desktop applications. Choose from 50+ WPF controls ...

    ComponentOne Studio for WPF2012 v3 1/3

    Studio for WPF features all of the rich data visualization and LOB controls you need to shorten your development time and create compelling Windows desktop applications. Choose from 50+ WPF controls ...

    ComponentOne Studio for WPF2012 v3 3/3

    Studio for WPF features all of the rich data visualization and LOB controls you need to shorten your development time and create compelling Windows desktop applications. Choose from 50+ WPF controls ...

    Tutorials - 4 - Linq SQL WCF and DataGrids R2.pdf

    - **LINQ to SQL**:LINQ to SQL是LINQ的一个具体实现,专门用于处理关系数据库。它为.NET Framework提供了将数据库表映射到.NET类的能力,并提供了一种简单的方法来执行CRUD操作(创建、读取、更新和删除)。 - **...

    C# XML demo,C# XML入门,XmlHelper

    The article evolves from simple concepts about XML to creation of XML documents, to DataSource assignment from XML documents to ListBoxes and DataGrids, and finally to DataSource update from changes ...

    VS2012 WPF框架 实现数据库的增删改查并用datagrids显示到窗口

    这些类使得我们可以使用Linq-to-Entities进行查询,简化了数据库交互。 接下来,我们需要在XAML中设计UI,包括添加DataGrid控件。DataGrid是WPF中用于展示表格数据的强大控件。我们可以通过绑定DataGrid的...

    在DataGrids,下拉列表中轻松使用ADO

    标题 "在DataGrids,下拉列表中轻松使用ADO" 暗示了本文将讨论如何在.NET框架中,特别是ASP.NET环境下,有效地利用ADO(ActiveX Data Objects)技术来与数据库交互,并在DataGrid控件和下拉列表中显示数据。...

    datagrids:用于处理 IMDG 的模式、实用程序和库

    数据网格用于处理 IMDG 的模式、实用程序和库如何构建你需要: 安装了gradle。 在撰写本文时为 v2.1。 连贯性.jar。 在撰写本文时为 v12.1.3。 一个名为 JAVA_3RD_PARTY_LIBS 的环境变量,这样 coherence.jar 可以在...

    tree节点移动 Tree节点移动到DataGrid Winfrom 皮肤

    在Windows Forms(WinForms)应用开发中,我们经常会遇到数据展示和操作的需求,例如将`TreeNodes`(树形结构的节点)与`DataGrids`(数据网格)结合使用。`TreeNode`是`TreeView`控件的基本元素,用于表示层次化的...

    DataGrid的客户端分页

    一方面,DataGrids大大简化了在HTML中通过表格中描述数据源,另一方面,DataGrid在服务器端如此重要,导致了大量的页面请求被传回服务器。例如,当用户使用分页DataGrid时,从一个页面转到另一个页面就会导致页面...

    oro-phpstorm-plugin:PhpStorm 插件,为 OroPlatform 配置文件启用自动完成

    datagrids.yml 实体.yml images.yml layout.yml navigation.yml requirejs.yml search.yml system_configuration.yml theme.yml 工作流程 在应用程序执行期间动态添加的实体方法的自动完成。 可以通过 ...

    JAWIG-开源

    JAWIG 代表 Javascript Advanced Widget Generator,是一个 Javascript 框架,用于仅使用 HTML 标签生成高级小部件,如 Datagrids、Tabletabs、Windows、Datatrees、Menus 和 Contextmenus。

    Manning.Flex.3.in.Action.Feb.2009

    DataGrids、列表和树是Flex应用中常用的控件,用于展示大量数据。本章通过多个实例演示了如何使用这些控件,并提供了有关性能优化方面的建议。 - **第9章:列表定制** 除了标准的列表控件外,Flex还支持高度定制...

    北极星博客系统(.NET版)

    在项目中提到的“第三方控件”,可能包括一些预构建的UI组件,如DataGrids、Charts、Calendars等,这些控件可以简化开发过程,提供更好的用户体验。它们可能来自于DevExpress、Telerik或DevExpress等供应商,用于...

    Alpha Controls 的数据库控件

    1. 数据网格(DataGrids):这些控件提供了用于显示和编辑数据库表的行和列的可视化界面。它们通常具有丰富的自定义选项,如列宽调整、排序、过滤、分页等功能,允许用户根据需求定制界面。 2. 数据绑定组件(Data-...

    C#超市商品管理

    此外,数据可视化工具如Charts和DataGrids可以帮助用户直观理解数据。 5. 安全与权限管理:对于多用户环境,系统需支持用户登录和权限控制。C#的身份验证和授权机制可以实现这一功能,确保只有授权的用户能访问特定...

    DevExpress 9.3.2 中文版

    5. **DataGrids**:无论是WinForms还是ASP.NET,数据网格都是关键组件,用于展示和操作大量数据。DevExpress的数据网格控件提供了丰富的特性和性能优化。 在压缩包中,我们看到了几个重要的文件: - **gac.bat**:...

Global site tag (gtag.js) - Google Analytics