Grid wont refresh using inline add

QuestionsGrid wont refresh using inline add
Grayson asked 4 years ago

why when using inline insert, the grid wont refresh after save.
im using custom sql query

here's the example using dummy data

$opt["on_insert"] = array("add_purchase_request_detail", null, false);

function add_purchase_request_detail()
{
global $grid2;

$str = "INSERT INTO
purchase_request_detail
(
purchase_request_id
)
VALUES
(
'1'
)";

$grid2->execute_query($str);

}

6 Answers
Grayson answered 4 years ago

error caught on console,
when set opt to true, no error, and grid will refreshed
$opt["on_insert"] = array("add_purchase_request_detail", null, true);

here's the error caught:

jquery.min.js:4 Uncaught SyntaxError: Unexpected end of inputn.parseJSON @

jquery.min.js:4jQuery.jqGrid.addParams.addRowParams.aftersavefunc @ purchase.php:381a.ajax.a.extend.complete @ jquery.jqGrid.min.js:399j @ jquery.min.js:2k.fireWith @ jquery.min.js:2x @ jquery.min.js:4b @ jquery.min.js:4send @ jquery.min.js:4n.extend.ajax @ jquery.min.js:4a.jgrid.extend.saveRow @ jquery.jqGrid.min.js:398b.fn.jqGrid @ jquery.jqGrid.min.js:34e.save.a.jqGrid.onClickButton @ jquery.jqGrid.min.js:409(anonymous function) @ jquery.jqGrid.min.js:361n.event.dispatch @ jquery.min.js:3r.handle @ jquery.min.js:3n.event.trigger @ jquery.min.js:3(anonymous function) @ jquery.min.js:3n.extend.each @ jquery.min.js:2n.fn.n.each @ jquery.min.js:2n.fn.extend.trigger @ jquery.min.js:3n.fn.(anonymous function) @ jquery.min.js:4onclick @ purchase.php:1

Abu Ghufran answered 4 years ago

Hi,

The inline add should return a JSON string with convention: e.g. id:insert_id, success:true

{"id":21,"success":true}

So in your callback (with continue_default:false), you must return json string as mentioned below:

$opt["on_insert"] = array("add_purchase_request_detail", null, false);
$grid->set_events($opt);

function add_purchase_request_detail()
{

$id = $grid2->execute_query($str,"insert_id");

$res = array("id" => $id, "success" => true);
echo json_encode($res);
}

Grayson answered 4 years ago

thankyou, that solved my problem.
then, is it possible if i dont want to use execute query

for this line:
$id = $grid2->execute_query($str,"insert_id");

instead i want to use traditional method:
mysqli_query($conn, $sql)

thankyou

Abu Ghufran answered 4 years ago

Yes. doable.

You need to return insert id as json object (mentioned above)

Grayson answered 4 years ago

oh, i got it
use mysqli_insert_id($conn), return the same thing.

what if i dont pass id to JSON array?
like this :
$res = array("success" => true);
echo json_encode($res);

I also got no error using this method.
I just want to know what does the ID passed to JSON array for?

thankyou for your answer

Abu Ghufran answered 4 years ago

It's for other cases compatibility.
If it's working for you then ignore it.

Your Answer

11 + 2 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?