Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional actions for Edit/Delete/Update #620

Open
prasanthchinna opened this issue May 24, 2013 · 9 comments
Open

Conditional actions for Edit/Delete/Update #620

prasanthchinna opened this issue May 24, 2013 · 9 comments

Comments

@prasanthchinna
Copy link

My requirement is to show Edit/Delete/Add link on conditional based using conditional actions. Can anybody help me.Please its a urgent requirement.

Thanks in advance.

@TroyWitthoeft
Copy link

I'm actually looking into the same thing right now. If you find a good solution, please share. I will do the same.

One solution is creating two jTables. One with all the needed actions

            actions: {
listAction: '/Demo/StudentList',
deleteAction: '/Demo/DeleteStudent',
updateAction: '/Demo/UpdateStudent',
createAction: '/Demo/CreateStudent'
},

and one without. You would use your server-side language and create logic which choose which table to display. But, that really is heavy handed way to achieve the desired result. Which server-side language are you working with? PHP? JS? C#?

@TroyWitthoeft
Copy link

Got it! This stack overflow question helped me to understand how I could conditionally execute javascript inside an MVC Razor view. Here is my example code.

            actions: {
                listAction: '@Url.Action("ListAudit")',
                @if (false)
                {
                    deleteAction: '/Demo/DeleteStudent', 
                }
                updateAction: '/Demo/UpdateStudent',
                createAction: '/Demo/CreateStudent'
            },

Simply replace false with whatever logic you need. For instance, you could check whether a user is in a particular role and only show the delete button if it is true.

                
                @if (User.IsInRole("admins"))
                {
                 deleteAction: '/Demo/DeleteStudent', 
                }

@prasanthchinna
Copy link
Author

Thanks for your post, actually i' m working with Asp.net, can yu provide me the above code with bit more detail, because i tried it, its not working proper

@TroyWitthoeft
Copy link

Github is stripping out the text tags <> from my code. You will need those! Check the link I posted for an example using text tags. I'll update this post with a better example tomorrow.

@prasanthchinna
Copy link
Author

Thank you ll be waiting for your post

@TroyWitthoeft
Copy link

The magic happens inside the

@if (yourcondition) 
{ 
<text>
// your conditional javascript code
</text>
}

Here is a more full example. Once again, this is ASP.NET MVC with Razor views.

$(document).ready(function () {

    //Prepare jtable plugin
    $('#EmployeeTable').jtable({
        title: 'The Employee List',
        sorting: true, //Enable sorting
        defaultSorting: 'Last ASC', //Sort by Name by default
        multiSorting: true,
        paging: true,
        pageSize: 25,
        actions: {
            listAction: '@Url.Action("ListEmployee")',
            @if (User.IsInRole("Admins"))
            {
                <text>  deleteAction: '@Url.Action("DeleteEmployee")',
                        updateAction: '@Url.Action("UpdateEmployee")',
                        createAction: '@Url.Action("CreateEmployee")'  </text>
            }  
        },
        fields: {
            EmployeeID: {
                key: true,
                create: false,
                edit: false,
                list: false
            },

@debianlinux
Copy link

This doesn't work for me. I have no idea what MVC3 or Razor are. I'm looking at simple javascript with php doing the SQL manipulation on the server side. As for the initial jtable layout, the only thing happening on the page is javascript and some supporting CSS.

Using the @if function in any way causes the jtable to not display. Commenting out the offending lines restores it to functionality. Any pointers in accomplishing this in standard javascript/html/php would be great. As you can see below I am manually declaring a variable to conditionally assess but ultimately I will be using a php $_SESSION variable which is largely irrelevant to this example which is why I simplified it so.

Code sample:

<script type="text/javascript">
    var var1 = 1;
        $(document).ready(function () {

            //Prepare jTable
            $('#Projects').jtable({
                title: 'Projects',
                jqueryuiTheme: true,
                paging: true,
                pageSize: 10,
                sorting: true,
                selecting: true,
                defaultSorting: 'projectid ASC',
                actions: {
                    listAction: 'projectsactions.php?action=list',
                    createAction: 'projectsactions.php?action=create',
                    updateAction: 'projectsactions.php?action=update',
    @if (var1 == 1) {
            <text>
                    deleteAction: 'projectsactions.php?action=delete'
            </text>
                    }
                },
                fields: {
                    projectid: {
                        key: true,
                        sorting: true,
                        title: 'Project ID',
                        width: '10%'
                    },

@TroyWitthoeft
Copy link

Sorry, my example doesn't work for PHP, it only works for those jTable user's using ASP.NET as their server side language. Please do post when you get a working PHP example, I'm sure that would be useful! Have you tried kjoye's solution for PHP over here?

@debianlinux
Copy link

Wow, not sure how I missed that as it is referenced both ways and I had already read that thread. Thanks, that works for this purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants