EasyUI Forum
September 14, 2025, 04:43:09 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Datagrid - Creating a new row when no rows currently exist  (Read 28367 times)
swader
Newbie
*
Posts: 11


View Profile
« on: March 28, 2012, 03:54:30 AM »

I am using the detailview script to create and edit rows through an expanded row with a detailed form helper.
This is the error I get when I want to create a new row and there are no rows in the grid currently present:

Code:
rows is undefined
[Break On This Error]

for(var i=0; i<rows.length; i++) {

I could hack my way through detailview to make a hotfix, but I'd rather avoid it if it is possible.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: March 28, 2012, 06:42:30 PM »

Could you describe more detail about your question? Some code may be more appropriate.
You can also refer to the detail view form example http://www.jeasyui.com/tutorial/app/crud3/index.html
Logged
swader
Newbie
*
Posts: 11


View Profile
« Reply #2 on: March 29, 2012, 12:01:18 AM »

Certainly.

I have a datagrid made like this:

Code:
var identifier = some table
var dataURL = some url from which I fetch data
var getURL = some url from which I get detailview

                       $(identifier).datagrid({
striped: true,
method: 'get',
pagination: "true",
pageSize: 20,
fitColumns: true,
loadFilter: function(data) {
return {rows: data.response, total: data.total}
},

url: 'dataURL',

view: detailview, 
detailFormatter:function(index,row){
return '<div class="ddv"></div>';  k
},
onExpandRow:function(index,row){
var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
$.get(getURL, {slug: row.slug, index: index}, function(data){
$(ddv).html(data);
$(identifier).datagrid('fixRowHeight');
$(identifier).datagrid('fixDetailRowHeight', index);
}, "html");
}

});

The code that creates a new row is this:
Code:
function ui_datagrid_newRow(identifier) {
$(identifier).datagrid('appendRow',{
isNewRecord:true
});
var index = $(identifier).datagrid('getRows').length - 1;
$(identifier).datagrid('expandRow', index);
$(identifier).datagrid('selectRow', index);
}

If the table is empty when I call "function ui_datagrid_newRow", I get this error:
Code:
rows is undefined
for(var i=0; i<rows.length; i++) {
on line 60 of jquery.datagrid-detailview.js

Does this help?
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 on: March 29, 2012, 12:52:54 AM »

What your 'dataURL' return when no rows exist? Notice that your code below:

Code:
loadFilter: function(data) {
  return {rows: data.response, total: data.total}
},

Be sure to make the rows is an array object even though it is empty. Try correct your code as:

Code:
loadFilter: function(data) {
  return {rows: data.response || [], total: data.total || 0}
},
Logged
swader
Newbie
*
Posts: 11


View Profile
« Reply #4 on: March 29, 2012, 01:18:28 AM »

My response always returns the array object, even if empty, like so:

{"response":[],"total":"0"}

However, if I do the fix you suggested, I don't get an error any more, but the new row still isn't appended.
I tried doing a console.log before and after the appendRow action, and both console.logs trigger, but the append never happens. The table stays the same and doesn't get a new row.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #5 on: March 29, 2012, 02:53:16 AM »

Please confirm your appended data can be post and saved properly. You can also download a new detail view file and try run your page again.
Logged
swader
Newbie
*
Posts: 11


View Profile
« Reply #6 on: March 29, 2012, 05:39:19 AM »

Downloaded the new detailview script, everything is the same.

I don't understand this:
"Please confirm your appended data can be post and saved properly."
Could you explain?

Which appended data? The appended data should only be an empty row, and it should go fetch the detailview from my detailview URL. But it never even makes the request, it just falls silent. I have tracked the problem to function _40b in the obfuscated source, but I could get no further.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #7 on: March 29, 2012, 06:56:45 AM »

After call appendRow method, As you say the new row is not appended. Is it not appended or has appended but not display?
The example below can run properly, may be it can give you some hints.
http://www.jeasyui.com/tutorial/app/crud3/index.html
Logged
swader
Newbie
*
Posts: 11


View Profile
« Reply #8 on: March 30, 2012, 04:37:52 AM »

The row doesn't get appended at all. All I get is that error if I don't have
return {rows: data.response || [], total: data.total || 0}
and if I do have it nothing happens at all.

If the code was unobfuscated I could tell you exactly where the problem occurs, but it's really hard like this.

If I try to run appendRow manually on a datagrid without rows, I get this

Code:
var rowLength = $(target).datagrid('getRows').length;  // line 100 in datagrid-detailview.js 

But if I run it on a datagrid that HAS rows, everything is fine.
Logged
swader
Newbie
*
Posts: 11


View Profile
« Reply #9 on: March 30, 2012, 05:38:53 AM »

I found out something interesting.

In your example, when your datagrid has no rows (i.e. I delete them all), then this call:
$("#dg").datagrid('getRows');
returns "[]";

In my example, when I delete all rows,
$("#dg").datagrid('getRows');
returns "undefined";
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!