I have a grid, based on a View, which calculates a field and displays a button if a record in a related table isn't present. The user can then click the button, to upload an image to this table.
This works well, but on inserting a new record, the grid isn't showing this button - as the database hasn't been requeried. I'm assuming I need to add a read of the newly created record, before returning the JSON to the grid, for the insert method.
currently the create is:-
if
(
$type
==
'create'
)
{
try{
$result
=
new
DataSourceResult(
"mysql:host=$host;dbname=$dbname"
,
$username
,
$password
);
}
catch(PDOException
$e
)
{
echo
$e
->getMessage();
}
$columns
=
array
(
'IssueNo'
,
'Title'
,
'Month'
,
'Year'
,
'ReadComic'
,
'Characters'
,
'Location'
,
'Publisher'
);
$data
=
$result
->create(
'comics'
,
$columns
,
$request
,
'ID'
);
}
How can change this to do a read of the view and return the new record to the grid? I'm not sure where the newly created ID would be to achieve this.
Thanks
5 Answers, 1 is accepted
Could you please advise how the Grid determines if the buttons should be added or not?
In general, we recommend using a template for the column that is shown the Grid. Then in that column to use a custom function to determine what should be displayed. This function will be called when the new record is added and it will make the needed calculations.
$category
->field(
'Category'
)
->title(
'Category'
)
->template(
'#=custonFunctionName#'
)
Then in the script file on the client, the function should be added:
function
buttonFunction(value){
//make the calculations
if
(buttonShoudBeAdded){
return
"<button>Test</button>"
}
return
" "
}
If this approach is not helpful, please share with us more details about the scenario and we will gladly assist further.
Regards,
Stefan
Progress Telerik
Thank you for the clarification.
In order the provide more detailed information we will need an example demonstrating the scenario. For example, we will need the logic used to determine if the button will be shown and also when this logic is executed. This will help us advise if there is a better place to execute the logic.
In general, the Grid provides many events which can be utilized in order to call the database when needed:
The read method of the dataSource can be used to call the database:
http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events
http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-read
If the scenario is PHP specific, please have in mind that we are not full experts in PHP and can assist mainly on how our controls are integrated with PHP.
Still, we will be happy to assist and provide additional information when needed.
Regards,
Stefan
Progress Telerik
I'm trying to replicate similar logic I've used in .NET MVC, where some fields used by the grid are calculated/linked in a view on the database (or a partial class in MVC). As these fields aren't available on the input form, the grid won't be able to properly update itself on the client-side.
In MVC its possible to pass back the full object (after doing an insert, and a new read for the newly inserted record) to the grid via the controller.
In PHP you provide the DataSourceResult.php file, which provides a create function. I'm calling this via my php script here:-
if
(
$type
==
'create'
)
{
try{
$result
=
new
DataSourceResult(
"mysql:host=$host;dbname=$dbname"
,
$username
,
$password
);
}
catch(PDOException
$e
)
{
echo
$e
->getMessage();
}
$columns
=
array
(
'IssueNo'
,
'Title'
,
'Month'
,
'Year'
,
'ReadComic'
,
'Characters'
,
'Location'
,
'Publisher'
);
$data
=
$result
->create(
'comics'
,
$columns
,
$request
,
'ID'
);
}
The $data variable is then passed back to the grid as JSON, which includes the newly created record ID.
I just need to know how to get the ID from the $data variable, and how to pass back data to the grid. Then I can add another read (to the view) for the newly inserted record, and update the grid with the template data (which I've generated in the view - as it checks if a related record is present or not).
Unfortunately debugging PHP is tricky, especially when passing back JSON, otherwise I'd inspect the $data variable, and see what it contains.
Whilst you may not be php experts,hopefully as you've created the DataSourceResult.php script, you'll be able to explain how it works.
We know how the DataSourceResult is working in PHP, but it is unclear how the application is structured. If possible please provide either the current PHP example or the MVC example where this is achieved. This will help us to understand the data flow and provide a suggestion best suited for it. I do understand that providing an example is not an easy task, but based on the provided information the scenario is a very complex one and a straightforward answer may not be applicable to it.
We will be expecting the example and we will gladly inspect it and try to provide a suggestion best suited for it.
Regards,
Stefan
Progress Telerik