【问题标题】:Add custom button in jtable在 jtable 中添加自定义按钮
【发布时间】:2013-05-25 08:16:39
【问题描述】:

我们可以在 jtable 中添加自定义按钮吗?是否有任何选项可用于创建按钮?
意思是如果我想要一个按钮来创建PDF,那我该怎么办?

【问题讨论】:

    标签: jquery jquery-jtable


    【解决方案1】:

    要插入一个按钮,您必须使用display: 函数,并根据您的选择自定义它;即我创建了一个带有按钮的列:变量data 包含当前记录的数据。

    $(document).ready(function () {
    
        $('#StudentTableContainer').jtable({
            title: 'The Student List',
            paging: true, //Enable paging
            pageSize: 10, //Set page size (default: 10)
            sorting: true, //Enable sorting
            defaultSorting: 'Name ASC', //Set default sorting
            actions: {
                listAction: '/Demo/StudentList',
                deleteAction: '/Demo/DeleteStudent',
                updateAction: '/Demo/UpdateStudent',
                createAction: '/Demo/CreateStudent'
            },
            fields: {
                StudentId: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                Name: {
                    title: 'Name',
                    width: '40%'
                },
                EmailAddress: {
                    title: 'Email address',
                    list: false
                },
                Password: {
                    title: 'User Password',
                    type: 'password',
                    list: false
                },
                Gender: {
                    title: 'Gender',
                    width: '20%',
                    options: { 'M': 'Male', 'F': 'Female' }
                },
                MyButton: {
                    title: 'MyButton',
                    width: '40%',
                    display: function(data) {
                         return '<button type="button" onclick="alert(' + data.record.StudentId + ')">create PDF</button> ';
                    }
                },
    
            }
        });
    
        //Load student list from server
        $('#StudentTableContainer').jtable('load');
    });
    

    【讨论】:

    • 是的,这很好,但我想更进一步,我的问题是,如果我们给这个按钮一个像 class='button1' 这样的类,那么我们可以使用这个类将任何事件附加到这个按钮
    • 当我使用上面的代码时,我得到一个错误数据未定义
    • 关于错误,页面的结果(即listAction: '/Demo/StudentList',)应该是一个json对象。在 php 中:$row = array("name"=&gt;"me"); $jTableResult = array(); $jTableResult['TotalRecordCount'] = $num_rows; $jTableResult['Result'] = "OK"; $jTableResult['Records'] = $rows; print json_encode($jTableResult); 所以定义了 data.record.name。另请查看文档以获取更多帮助:jtable.org/ApiReference#fopt-display关于附加事件,试试这个,它应该可以工作:$( ".myclassbutton" ).bind( "click", function() { alert( "User clicked on 'foo.'" ); });
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2012-07-29
    相关资源
    最近更新 更多